Convert web pages to PDF with wkhtmltopdf

wkhtmltopdf

It can often be useful to keep a local copy of a web page, and your browser’s "Save Page As" option is a good place to start. But typically this saves an HTML file and a separate folder with its resources, or an archive format like MHT, not so convenient if you’d like to share the document with others.

Wkhtmltopdf is an open source tool which quickly converts HTML to PDF, ready for viewing just about anywhere. It’s written for the command line, but don’t let that put you off -- you’ll be using it productively in seconds.

Saving a remote web page to PDF, for instance, is as simple as this:

wkhtmltopdf http://www.google.com google.pdf

Wkhtmltopdf uses WebKit to render the page, which means you get accurate PDFs which look and feel just like the original HTML -- complete with clickable links.

You can probably already guess how to convert a local file:

wkhtmltopdf c:\folder\MyFile.html MyFile.pdf

There’s no need to stop with a single file, though. Point the program at multiple HTML documents and it can combine them into a book with a cover and a table of contents:

wkhtmltopdf cover cover.html toc chapter1.html chapter2.html chapter3.html book.pdf

If you need more flexibility then there are plenty of more advanced switches available. You’re able to set the PDF’s page size, orientation, dpi, margins, headers, footers, and much, much more. Plus, as this is a command line tool, it’s easy to automate via Task Scheduler or your own scripts.

Although PDF will be the output format of choice for most people, wkhtmltopdf can also save Postscript files. Just specify .ps as the extension of your exported file:

wkhtmltopdf http://www.microsoft.com microsoft.ps

We found the occasional issue: text that wasn’t rendered as we expected, a document which had local (file://) rather than remote (http://) links. But no HTML converter is problem-free, and even at version 0.12.1 wkhtmltopdf is better than most. Well worth a look, although if you’d like to avoid the command line, HTML to PDF Tools is a similar GUI-based alternative.

2 Responses to Convert web pages to PDF with wkhtmltopdf

© 1998-2024 BetaNews, Inc. All Rights Reserved. Privacy Policy - Cookie Policy.