Wednesday, July 18, 2012

Convert HTML to PDF using WKHTMLTOPDF


I was looking for solution to generate the PDF from dynamic HTML in last week. I was looking for easy way to generate the PDF without being affected with syntax and design.

While goggling I found the tool: WKHTMLTOPDF  

This tool is converting HTML (CSS support) to PDF the same way you save a PDF from browser.

Here are the steps to install and integrate the tool.

First, install the webkit library on your server.

$ wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.8.3-static.tar.bz2
$ tar -jxvf wkhtmltopdf-0.8.3-static.tar.bz2
$ sudo aptitude install ia32-libs

Make symbolic link to tool in /usr/local/bin

$ sudo ln -s /full_path/WKHTMLTOPDF /usr/local/bin/WKHTMLTOPDF

Now we are ready to useJ.

Now try the following command to convert the google page into PDF:

$ wkhtmltopdf http://www.google.com example1.pdf

There are many options where you can generate the different size/types of PDF like A4, A2 etc

Here are few examples:

$ wkhtmltopdf http://www.google.com examplea1.pdf -s A4

$ wkhtmltopdf http://www.google.com examplea2.pdf -s A2

No comments: