Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Prawn on Heroku

We are currently working on a Rails application hosted on Heroku. We are trying to generate a PDF and push it to the user to download.

We are using Prawn to handle the PDF generation.

Our code for generating the PDF is currently:

Prawn::Document.generate @name[0]+ ".pdf" do

Followed by all of our code to generate the document. Unfortunately, this saves the document to the disk which is not possible (to the best of my knowledge) for applications hosted on Heroku.

We then push it to the user using

send_file "#{Rails.root}/"+@name[0]+ ".pdf", :type =>
'application/pdf',:filename => @name[0]+ ".pdf"

Is there any way using Prawn to directly push the download of the document to the user without saving the document to disk first? If not, are there any other gems for generating PDFs that don't require saving the file to the disk prior to sending the file?

like image 275
Matt K Avatar asked Mar 12 '12 21:03

Matt K


1 Answers

Though this was answered long ago, I'll post for others who may want to do this.

You can also call render with no file name in current Prawn v0.13.2. A string will be returned, which can be sent to the client with send_data. The pattern is:

pdf = Prawn::Document.new
# ... calls to build the pdf
send_data pdf.render, 
          type: 'application/pdf', 
          filename: 'download_filename.pdf',  
          disposition: :inline

This will display the PDF in the browser. If you want instead to have the user download it, omit , disposition: :inline

Of course you only want to do this if the document is reasonably short or your system is not heavily used because it will consume RAM until the user's download is complete.

like image 187
Gene Avatar answered Oct 06 '22 06:10

Gene