Monday, February 17, 2014

wkhtmltopdf converts HTML to PDF

Most - if not all - new electronic devices don't come with printed manuals. For the Sony laptop SVF15A1 I bought, I had to go to the Sony web site to access the user guide. The user guide is composed of many individual HTML pages. This posted a problem for me because I wanted to convert some HTML pages to PDF documents for easier off-line access.

Modern web browsers, such as Chrome and Firefox, have the built-in print to PDF feature.

For Chrome,

  1. Navigate to the HTML page, right click, and select Print.
  2. Select Save as PDF to be the Destination.
  3. Click Save, and select the output directory to save in.

Note that the output PDF file is automatically named User Guide _ Using the Touch Pad.pdf. I did not have to manually make up the file name because the default name was extracted from the HTML page content. This saves so much time for users that it becomes a major advantage for using the browser print function to do the conversion.

If you prefer a command-line solution, wkhtmltopdf is a simple yet powerful tool to convert HTML to PDF.

$ wkhtmltopdf -s Letter http://docs.esupport.sony.com/pc/SVF14A1_15A1_series/EN/contents/TP0000053442.html UserGuideTouchPad.pdf
Loading page (1/2)
Printing pages (2/2)
Done               

wkhtmltopdf can convert HTML files stored on the local hard drive or over the Internet. The above example specifies a URL from the Sony support web site.

By default, the pages in the output PDF file are of the size A4. North American users may specify the page size Letter using the -s parameter.

The last parameter to wkhtmltopdf is the mandatory output file name. Despite its rich set of parameters, it is missing the feature to automatically name the output file. To harness the full power of wkhtmltopdf, please refer to the man page.

If you want to merge individual PDF files, please see my posts on pdftk, and ImageMagick.

1 comment:

Mark Henry said...

Apart from HTML to PDF conversion, you may also need to convert JPG or image files into PDF. There is this one awesome JPG to PDF converter that is available. This JPG to PDF converter from PDFOnline is compatible with multiple formats and delivers power packed performance.