2.20.1.1.2. wkhtmltopdf and wkhtmltoimage utilities

wkhtmltopdf — a set of two utilities command line to render HTML files or web pages using the WebKit engine:

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

Utilities available on hosting default. The examples below use version 0.12 for the wkhtmltopdf utility. To use version 0.10, just replace the utility name with wkhtmltopdf on wkhtmltopdf-amd64.

View quick documentation:

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

View detailed documentation:

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

Launch format:

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

A simple example of converting a web page:

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

    Or:

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

    Or:

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

A simple example of converting an HTML document:

  • To PDF document:
    wkhtmltopdf /home/example/way/to/file/document.html document.pdf

    Or:

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

    Where example thisis hosting account name.

  • To the image file:
    wkhtmltoimage /home/example/way/to/file/document.html document.png

    Or:

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

    Where example thisis 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 indents 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 HTML source file> <path and name of output PDF file>

Examples of other useful options for wkhtmltopdf:

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

You can use utilities from PHP using the function exec.

Content