Save PDF file shown by PDFKit middleware

If someone is interested in saving a PDF file in a file system that is shown in PDFKit middleware gem format, then here ...

  • Override the call method of middleware.rb.
  • When overriding, simply replace this line:

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

    from

     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 do not want to give the answer in pdf format. If you have another request, then continue.

This procedure is really complete, because when you have heavy JavaScript CSS in your view file, then the render_to_string method render_to_string not work, that is, it will not display heavy JavaScript.

+6
ruby ruby-on-rails pdfkit
source share
1 answer

If someone is interested in saving a PDF file in a file system that is shown in PDFKit middleware gem format, then here ...

  • Override the call method of middleware.rb.
  • When overriding, simply replace this line:

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

    from

     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 do not want to give the answer in pdf format. If you have another request, then continue.

This procedure is really complete, because when you have heavy JavaScript CSS in your view file, then the render_to_string method render_to_string not work, that is, it will not display heavy JavaScript.

+1
source share

All Articles