I was investigating ways to generate pdf's in Ruby on Rails, but I had one enormous constraint: it had to deploy on heroku.
There are two very different ways to generate pdf's in ruby:
wkhtmltopdf
under the covers. I dreamed of having a view magically converted to PDF.I went for the second option. Furthermore, I choose wicked_pdf
over PDFKit
, because I felt the rails integration was better. It allowed me to just render a view which would automatically be downloaded as a PDF.
Luckily, getting it running on heroku proved to be incredibly easy: just including the correct gem with the binaries that work on heroku.
In my Gemfile
I added the following:
gem "wicked_pdf"
gem "wkhtmltopdf-heroku", :git => 'git://github.com/camdez/wkhtmltopdf-heroku.git'
And, then, inside a view you want to render as pdf, write something like
respond_to do |format|
format.js
format.pdf {
render :pdf => "show", :header => { :font_size => '8', :right => '[page] of [toPage]' }, :footer => {:font_size => '8', :right => 'Generated by jottinx.com' }
}
end
Then, you will still have to create the view, show.pdf.erb
. Just make sure your view renders HTML and it will be converted to PDF correctly. That is just awesome.
Hope this helps.
Comments
This worked great on my Rails 3 app. Note: the wkhtmltopdf-heroku is now an official gem (don't need to reference the git repo). Thank you very much
Add comment