Save PDF file shown by PDFKit middleware
Asked Answered
N

1

8

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.

Noguchi answered 1/3, 2011 at 11:45 Comment(3)
I may have been better to post this in question form, then answer and accept your own question. This is now permanently listed as an unanswered question.Anchusin
Just another fun reminder to please convert your edit into an answer :)Noachian
I have edited part of the question and added it as an answer.Congressional
C
3

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.

Congressional answered 1/8, 2013 at 17:8 Comment(1)
This answer was provided by the asker himself. I am changing it into Q&A formatCongressional

© 2022 - 2024 — McMap. All rights reserved.