XHTML to PDF with CSS and Javascript
There are tons and tons of HTML to PDF type tools out there. For basic HTML with little or no CSS and javascript these are ok. If your using CSS2/CSS3 or javascript for some of your layout, the majority of the tools out there don’t get the job done.
NOTE: If all you need is basic HTML to PDF conversion without CSS or javascript I’d suggest using XHTML2PDF.
Most HTML to PDF converters out there make their own rendering engine, which means they try to figure out the HTML and change that to PDF. These tools typically do well with basic HTML but don’t even bother with or have limited support for CSS and javascript.
Every developer knows the problem with trying to make a site look and work the same in different browsers. Using a tool with it’s own rendering engine is like trying to make your site work in yet another browser.
The approach I found that works best is to use a standard web browsers rendering engine to deal with the CSS/Javascript and such then try to get that converted into a PDF. There are a few commercial tools that use IE’s rendering engine, but it’s hard to find out from the software companies which rendering engine (IE, Firefox/Gecko, Safari, etc) they use and what version. Not to mention the cost and support issues.
The tool I like the best is wkhtmltopdf. It’s a OpenSource tool which uses the Webkit rendering engine. It’s a basic shell tool which works on Windows/Mac/Linux. This tools uses Webkit to render the page, then turns that into a PDF. Webkit is what Apple’s Safari browser uses and originally came from KDE’s Konqueror.
wkhtmltopdf has a lot of cool features like:
- Custom and default PDF header/footer
- username/password/proxy info for protected content
- Book and TOC ability. So you can show all the pages on the left of your reader and get a auto-generated TOC at the start of the document.
- Works on Windows/Mac/Linux, was cake to install and use
Basic Usage:
wkhtmltopdf http://website/page.html /path/to/page.pdf
That command would go to http://website/page.html using the Webkit rending engine and turn that into a PDF that went to /path/to/page.pdf. Very simple.
I would love to find a tool like wkhtmltopdf that used either the Firefox/Gekco or IE rendering engine, but couldn’t find anything that worked as well as wkhtmltopdf. It would be even better if there was a tool that would allow you to choose between the various rending engines. But I’ll take what I can get, and for right now wkhtmltopdf is the best OpenSource option available.

