Convert website page to pdf and image

To convert website page to pdf and image is very useful for various reasons. This kind of requirement you may have seen while making any report and we need to convert website page to pdf or image.

Recently, we got requirement to create website page to pdf for some report. Actually the report was displaying on web page only and through script we want to automate that web report should be sent via email as a attached pdf file. To accomplish this we have use the utility called wkhtmltopdf and wkhtmltoimage.

wkhtmltopdf and wkhtmltoimage are open source (LGPLv3) command line tools to render HTML into PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require a display or display service. (Reference taken from official website)

Install the wkhtmltopdf and wkhtmltoimage on CentOS 7 / Ubuntu 14.04 LTS

On CentOS 7.x/ RHEL 7.x, use the below given command to install. On RHEL 7.x and CentOS 7.x, first install epel repo.

yum install epel-release
yum install wkhmtltopdf

On Ubuntu 14.04 LTS, use below given command as per your Operating System architecture.

For 32 bit Ubuntu 14.04 LTS system,

wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/wkhtmltox-0.12.2.1_linux-precise-i386.deb
sudo dpkg -i wkhtmltox-0.12.2.1_linux-precise-i386.deb

For 64 bit Ubuntu 14.04 LTS system,

wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/wkhtmltox-0.12.2.1_linux-precise-amd64.deb
sudo dpkg -i wkhtmltox-0.12.2.1_linux-precise-amd64.deb

You can also explore the Download page of wkhtmltopdf for your Operating System.

How to use wkhtmltopdf and wkhtmltoimage

We are sharing some of the example here which is generally used. Please do note, replace the example URL, username, password, output file name (eg. test.pdf,test.png)and other parameter values as per your scenario cases and requirement.

Example 1: Without using any extra parameter we are using here wkhtmltopdf and wkhtmltoimage command.

For PDF file :

wkhtmltopdf https://sharadchhetri.com /tmp/test.pdf

For image file:

wkhtmltoimage https://sharadchhetri.com /tmp/test.png

Example 2: In case the URL has HTTP authentication then you can provide username and password in command line.

For PDF file :

wkhtmltopdf --username GiveYourUserName --password GiveYourPasword https://sharadchhetri.com /tmp/test.pdf

For image file:

wkhtmltoimage --username GiveYourUserName --password GiveYourPasword https://sharadchhetri.com /tmp/test.png

Example 3: Adding Table Of Content in pdf

wkhtmltopdf toc https://sharadchhetri.com/ sharad.pdf

Given below is example output of ‘Table Of Content’ –
sharad.pdf

You can explore and use multiple options available with wkhtmltopdf and wkhtmltoimage , to read more visit http://wkhtmltopdf.org/usage/wkhtmltopdf.txt