Thomas' Tech Tips

Convert HTML files to PDF

20 March 2022 - Thomas Damgaard

wkhtmltopdf is a neat command line utility that will convert HTML to PDF or image using WebKit.

To convert an HTML file to PDF, run:

wkhtmltopdf sample.html sample.pdf

To batch convert a bunch of HTML files to PDF files with corresponding names, run:

for htmlfile in *.html; do
    wkhtmltopdf "$htmlfile" "$(basename "$htmlfile" .html).pdf"
done

This will produce example.pdf as a PDF version of example.html.

In Debian GNU/Linux, wkhtmltopdf is available from the official mirrors and can thus be installed by running

sudo apt install wkhtmltopdf
Filed under: debian, linux, pdf, shell, tips

Back to article list