Today we are going to learn that how we can convert our web pages from html format to pdf or image format. For obtaining these results we’ll use the most famous command line tools wkhtmltopdf & wkhtmltoimage. They are Open Source Command line tools used for conversion processes from html to pdf and image respectively.
They both use the QT WebKit rendering engine to perform the actions.
By default, the versions available on Ubuntu are old for these tools. So, we have to Install them manually. Follow the guide to get started that how we can Install wkhtmltopdf & wkhtmltoimage on Ubuntu 19.04 Linux.
First of all we’ll download the tools from .deb package. Further these tools do not require a display so they are entirely headless.
Install wkhtmltopdf on Ubuntu 19.04 / Debian 10
Visit the below link to Download the latest precompiled binary from the release page.
Download latest version of wkhtmltopdf
Step 1: Install wkhtmltopdf on Ubuntu 19.04
sudo apt update
sudo apt -y install wget
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb
sudo dpkg -i wkhtmltox_0.12.5-1.bionic_amd64.deb
sudo apt -f install
If you get error for the fonts try the following command
sudo apt-get update -y
sudo apt-get install -y xfonts-75dpi
You can see the following results upon successful Installation.
(Reading database … 172496 files and directories currently installed.)
Preparing to unpack wkhtmltox_0.12.5-1.bionic_amd64.deb …
Unpacking wkhtmltox (1:0.12.5-1.bionic) over (1:0.12.5-1.bionic) …
Setting up wkhtmltox (1:0.12.5-1.bionic) …
Processing triggers for man-db (2.8.3-2ubuntu0.1) …
Step 2: Install wkhtmltopdf on Debian 10 / Debian 9
To Install on Debian 9 type the following command
sudo apt update
sudo apt -y install wget
wget https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.5/wkhtmltox_0.12.5-1.stretch_amd64.deb
sudo dpkg -i wkhtmltox_0.12.5-1.stretch_amd64.deb
sudo apt -f install
Make sure to check the Installed version to verify your Installation.
$ wkhtmltopdf --version
wkhtmltopdf 0.12.5 (with patched qt)
$ wkhtmltoimage --version
wkhtmltoimage 0.12.5 (with patched qt)
Step 3: How to Use wkhtmltopdf on Ubuntu / Debian
Run the given command to convert a web page to PDF format.
$ wkhtmltopdf <webpage> <destination-pdf-file>
Look at the example:
wkhtmltopdf https://osradar.com osradar.pdf
Execution Output:
As you can see the results, a pdf file is being created and you can now view the contents.
$ file osradar.pdf
You can convert to image using the same syntax as:
$ wkhtmltoimage https://osradar.com osradar.png
Congratulations! You’ve learned how we can convert our web pages to pdf and Image format using wkhtmltopdf & wkhtmltoimage tool.
If you’ve any queries regarding this tutorial, feel free to ask!