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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
I appreciate what the Prawn library can do. I appreciate all the hard work that has been put into it. It’s just not for me. So not worth it.
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:
In your controller:
1 2 3 4 5 6 7 8 9 10 11 12
You don’t have to specify a template, as it will choose the template for the current controller and action by default. Also, by default, layout is set to false, so you may want to keep that around.
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.
You know the Wicked Witch likes it.