How to convert HTML pages to PDF format on Linux

Last updated on August 31, 2020 by Dan Nanni

While HTML is an excellent medium for distributing and consuming information on the web, it is not an ideal format as far as printing and archiving purposes are concerned. For that, PDF is a better format, as PDF documents have well-defined page layout, and have all contained images embedded into PDF files. If you would like to convert HTML pages to PDF format on Linux, follow this guideline below.

In Linux environment, you can use a command line utility called wkhtmltopdf to convert any HTML webpage or any live URL to a PDF file. wkhtmltopdf uses Webkit web browser rendering engine to perform HTML page download and HTML-to-PDF conversion.

Install wkhtmltopdf on Linux

On Ubuntu, Debian, Linux Mint:

On Debian-based system, you can install wkhtmltopdf from base repositories:

$ sudo apt-get install wkhtmltopdf

Please be aware that wkhtmltopdf installed via apt-get has reduced functionality and other limitations. First of all, it cannot run without X11 system. Also, it cannot add hyperlinks or a table of contents in the converted PDF file.

On Any Linux:

If you would like to use wkhtmltopdf without X11 system, while enjoying its full features, you can use a static binary of wkhtmltopdf that is distributed from its official website, where pre-build binaries are available for Debian, Ubuntu, CentOS, openSUSE, Arch Linux and others. This version is built with Qt and X11 integrated.

Convert an HTML Page to PDF with wkhtmltopdf

To convert HTML to PDF using wkhtmltopdf, use the following command.

$ wkhtmltopdf http://www.cnn.com cnn.pdf

Note that if you want to capture web pages hosted on https site, you need to install openssl first, and run xkhtmltopdf.

On Debian-based system:

$ sudo apt-get install openssl

On Red Hat-based system:

$ sudo yum install openssl

If xkhtmltopdf does not work for some reason, an alternative way to convert HTML web pages to PDF files is to use Google Chrome browser. If you don't have Google Chrome installed, install it first.

On Google Chrome, go to the URL of the web page you would like to convert to PDF. Then, choose Print a page menu of Google Chrome, and change Destination to Save as PDF. Once you click print button, the web page will be saved as a local PDF file that you designate.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Xmodulo © 2021 ‒ AboutWrite for UsFeed ‒ Powered by DigitalOcean