Jan 2, 2010

How to convert HTML pages to PDF in Ubuntu Linux


A really neat tool I was introduced to a few days back is wkhtmltopdf, a freely distributed tool that allows you to convert HTML pages into PDF documents. This is not something I’ve found a lot of use for too often, but when I’ve needed it it’s been a life saver. Let’s take a look at how to set it up and use it.
The installation of wkhtmltopdf is a lot less difficult that trying to pronounce the name of the project. As the project is not yet distributed as installable binaries you will need to compile it.

Requirements:

- QT 4.4 or above
- CMake 1.6 or above
- Subvertion

On Ubuntu do the following to install the tools required to compile:
# sudo apt-get update
# sudo apt-get install libqt4-dev qt4-dev-tools build-essential cmake
Now check out the latest code for the application from the project’s SVN repositories.
# svn checkout http://wkhtmltopdf.googlecode.com/svn/trunk/ wkhtmltopdf
Time to build and compile it:
# cd wkhtmltopdf
# cmake -D CMAKE_INSTALL_PREFIX=/usr . 
# make
# sudo make install
Once you are done with the previous steps using wkhtmltopdf is a piece of cake. To convert a webpage from HTML to PDF all you need to do is to run the following command:
# wkhtmltopdf http://www.simplehelp.net simplehelp.pdf
The previous command will access the website http://www.simplehelp.net and create a PDF document with its contents. You obviously need a working Internet Connection for this to work. You can also modify the above command slightly to convert an HTML file stored on your computer into a PDF document:
# wkhtmltopdf /home/calvin/sevenacross.html sevenacross.pdf
Pretty neat stuff, eh! wkhtmltopdf has several options allowing you to make the PDF document in Portrait mode or Landscape, and so on. Visit the project’s website for more info on it

No comments:

Post a Comment