Install wkhtmltopdf on ubuntu 12.04

Install wkhtmltopdf on ubuntu 12.04


Simple shell utility to convert html to pdf using the webkit rendering engine, and qt.

Searching the web, I have found several command line tools that allow you to convert a HTML-document to a PDF-document, however they all seem to use their own, and rather incomplete rendering engine, resulting in poor quality. Recently QT 4.4 was released with a WebKit widget (WebKit is the engine of Apples Safari, which is a fork of the KDE KHtml), and making a good tool became very easy.


-Convert web pages into PDF documents using webkit
-Adding headers and footers (static version only)
-TOC generation (static version only)
-Batch mode conversions
-(Linux) No longer requires an XServer to be running (however the X11 client libs must be installed)

Install static binary :

First check os is 32 bit or 64 bit by using following command

Try uname -m. It seems like the uname -m actually gives x86_64 when it is an kernel 64 bits

Run following command

#sudo apt-get install openssl build-essential xorg libssl-dev libxrender-dev

Then run following command

#sudo apt-get install wkhtmltopdf


Based on OS download wkhtmltopdf package from following site



#wget /

Then extract using command

#tar -xvjf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2

Then move extracted DIR to /usr/bin folder

#sudo mv wkhtmltopdf-amd64 /usr/bin/wkhtmltopdf

#chown root:root /usr/bin/wkhtmltopdf
#chmod +x /usr/bin/wkhtmltopdf

Check wkhtmltopdf is install or not using following command

#which wkhtmltopdf


#wkhtmltopdf / google.pdf

if got following error,

Segmentation fault (core dumped)

then solution are :

1) try wkhtmltopdf –disable-javascript / google.pdf (Sometime works)
2) try wkhtmltopdf –use-xserver / google.pdf (this works for all)

if got following error,

cannot connect to X server

We need to install X virtual server.

then solution are :

#sudo apt-get install xvfb
#xvfb-run --server-args="-screen 0, 1024x680x24" wkhtmltopdf --use-xserver file1.html file2.pdf


<?php exec("startx -- `which Xvfb` :1 -screen 0 800x600x24 && DISPLAY=:1 && wkhtmltopdf --use-xserver test.html output.pdf"); ?>


Sometimes, it can happen that the static binary does not work for your system for one reason or the other, in that case you might need to compile qtwkhtmltopdf yourself.

Go for compilation :

1)Installing dependencies

Before you install you will need to have X11, gcc and openssl installed. On Debian/Ubuntu this can be done as follows:

#sudo aptitude install openssl build-essential xorg libssl-dev libxrender-dev

on other systems you must use your own package manager, the packages might be named differently.

2)Getting the source

Now you have two option either download the latest release source, or make a bleeding edge build from git

-Stable release build

Download and extract the code by running

#wget /

#tar -xvf wkhtmltopdf-0.8.3.tar.bz2

#ls -s wkhtmltopdf-0.8.3 wkhtmltopdf

Be sure to change the version number to the latest

-git build

First you need to have git installed on Debian/Ubuntu:

#sudo aptitude install git-core

Then download the source

#git clone git:/ wkhtmltopdf

3)Installing QT

Before you can compile wkhtmltopdf you need to have QT installed, again you have two options: You can install the QT from your distribution, this will be fast but your wkhtmltopdf build will not have all the features of the static build, or you can compile QT your self, this takes a long time but you will get all the features.

-Installing QT from your distribution

On ubuntu this is done by:

#aptitude install libqt4-dev qt4-dev-tools

On your distribution the packages might be named something else
You must now run qmake in the wkhtmltopdf folder.

#cd wkhtmltopdf


-Installing QT yourself

First check out the source

#git clone git:/ wkhtmltopdf-qt
#cd wkhtmltopdf-qt

Qt must be configured, a good set of configuration options for qt can found by looking at the output of

#cat ../wkhtmltopdf/static_qt_conf_base ../wkhtmltopdf/static_qt_conf_linux | sed -re 's/#.*/'

#./configure -nomake tools,examples,demos,docs,translations -opensource -prefix "../wkqt"

And Qt compiled and installed (this will take a while)

#make -j3 && make install
#cd ..

You must now run qmake in the wkhtmltopdf folder.

#cd wkhtmltopdf

4)Compiling and installing wkhtmltopdf Now all you need to do is compile and install wkhtmltopdf

#make && make install

you are done.

Some Useful links,



PHP binding,


function generate_pdf($html,$filename = 'download.pdf',$exec = false,$download = false)
if($exec === true){
exec('xvfb-run --server-args="-screen 0, 1024x680x24" wkhtmltopdf --use-xserver '.$html.' '.$filename);
$pdf = file_get_contents($filename);
} else {
$descriptorspec = array(
0 => array('pipe', 'r'), / stdin
1 => array('pipe', 'w'), / stdout
2 => array('pipe', 'w'), / stderr

$process = proc_open('xvfb-run --server-args="-screen 0, 1024x680x24" wkhtmltopdf --use-xserver '.$html.' -', $descriptorspec, $pipes);

$pdf = stream_get_contents($pipes[1]);
$errors = stream_get_contents($pipes[2]);

$return_value = proc_close($process);
if ($errors) die('PDF GENERATOR ERROR:<br />' . nl2br(htmlspecialchars($errors)));


header('Cache-Control: public, must-revalidate, max-age=0'); / HTTP/1.1
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT'); / Date in the past
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header('Content-Length: '.strlen($pdf));

if($download === true){
header('Content-Description: File Transfer');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
header('Content-Type: application/pdf', false);
header('Content-Disposition: attachment; filename="'.basename($filename).'";');
header('Content-Transfer-Encoding: binary');
} else {
header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="'.basename($filename).'";');
echo $pdf;

$html = "/";
$filename = 'google.pdf';

generate_pdf($html,$filename,$exec = true,$download = false);


More resources,





This entry was posted in didodecahedral and tagged . Bookmark the (212) 815-0504.

Leave a Reply