Wkhtmltopdf – A Smart Tool to Convert Website HTML Page to PDF in Linux

[‘

n

Wkhtmltopdf is an open source simple and much effective command-line shell utility that enables user to convert any given HTML (Web Page) to PDF document or an image (jpg, png, etc).

n

Wkhtmltopdf is written in C++ programming language and distributed under GNU/GPL (General Public License). It uses WebKit rendering layout engine to convert HTML pages to PDF document without loosing the quality of the pages. Its is really very useful and trustworthy solution for creating and storing snapshots of web pages in real-time.

n

Wkhtmltopdf Features

n

    n

  1. Open source and cross platform.
  2. n

  3. Convert any HTML web pages to PDF files using WebKit engine.
  4. n

  5. Options to add headers and footers
  6. n

  7. Table of Content (TOC) generation option.
  8. n

  9. Provides batch mode conversions.
  10. n

  11. Support for PHP or Python via bindings to libwkhtmltox.
  12. n

n

In this article we will show you how to install Wkhtmltopdf program under Linux systems using source tarball files.

n

Install Evince (PDF Viewer)

n

Let’s install evince (a PDF reader) program for viewing PDF files in Linux systems.

n

$ sudo yum install evince             [RHEL/CentOS and Fedora]rn$ sudo dnf install evince             [On Fedora 22+ versions]rn$ sudo apt-get install evince         [On Debian/Ubuntu systems]rn

n

Download Wkhtmltopdf Source File

n

Download wkhtmltopdf source files for your Linux architecture using Wget command, or you can also download latest versions (current stable series is 0.12.4) at wkhtmltopdf download page.

n

On 64-bit Linux OS

n

$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xzrn

n

On 32-bit Linux OS

n

$ wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-i386.tar.xz

n

Install Wkhtmltopdf in Linux

n

Extract the files to a current working directory using following tar command.

n

------ On 64-bit Linux OS ------rn$ sudo tar -xvf wkhtmltox-0.12.4_linux-generic-amd64.tar.xz rnrn------ On 32-bit Linux OS ------rn$ sudo tar -xvzf wkhtmltox-0.12.4_linux-generic-i386.tar.xz rn

n

Install the wkhtmltopdf under /usr/bin directory for easy execution of program from any path.

n

$ sudo cp wkhtmltox/bin/wkhtmltopdf /usr/bin/rn

n

How to Use Wkhtmltopdf?

n

Here we will see how to covert remote HTML pages to PDF files, verify information, view created files using evince program from the GNOME Desktop.

n

Convert Website HTML Page to PDF File

n

To convert any website HTML web page to PDF, run the following example command. It will convert the given webpage to 10-Sudo-Configurations.pdf in current working directory.

n

# wkhtmltopdf https://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/ 10-Sudo-Configurations.pdfrn

n

Sample Output :

n

Loading pages (1/6)rnCounting pages (2/6)rnResolving links (4/6)rnLoading headers and footers (5/6)rnPrinting pages (6/6)rnDone

n

View Generated PDF File

n

To verify that the file is created, use the following command.

n

$ file 10-Sudo-Configurations.pdfrn

n

Sample Output :

n

10-Sudo-Configurations.pdf: PDF document, version 1.4

n

View Information of Generated PDF File

n

To view the information of generated file, issue the following command.

n

$ pdfinfo 10-Sudo-Configurations.pdf

n

Sample Output :

n

Title:          10 Useful Sudoers Configurations for Setting 'sudo' in LinuxrnCreator:        wkhtmltopdf 0.12.4rnProducer:       Qt 4.8.7rnCreationDate:   Sat Jan 28 13:02:58 2017rnTagged:         nornUserProperties: nornSuspects:       nornForm:           nonernJavaScript:     nornPages:          13rnEncrypted:      nornPage size:      595 x 842 pts (A4)rnPage rot:       0rnFile size:      697827 bytesrnOptimized:      nornPDF version:    1.4rn

n

View Created PDF File

n

Take a look at the newly created PDF file using evince program from the desktop.

n

$ evince 10-Sudo-Configurations.pdf

n

Sample Screenshot :

n

Looks pretty nice under my Linux Mint 17 box.

n

View Website Page in PDF
View Website Page in PDF

n

Create TOC (Table Of Content) of a Page to PDF

n

To create a table of content for a PDF file, use the option as toc.

n

$ wkhtmltopdf toc https://www.tecmint.com/sudoers-configurations-for-setting-sudo-in-linux/ 10-Sudo-Configurations.pdfrn

n

Sample Output :

n

Loading pages (1/6)rnCounting pages (2/6)rnLoading TOC (3/6)rnResolving links (4/6)rnLoading headers and footers (5/6)rnPrinting pages (6/6)rnDone

n

To check the TOC for the created file, again use evince program.

n

$ evince 10-Sudo-Configurations.pdf

n

Sample Screenshot :

n

Take a look at the picture below. it looks even more better than the above.

n

Create Website Page to Table of Contents in PDF
Create Website Page to Table of Contents in PDF

n

Wkhtmltopdf Options and Usage

n

For Wkhtmltopdf more usage and options, use the following help command. It will display list of all available options that you can use with it.

n

$ wkhtmltopdf --helprn

n

‘]