A bit of background, in the hopes that it will help you save some time:
At The Job, I need PDF generation. We want to generate various documents in PDF form that are professional looking without requiring our users print a particular web page to some PDF printer. I’d prefer it be really easy for me to write, of course, but if it’s something I can do once and never again, I can probably live with a convoluted DSL of some sort. So, once upon a Google, my search began.
… and it was frustrating.
I started with Princely, a Rails plugin that uses the PrinceXML library to generate PDFs from Rails views. It’s super-simple. It’s almost everything I wanted in a PDF generation plugin. It allows you to do all your layout and prettifying with CSS and HTML, which is sweet because — damn — I already know HTML and CSS. Hell yes.
Except that it watermarks all your PDFs with a blue square containing a white ‘P’. You can get rid of it if you’ve got $3800. Verdict? Not worth it.
I moved to playing with Prawn, which is essentially a Ruby-ish DSL for laying out and generating PDFs. It was a nightmare. Check out this gist to get an idea of what I mean. It can’t be printed here. It’s too big and cumbersome. It’ll break my blog. … Here’s a taste:
1 def info_table
2 tags :blue => { :color => ‘blue’ },
3 :green => { :color => ‘green’ }
4
5 bounding_box [235, 700], :width => 300 do
6 font "Times-Roman"
7 fill_color(‘999999’)
8 text "TERM SHEET", :size => 24, :align => :right
9 fill_color(‘000000’)
10 font "Helvetica"
11 text "<blue>Keep IT Safe,</blue> <green>Keep IT Simple</green>", :size => 12, :align => :right
12 end
13 bounding_box [260, 650] do
14 table [[ticket</span>, <span class="iv">sales_rep,doc_no</span>, <span class="iv">project, @date]],
15 :headers => [‘TICKET’, ‘SALES REP’, ‘DOC. NO.’, ‘PROJECT NO.’, ‘DATE’],
16 :header_color => ‘000000’,
17 :header_text_color => ‘ffffff’,
18 :align_headers => :center,
19 :vertical_padding => 2,
20 :horizontal_padding => 2,
21 :font_size => 8,
22 :width => 272,
23 :border_width => 0.2,
24 :align => { 0 => :center, 1 => :center, 2 => :center, 3 => :center, 4 => :center, 5 => :center, 6 => :left }
25 end
26 end
So, with a little help from Elijah Miller and the Indy.rb IRC channel (#indyrb on Freenode), the shell utility wkhtmltopdf landed in my lap. It uses the WebKit engine via QT 4.4 to render HTML and CSS and convert it to PDF. It’s free. It’s exactly what I needed. Based upon wkhtmltopdf, the Wicked PDF plugin was born to make PDF generation in your Rails app easier and cheaper than it was previously.
First of all, it’s wicked-easy to get started. After you’ve visited wkhtmltopdf and installed wkhtmltopdf, simply:
script/plugin install git://github.com/mileszs/wicked_pdf.git
1 class ThingsController < ApplicationController
2 def show
3 respond_to do |format|
4 format.html
5 format.pdf do
6 render :pdf => "file_name",
7 :template => "things/show.pdf.erb",
8 :layout => "pdf.html"
9 end
10 end
11 end
12 end
I hope this helps others who have travelled the long, lonely, crufty, potentially expensive route of finding a sufficiently powerful and simple PDF generator. If you find it lacking, or if you find a bug, please report it to Wicked PDF’s Issues page so I can make it do right.
Look:

You know the Wicked Witch likes it.
(0 Comments)