PDF generation, the easy way
December 10th, 2009 by dimitry
Thanks to a tip-off from Matthew, I’ve finally seen the light in efficient, on-the-fly HTML to PDF generation. Prepare to be enlightened.
The old way
First, you’d need to install a PDF generation library such as PDFLib or FPDF. Then you need to study its documentation and figure out how to lay out the PDF document to closely mimic your HTML page. If installing one of those libraries is not tedious enough, step two is exceptionally time consuming and irritating.
To add to the headaches, you now have to make changes in two places, making maintenance harder. Do you like to do extra work? I don’t. I’d rather be outside, frolicking in a field, chasing butterflies.
Enter the new way
The new way calls for a browser. Instead of designing a custom PDF document, we simply want to take our current HTML, print it to a PDF file and serve it to the user. Mac OS X users have had this functionality for years. In Windows, printing to a PDF file required installing a custom printer driver. Yuck. We decided to take the end-user’s capabilities out of the equation and simply installed a browser on our server.
The idea is to take an HTML file, load it in the browser on our server and make the browser print it to a PDF file.
Sounds simple, right? Almost. Turns out that running Firefox on a Linux machine requires an X11 display to be available. This adds another layer of complexity that we didn’t want. Then I stumbled onto wkhtmltopdf (read: Webkit HTML to PDF) and it saved the day.
Latest version of wkhtmltopdf doesn’t require an X11 display, freeing up resources and headaches. More importantly, check out the easy of use:
wkhtmltopdf http://www.deeplocal.com deeplocal.pdf
Genius! Now we can limit all the maintenance to one HTML file. Time is saved and we’re all happy.
-
whit537