开发者

Save PDF file shown by PDFKit middleware

开发者 https://www.devze.com 2023-02-14 06:38 出处:网络
If somebody is interested in saving the PDF file in the filesystem which is shown by 开发者_运维问答PDFKit middleware gem, then here it is...

If somebody is interested in saving the PDF file in the filesystem which is shown by 开发者_运维问答PDFKit middleware gem, then here it is...

  1. Override the call method of middleware.rb file.
  2. In overriding just replace this line:

    body = PDFKit.new(translate_paths(body, env), @options).to_pdf
    

    with

    pdf = PDFKit.new(translate_paths(body, env), @options)
    file = pdf.to_file('Your/file/name/path')
    Mymodel.my_method()     #You can write your method here to use that file
    body = pdf.to_pdf   #Here you can change the response body
    

You can also override the response-body and content-type if you don't want to give pdf response. If you have any further query then go ahead.

This procedure is really help full because when you are having heavy JavaScript an CSS in your view file then render_to_string method will not work i.e. it will not render the heavy JavaScript.


If somebody is interested in saving the PDF file in the filesystem which is shown by PDFKit middleware gem, then here it is...

  1. Override the call method of middleware.rb file.
  2. In overriding just replace this line:

    body = PDFKit.new(translate_paths(body, env), @options).to_pdf
    

    with

    pdf = PDFKit.new(translate_paths(body, env), @options)
    file = pdf.to_file('Your/file/name/path')
    Mymodel.my_method()     #You can write your method here to use that file
    body = pdf.to_pdf   #Here you can change the response body
    

You can also override the response-body and content-type if you don't want to give pdf response. If you have any further query then go ahead.

This procedure is really help full because when you are having heavy JavaScript an CSS in your view file then render_to_string method will not work i.e. it will not render the heavy JavaScript.

0

精彩评论

暂无评论...
验证码 换一张
取 消