Software dev, tech, mind hacks and the occasional personal bit

HTML to PDF Conversion Plugin For Rails (A fork of wicked pdf)

Once a business web application reaches a certain size, the need often arises to generate PDFs from HTML/CSS.

Up until recently, the story around this for a MRI Rails application was not good. You could either use tools like Prawn, which require a description of the layout in a specific DSL, or pay for a tool like Prince XML which can convert from HTML, but which costs quite a bit. Those using JRuby were in a stronger position as they could use the Java PDF library called Flying Saucer.

The good news is that PDF generation for MRI Ruby is now easy and free, thanks to webkit, the open source webkit wrapper called wkhtmltopdf and mileszs’s wickedpdf plugin. I was really excited to come across this plugin and started to use it right away. However, it had a couple of issues:

  • Temp file handling caused errors when two PDFs were being generated within the same second (eg, 2 requests at almost the same time)
  • Problems generating PDF were not reported

Galdomedia forked the code and updated it to use standard Ruby temp files. This was great for ruby 1.7, but not good for Ruby 1.6 which does not allow you to set the extension on temp files (wkhtmltopdf relies on having a .html extension).

As my production servers run Ruby 1.6, I needed a different approach. My fork uses streams rather than temporary files, and adds some basic error handling and basic integration tests.

To install in a rails app:

script/plugin install git://github.com/jcrisp/wicked_pdf.git

Or clone the code from GitHub.

Previous

Presentation Zen by Garr Reynolds

Next

Si-Hing (Wing Chun Junior Instructor)

1 Comment

  1. Wow, Thanks! This worked in our production environment perfectly. You saved the day!!!!!!!

Powered by WordPress & Theme by Anders Norén