In the next article we are going to take a look at the wkhtmltopdf package, which includes the wkhtmltopdf and wkhtmltoimage tools. These are open source (LGPLv3) command line tools with which render HTML to PDF or various image formats using the Qt WebKit rendering engine.
In the following lines we will see how we can convert web pages from html format to pdf format or image, all this in a simple way and with just one command for each case. To get these results, we will use the mentioned command line tools wkhtmltopdf and wkhtmltoimage.
In the case of wkhtmltopdf, it must be said that it is capable of placing multiple objects in the output file. It will allow us to work either with a single page, a cover web page or a table of contents. Objects are placed in the output document in the order they are specified on the command line, options can be specified by object or in the global options area.
Install wkhtmltopdf on Ubuntu 18.04
In order to obtain the latest versions of the tools included in the wkhtmltopdf package, we will need to install the corresponding .deb package, which we will have to download from its website. In the following lines we are going to install wkhtmltopdf and wkhtmltoimage on Ubuntu 18.04 quickly and easily.
To start we will update the list of software available for the operating system. We will do this by typing in a terminal (Ctrl + Alt + T):
sudo apt update
Updated the software list, let's install wget, in case we don't have it installed already:
sudo apt -y install wget
The next step will be visit the launches website, from it we will be able to download its latest stable version published. On this page we will find the latest version of the wkhtmltopdf .deb package. You can also write the following command in the terminal, with which we are going to download the latest stable version today:
wget https://downloads.wkhtmltopdf.org/0.12/0.12.5/wkhtmltox_0.12.5-1.bionic_amd64.deb
Once the download is finished, we can install the newly downloaded package typing the command:
sudo dpkg -i wkhtmltox_0.12.5-1.bionic_amd64.deb
If dependencies fail, as you can see in the previous screenshot, we will be able to correct this by typing in the same terminal:
sudo apt -f install
How to use wkhtmltopdf in Ubuntu
If we are interested in converting a web page to PDF format, we will only have to use the following syntax in terminal to convert:
wkhtmltopdf [página-web] [archivo.pdf]
An example of use to use this command would be the following:
wkhtmltopdf https://es.wikipedia.org/wiki/Bash bash.pdf
After the conversion, you can see the results. A pdf file will have been created in the directory from which we execute the command, which will allow us to see its content, as we would with any pdf.
file bash.pdf
How to use wkhtmltoimage in Ubuntu
In case we are interested convert web page to image, we can use the same syntax that we used with the previous command:
wkhtmltoimage https://es.wikipedia.org/wiki/Bash bash.png
Help
It can be obtained help on wkhtmltopdf and wkhtmltoimage by typing the following commands in the terminal. To get help about the first command you will have to use:
wkhtmltopdf -h
In case you need help on the second command, we can also add the same option:
wkhtmltoimage -h
Another way to get help will be use man pages corresponding.
We just see how we can convert web pages from HTML format of interest to PDF and Image using the wkhtmltopdf and wkhtmltoimage tools. The use that users can make of these tools is varied, from using them to generate invoices, save notes, create birthday cards or any other type of thing that comes to mind. You just have to use your imagination.
The source code of this application is available in the repository of GitHub of the application and you can know more about her in the project website. In it you can find documentation about these tools.