generating pdf on heroku

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:

  • use prawn: it is pure ruby, very powerful. It has it's own DSL, that unleaches all the power of building a PDF, but at the same time: it seems to be very hard and tedious.
  • use some sort of HTML to PDF conversion. In ruby there exists two gems: wicked_pdf and PDFKit, both use 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 my to just render a view which would automatically be downloaded as a PDF.

Setting up wicked_pdf in Rails to run on heroku

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: [ruby] gem "wicked_pdf" gem "wkhtmltopdf-heroku", :git => 'git://' [/ruby] And, then, inside a view you want to render as pdf, write something like [ruby] 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' } } end end [/ruby] 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.

Robin 2013-02-06 16:44:21 UTC

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

Recent comments