wkhtmltopdf

Convert HTML pages to PDF

TLDR

Convert HTML to PDF
$ wkhtmltopdf [input.html] [output.pdf]
Convert URL to PDF
$ wkhtmltopdf [https://example.com] [output.pdf]
Set page size
$ wkhtmltopdf --page-size [A4] [input.html] [output.pdf]
Set orientation
$ wkhtmltopdf --orientation [Landscape] [input.html] [output.pdf]
Add margins
$ wkhtmltopdf --margin-top [20mm] --margin-bottom [20mm] [input.html] [output.pdf]
Disable JavaScript
$ wkhtmltopdf --disable-javascript [input.html] [output.pdf]
Add header and footer
$ wkhtmltopdf --header-center "[title]" --footer-center "[page]/[topage]" [input.html] [output.pdf]
Multiple pages
$ wkhtmltopdf [page1.html] [page2.html] [output.pdf]

SYNOPSIS

wkhtmltopdf [--page-size size] [--orientation orient] [--margin-* size] [options] input output

DESCRIPTION

wkhtmltopdf converts HTML to PDF using the WebKit rendering engine. It renders pages as a browser would, then outputs to PDF.Multiple input pages combine into a single PDF. Table of contents can be auto-generated from headings. Cover pages and headers/footers frame the content.Headers and footers support variables: [page], [topage], [title], [section], [subsection], [date], [time], [webpage]. These enable automatic page numbers and running headers.Page breaks can be controlled via CSS print media rules. The tool respects page-break-before, page-break-after, and related properties.JavaScript delay handles dynamic content that loads asynchronously. For heavily JS-dependent pages, increasing delay ensures complete rendering.Authentication options handle basic auth, cookies, and custom headers. This enables converting protected pages.

PARAMETERS

--page-size SIZE

Paper size: A4, Letter, Legal, etc.
--orientation ORIENT
Landscape or Portrait.
--margin-top SIZE
Top margin (e.g., 10mm, 1in).
--margin-bottom SIZE
Bottom margin.
--margin-left SIZE
Left margin.
--margin-right SIZE
Right margin.
--dpi DPI
Output resolution.
--zoom FACTOR
Zoom factor.
--grayscale
Generate grayscale PDF.
--disable-javascript
Don't run JavaScript.
--javascript-delay MS
Wait before printing.
--header-html FILE
HTML header file.
--header-center TEXT
Centered header text.
--footer-html FILE
HTML footer file.
--footer-center TEXT
Centered footer text.
--toc
Include table of contents.
--cover FILE
Cover page HTML.
--cookie NAME VALUE
Set cookie.
--username USER
HTTP authentication user.
--password PASS
HTTP authentication password.
--no-images
Don't load images.
--lowquality
Generate lower quality PDF/PS to reduce file size.
--encoding ENCODING
Set the default text encoding.

INSTALL

sudo dnf install wkhtmltopdf
nix profile install nixpkgs#wkhtmltopdf

CAVEATS

Based on old QtWebKit - some modern CSS/JS may not work. Project is no longer maintained. Security concerns with untrusted content. Large pages use significant memory. Some fonts may not embed correctly. Consider WeasyPrint or Chrome headless as alternatives.

HISTORY

wkhtmltopdf was created by Jakob Truelsen around 2008. It provided command-line HTML to PDF conversion before headless browsers existed. Despite being based on an older WebKit version, it became widely used. Development slowed as headless Chrome/Firefox emerged as alternatives.

SEE ALSO

wkhtmltoimage(1), weasyprint(1), chromium(1), pandoc(1), htmldoc(1)