Do you need a simple open source cross-platform command line tool that converts web pages and HTML to a PDF file? Look no further, try wkhtmltopdf.

It is a 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. Let us see how to install wkhtmltopdf in Linux or Unix and try to convert HTML page to a pdf file.

Advertisement

wkhtmltopdf Software features

Use the wkhtmltopdf to convert HTML page to a PDF format under Linux / macOS / Windows. The features are as follows:

  1. Cross platform.
  2. Open source.
  3. Convert any web pages into PDF documents using webkit.
  4. You can add headers and footers.
  5. TOC generation.
  6. Batch mode conversions.
  7. Can run on Linux server with an XServer (the X11 client libs must be installed).
  8. Can be directly used by PHP or Python via bindings to libwkhtmltox.

A note about Debian / Ubuntu Linux user

You can install wkhtmltopdf using the apt or apt-get command command:
$ sudo apt-get install wkhtmltopdf
$ sudo ln -s /usr/bin/wkhtmltopdf /usr/local/bin/html2pdf

Sample outputs:

[sudo] password for vivek: 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following NEW packages will be installed:
  wkhtmltopdf
0 upgraded, 1 newly installed, 0 to remove and 10 not upgraded.
Need to get 116 kB of archives.
After this operation, 303 kB of additional disk space will be used.
Get:1 http://debian.osuosl.org/debian/ squeeze/main wkhtmltopdf amd64 0.9.9-1 [116 kB]
Fetched 116 kB in 2s (49.4 kB/s)                      
Selecting previously deselected package wkhtmltopdf.
(Reading database ... 274164 files and directories currently installed.)
Unpacking wkhtmltopdf (from .../wkhtmltopdf_0.9.9-1_amd64.deb) ...
Processing triggers for man-db ...
Setting up wkhtmltopdf (0.9.9-1) ...

Fedora Linux install wkhtmltopdf

Use the dnf command as follows:
$ sudo dnf install wkhtmltopdf

Install wkhtmltopdf on MacOS unix

First, install Homebrew on macOS and then type the following brew command:
$ brew install wkhtmltopdf
OR
$ brew cask install wkhtmltopdf

Download wkhtmltopdf To Convert HTML Page To a PDF Using Open Source Tool

Visit this page to grab wkhtmltopdf for Linux / MS-Windows / Apple Mac OS X. You can also use the wget command as follows:
$ wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
Sample outputs:

Resolving wkhtmltopdf.googlecode.com... 74.125.135.82, 2404:6800:4001:c01::52
Connecting to wkhtmltopdf.googlecode.com|74.125.135.82|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 11175276 (11M) [application/octet-stream]
Saving to: `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2'
 
100%[======================================>] 1,11,75,276  480K/s   in 23s     
 
2012-10-04 01:21:43 (477 KB/s) - `wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2' saved [11175276/11175276]

Install wkhtmltopdf under Linux

Type the following tar command to extract files:
$ tar xvf wkhtmltopdf-0.11.0_rc1-static-amd64.tar.bz2
Sample outputs:

wkhtmltopdf-amd64

Install the same in your private ~/bin/ directory or in /usr/local/bin directory:
$ mv wkhtmltopdf-amd64 ~/bin/
$ ln -s ~/bin/wkhtmltopdf-amd64 ~/bin/html2pdf

OR
$ sudo mv wkhtmltopdf-amd64 /usr/local/bin/
ln -s /usr/local/bin/wkhtmltopdf-amd64 /usr/local/bin/html2pdf

How do I use wkhtmltopdf?

The syntax is as follows:

html2pdf http://www.cyberciti.biz/path/to/url.html output.pdf
html2pdf http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
html2pdf -option1  -option2 http://www.cyberciti.biz/blog/print/url-slut.html output.pdf

OR

wkhtmltopdf http://www.cyberciti.biz/path/to/url.html output.pdf
wkhtmltopdf http://www.cyberciti.biz/blog/print/url-slut.html output.pdf
wkhtmltopdf -option1  -option2 http://www.cyberciti.biz/blog/print/url-slut.html output.pdf

Example: Simple html to pdf file

In this example, convert out bash for loop page to a pdf file:
$ html2pdf http://www.cyberciti.biz/faq/bash-for-loop/print/ /tmp/bash.for.loop.pdf
Sample outputs:

Loading pages (1/6)
Counting pages (2/6)                                               
Resolving links (4/6)                                                       
Loading headers and footers (5/6)                                           
Printing pages (6/6)
Done

To view generated pdf file click here. Verify pdf file, enter:
$ file /tmp/bash.for.loop.pdf
Sample outputs:

/tmp/bash.for.loop.pdf: PDF document, version 1.4

Use the pdfinfo command to prints the contents of the ‘Info’ dictionary (plus some other useful information) from a Portable Document Format (PDF) file:
$ pdfinfo /tmp/bash.for.loop.pdf
Sample outputs:

Title:          Frequently Asked Questions About Linux / UNIX ยป Bash For Loop Examples ยป Print
Creator:        
Producer:       wkhtmltopdf
CreationDate:   Thu Oct  4 01:29:33 2012
Tagged:         no
Pages:          4
Encrypted:      no
Page size:      595 x 842 pts (A4)
File size:      98792 bytes
Optimized:      no
PDF version:    1.4

Grayscale pdf

The following PDF will be generated in grayscale:
$ html2pdf -g http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf

Set orientation to Landscape or Portrait

Use the following syntax:
$ html2pdf -O Landscape http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Where,

  • -O Landscape|Portrait. The default is Portrait.

How do I set page size?

Use the following syntax:
$ html2pdf -S SIZE http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Where,

  • -s Size : Set paper size to: A4, Letter, etc. (default A4)

How do I generate table of content?

A table of content can be added to the document by adding a toc the command line option. For example:
$ html2pdf toc http://www.cyberciti.biz/faq/bash-for-loop/print/ bash.for.loop.pdf
Sample outputs:

Linux / Unix HTML to PDF File Command Line Option

Fig.01: wkhtmltopdf in action

Please note that the table of content is generated based on the H tags in the input documents.

How do I see all available options?

You can see a list of commonly used options, enter:
$ wkhtmltopdf --help
OR see all available options i.e. display more extensive help, detailing less common command switches, run:
$ wkhtmltopdf -H | less

References:

๐Ÿฅบ Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! ๐Ÿค 
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

20 comments… add one
  • Alex Ken Oct 3, 2012 @ 23:58

    Thank you, very useful.

  • nanchor Oct 4, 2012 @ 2:40

    I use ‘Print to file’ mode in Linux and it can save a web page as pdf. Maybe wkhtmltopdf output has better quality?

  • Rahul Bansal Oct 4, 2012 @ 5:37

    Really nice. I think combining with ghostscript, we can use it for “webpages to screenshot” like use-cases.

  • PNA Prasanna Oct 4, 2012 @ 5:47

    Good stuff.

  • Sujeet Gupta Oct 4, 2012 @ 5:54

    Its throwing an error.
    How to solve it?

     html2pdf http://www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/ output.pdf
    Loading pages (1/6)
    Counting pages (2/6)                                               
    QPixmap: Cannot create a QPixmap when no GUI is being used===] Object 1 of 1
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    Resolving links (4/6)                                                       
    Loading headers and footers (5/6)                                           
    Printing pages (6/6)
    QPixmap: Cannot create a QPixmap when no GUI is being used   ] Page 1 of 5
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    QPixmap: Cannot create a QPixmap when no GUI is being used
    Segmentation fault
    
    • ๐Ÿ›ก๏ธ Vivek Gite (Author and Admin) Vivek Gite Oct 4, 2012 @ 7:44

      I tested this with wkhtmltopdf v0.11.0 rc1. I think this will get fixed in next release. I suggest you use stable version (amd64):

      wget http://wkhtmltopdf.googlecode.com/files/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
      tar wkhtmltopdf-0.9.9-static-amd64.tar.bz2
      ./wkhtmltopdf-amd64 www.cyberciti.biz/open-source/html-to-pdf-freeware-linux-osx-windows-software/ output.pdf
      Loading pages (1/5)
      Resolving links (2/5)                                              
      Counting pages (3/5)                                                      
      Printing pages (5/5)                                                      
      Done                       
      
      • Nikesh May 24, 2013 @ 12:32

        I installed wkhtmltopdf in my local computer in which GUI is installed. It’s working properly.

        Next I installed the same in a Dedicated server in Remote Location, which is not having GUI interface..

        I’m getting the below error message while I try to run wkhtml2pdf. and It’s creating an empty pdf file with 0KB. Do you know how to fix this error ?

        Printing pages (6/6)
        QPixmap: Cannot create a QPixmap when no GUI is being used===] Page 1 of 1
        QPixmap: Cannot create a QPixmap when no GUI is being used
        QPixmap: Cannot create a QPixmap when no GUI is being used
        QPixmap: Cannot create a QPixmap when no GUI is being used
        QPixmap: Cannot create a QPixmap when no GUI is being used

  • slezhuk Oct 4, 2012 @ 5:54

    Great program, thanks! In ubuntu 12.04 wkhtmltopdf is in repository, so you need only “sudo apt-get install wkhtmltopdf”

    • ๐Ÿ›ก๏ธ Vivek Gite (Author and Admin) Vivek Gite Oct 4, 2012 @ 7:49

      The post has been updated.

      Appreciate your feedback.

  • battila.hu Oct 4, 2012 @ 8:04

    Hey,

    I use htmldoc:

    Package: htmldoc
    Status: install ok installed
    Priority: optional
    Section: web
    Installed-Size: 512
    Maintainer: Ubuntu Developers 
    Architecture: amd64
    Version: 1.8.27-4.1ubuntu2
    Depends: libc6 (>= 2.7), libfltk1.1 (>= 1.1.6), libgcc1 (>= 1:4.1.1), libjpeg62 (>= 6b1), libpng12-0 (>= 1.2.13-4), libssl1.0.0 (>= 1.0.0), libx11-6, zlib1g (>= 1:1.1.4), htmldoc-common
    Description: HTML processor that generates indexed HTML, PS, and PDF
     HTMLDOC is a program for writing documentation in HTML and producing
     indexed HTML, PostScript, or PDF output (with tables of contents).
     It supports most HTML 3.2 and some HTML 4.0 syntax, as well as GIF,
     JPEG, and PNG images.
    Homepage: http://www.htmldoc.org/
    Original-Maintainer: Jeff Licquia 
    
  • Ahmad Oct 4, 2012 @ 9:37

    Thanks… Now can i convert a normal text file (non html) to PDF using this program? I tried and it threw an error.

  • deoren Oct 8, 2012 @ 21:01

    Great tutorial. I’ll definitely refer to this again. :)

  • meow Dec 6, 2012 @ 11:44

    html to pdf is but not big deal, otherwise, pdf2htmlex as the name self-explained which is a truly magnificent tool that should be noticed.

  • Rakhi Dhavale Apr 28, 2013 @ 10:02

    thanks!!! Great very useful!!

  • Sergio Jun 19, 2014 @ 19:09

    Thanks… Excelent program, but it throw error: wkhtmltopdf: cannot connect to X server. Is posible to make it to use only in therminal

  • Alexey Ratnikov Jan 29, 2015 @ 9:05

    Error on Debian 6.0: QPixmap: Cannot create a QPixmap when no GUI is being used

  • stippi Oct 16, 2023 @ 6:27

    Fedora 38 also has it in it’s repos:
    dnf install wkhtmltopdf
    Cheers

  • Michael Uplawski Dec 24, 2023 @ 11:37

    To allow the conversion of mostly unusable HTML-mail to PDF, it would be cool to *pipe-in* mails or -components to wkhtmltopdf.
    Unfortunately it is only the argument-list which is currently expected on STDIN.

    Maybe you could do a quick change for ARGF ?

    Thank you in advance.

Leave a Reply

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

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.