how to use wkhtmltopdf to convert URL to PDF

Sarath Pillai's picture
convert html to pdf

wkhtmltopdf is a superb tool that can be used to convert a given url or local html page, to pdf format. This tool after installing can be embeded inside php code,inside shell script or any other scripting language.

there are several web based online tools available wherein you can specify the URL you need to convert to pdf as input and download the converted pdf file as output.

But those tools cannot be used while working in a console terminal, or is not a viable option when you need that functionality in a script.
wkhtml to pdf uses webkit engine and qt for converting html to pdf. this package is available for downloaded from googlecode.com
We tested this on an ubuntu machine...(works well with Red hat based machines also).

Step1: Installing wkhtmltopdf

Installing wkhtml to pdf can be done through apt-get on ubuntu machines, on other machines, like redhat you can download the tar.gz package from

code.google.com/p/wkhtmltopdf/.

installing wkhtmltopdf

and beleave me it does a handsome job when it comes to converting html to pdf.

I just ran the below code to convert www.google.com to a pdf file format.

$wkhtmltopdf http://www.google.com /home/sarath/Desktop/google.pdf

the command take URL as the first argment and the destination file name and path as the second argument.

converting html to pdf with wkhtml

 

You can get all availabel options in wkhtmltopdf using the below command.

$wkhtmltopdf --help

You can easily create the table of contents available in most of the pdf files withe the help of wkhtmltopdf by the below method.

Some of the common options available are as follows.

 
--collate                                Collate when printing multiple copies
 --copies <number>               Number of copies to print into the pdf
                                             file (default 1)
 --extended-help                    Display more extensive help, detailing
                                             less common command switches
-h, --help Display help -O, --orientation <orientation> Set orientation to Landscape or Portrait -s, --page-size <size> Set paper size to: A4, Letter, etc. --password <password> HTTP Authentication password -p, --proxy <proxy> Use a proxy -q, --quiet Be less verbose --username <username> HTTP Authentication username -V, --version Output version information an exit

 

Rate this article: 
Average: 3 (152 votes)

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.