2.19.1.1.1. wkhtmltopdf and wkhtmltoimage utilities

wkhtmltopdf is a set of two command-line utilities for rendering HTML files or web pages using WebKit engine:

  • The wkhtmltopdf utility renders to PDF and exists in two variants:
    • Version 0.10:
      /usr/local/bin/wkhtmltopdf-amd64
    • Version 0.12:
      /usr/local/bin/wkhtmltopdf
  • The wkhtmltoimage utility renders to image file:
    /usr/local/bin/wkhtmltoimage

Utilities are available on hosting by default. In examples below for wkhtmltopdf utility version 0.12 is used. To use version 0.10, just replace utility name wkhtmltopdf with wkhtmltopdf-amd64 in command.

View short documentation:

  • wkhtmltopdf:
    wkhtmltopdf -h
  • wkhtmltoimage:
    wkhtmltoimage -h

View detailed documentation:

  • wkhtmltopdf:
    wkhtmltopdf -H
  • wkhtmltoimage:
    wkhtmltoimage -H

Launch format:

  • wkhtmltopdf:
    wkhtmltopdf <URL or path to source HTML file> <path and name of output PDF file>
  • wkhtmltoimage:
    wkhtmltoimage <URL or path to source HTML file> <path and name of output image file>

Simple example of web page transformation:

  • In PDF document:
    wkhtmltopdf http://google.com google.pdf

    Or:

    /usr/local/bin/wkhtmltopdf http://google.com google.pdf
  • In image file:
    wkhtmltoimage http://google.com google.png

    Or:

    /usr/local/bin/wkhtmltoimage http://google.com google.png

Simple example of HTML document transformation:

  • In PDF document:
    wkhtmltopdf /home/example/path/to/file/document.html document.pdf

    Or:

    /usr/local/bin/wkhtmltopdf /home/example/path/to/file/document.html document.pdf

    Where example is hosting account name.

  • In image file:
    wkhtmltoimage /home/example/path/to/file/document.html document.png

    Or:

    /usr/local/bin/wkhtmltoimage /home/example/path/to/file/document.html document.png

    Where example is hosting account name.

When generating a PDF document, you can add a header and footer from separate HTML files:

wkhtmltopdf --header-html <path to HTML header> --footer-html <path to HTML footer> <path to source HTML file> <path and name of output PDF file>

If necessary, you can set margins for PDF documents:

wkhtmltopdf --margin-top 25mm --margin-bottom 25 mm --margin-left 15mm --margin-right 15mm --header-html <path to HTML header> --footer-html <path to HTML footer> <path to source HTML file> <path and name of output PDF file>

Examples of other useful options for wkhtmltopdf:

  • –encoding windows-1251 — explicit specification of source HTML file encoding.
  • –page-size A4 — output PDF file page format.
  • –orientation Landscape — page orientation of output PDF file.

You can use utilities from PHP with the exec function.

Content