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:

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
   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.