wkhtmltopdf – Generating PDF and Images

wkhtmltopdf is amazing  open source (LGPLv3) command line tool wkhtmltopdf to render HTML to PDF and various image formats using the Qt WebKit rendering engine. These run entirely “headless” and do not require any service.

There is also a C library, if you’re into that kind of thing.

How to use wkhtmltopdf ?

  1. Download a precompiled binary or build from source
  2. Create your HTML document that you want to convert into a PDF (or image)
  3. Run your HTML document through the tool.
    For example, if I really like the treatment LiferayUI has done to their logo today and want to capture it forever as a PDF:wkhtmltopdf https://liferayui.com liferayui.pdf
Global Options:
      --collate                       Collate when printing multiple copies
                                      (default)
      --no-collate                    Do not collate when printing multiple
                                      copies
      --cookie-jar <path>             Read and write cookies from and to the
                                      supplied cookie jar file
      --copies <number>               Number of copies to print into the pdf
                                      file (default 1)
  -d, --dpi <dpi>                     Change the dpi explicitly (this has no
                                      effect on X11 based systems)
  -H, --extended-help                 Display more extensive help, detailing
                                      less common command switches
  -g, --grayscale                     PDF will be generated in grayscale
  -h, --help                          Display help
      --htmldoc                       Output program html help
      --image-dpi <integer>           When embedding images scale them down to
                                      this dpi (default 600)
      --image-quality <integer>       When jpeg compressing images use this
                                      quality (default 94)
      --license                       Output license information and exit
  -l, --lowquality                    Generates lower quality pdf/ps. Useful to
                                      shrink the result document space
      --manpage                       Output program man page
  -B, --margin-bottom <unitreal>      Set the page bottom margin
  -L, --margin-left <unitreal>        Set the page left margin (default 10mm)
  -R, --margin-right <unitreal>       Set the page right margin (default 10mm)
  -T, --margin-top <unitreal>         Set the page top margin
  -O, --orientation <orientation>     Set orientation to Landscape or Portrait
                                      (default Portrait)
      --page-height <unitreal>        Page height
  -s, --page-size <Size>              Set paper size to: A4, Letter, etc.
                                      (default A4)
      --page-width <unitreal>         Page width
      --no-pdf-compression            Do not use lossless compression on pdf
                                      objects
  -q, --quiet                         Be less verbose
      --read-args-from-stdin          Read command line arguments from stdin
      --readme                        Output program readme
      --title <text>                  The title of the generated pdf file (The
                                      title of the first document is used if not
                                      specified)
  -V, --version                       Output version information and exit

Additional options

That’s amazing, I’ve always wanted to turn LiferayUI homepage into a PDF, but I want a table of contents as well.

There are lot’s of command line options. You can check out the auto-generated wkhtmltopdf options.

You can also add custom Header and Footer in PDF using page option.

wkhtmltopdf --header-html header.html https://liferayui.com liferayui.pdf

Leave a Reply