开发者

Streaming output from Prawn/Prawnto in Rails before render

开发者 https://www.devze.com 2023-02-19 18:18 出处:网络
Does anyone have any examples of starting the file stream before the rendering/building process when using Prawn?

Does anyone have any examples of starting the file stream before the rendering/building process when using Prawn?

The situation is that I have a report of 350 or so pages that takes about 60 seconds to run on my own workstation, but when moving it to production (EC2) the time to run the report goes up over 300 seconds and when it finishes it does not send the PDF to the browser.

Even 60 seconds is a long time without any user feedback but 300 just doesn't work even if you wait. What I have been looking to do is to start the file download before the rendering process (I am using a .pdf.prawn view for this with Prawnto), so that at least the user can see something is happening while it processes, but so far have been unable to do this. All the examples and guides I have found start the download after the render has finished, and after the long wait.

If all else fails I will look at scheduling the report but ideally the user would be able to view the report, then go back and make changes and run it again, which scheduling obviously wouldn't allow.

Not really much code to show with Prawnto as it's all just PDF layout in the view, and called via this in the controller. I have tried send_file, and moving the PDF layout to the controll开发者_如何转开发er to run it without Prawnto, but all changes seem to give the same result - the PDF is sent to the browser after the long wait.

I am out of ideas at this point and would appreciate any ideas!

 respond_to do |format|
  format.pdf {
    prawnto :prawn => {:page_layout => :portrait, :skip_page_creation => true}, :inline => false
    render :layout => false
  }


If it takes >60s to generate your PDF you should consider offloading it to a background worker. Using Delayed::Job or Resque is recommended.

Have the worker create the PDF and save it to disk; throw a flag (e.g. in a database or whatever) to indicate the PDF is ready for download.

This will add some extra complexity to your app, but the user experience is greatly enhanced and you don't have the 'can't download PDF' problem you describe. Also, if you app is heavily used, you can off-load the PDF-creating workers to a different server more easily.

0

精彩评论

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

关注公众号