How to Convert HTML to PDF

A few years ago, generating PDF files via web server side scripts was a real nightmare. Although several libraries were available for the task, most of them were very difficult to use. Times have changed, and now days easiest approach is to convert HTML to PDF.


wkhtmltopdf and is an open source (LGPL) command line tool to render HTML into PDF using the QT Webkit rendering engine. It runs entirely “headless” and does not require a display or display service. To use this in your web application, you would need a server on which you can install wkhtmltopdf.


As open source software, you can install the tool by building it from the source. On some distributions, it is included in the default repositories. Like, for example, to install it in Ubuntu, all you have to do is run two commands:

sudo apt-get update 
sudo apt-get install xvfb libfontconfig wkhtmltopdf


After that, converting web pages is as easy as running:

wkhtmltopdf google.pdf

To use this tool in PHP, you would have to run it using system, or exec, or similar function.

More info:

Note: Similar to wkhtmltopdf, there is wkhtmltoimage which converts HTML into various image format, also using Webkit.

Convert HTML to PDF with PhantomJS

PhantomJS is much more then a screen capturing tool. As the authors state: “PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.” Except for screen capturing, it can be used for headless website testing, page automation and network monitoring.

Way of getting PDF from your HTML using PhantomJS can be found here.


By Aleksandar Jakovljević

My name is Aleksandar Jakovljević and I am a web developer from Belgrade, Serbia. I was born in Smederevo, in 1980. I started focusing on web development since late 2003. I am mostly focused on open source web technologies. I specialised in PHP, focusing on Symfony framework, with skills in Drupal and WordPress, too. I worked more than 7 years for art_net studio and 10 years for Computer Rock (former Spoiled Milk). Now I am working on developing industry leading subscription management system as a part of Plenigo team since August 2021. I love the web and everything related to it. The possibility to publish your ideas quickly and make them available to the entire world is what made me decide to pursue a career in web development.

Follow on: Twitter | GitHub | LinkedIn | Facebook | Instagram

Hire me on Upwork

Leave a comment

Your email address will not be published. Required fields are marked *

The reCAPTCHA verification period has expired. Please reload the page.