How to Convert HTML to PDF Using wkhtmltopdf and Python
In this tutorial, you’ll learn how to convert HTML into PDF using wkhtmltopdf, an open source command-line tool that converts HTML to PDF using the Qt WebKit rendering engine. The current version of wkhtmltopdf is 0.12.6, which was released in 2020. It’s available for macOS, Linux, and Windows.
Common use cases for converting HTML to PDF include generating invoices or receipts for sales, printing shipping labels, converting resumes to PDF, and much more.
This tutorial will use Python-PDFKit to convert HTML to PDF, and pdfkit, a simple Python wrapper that allows you to convert HTML to PDF using the wkhtmltopdf utility.
Installing wkhtmltopdf
Before you can use wkhtmltopdf, you need to install it on your operating system.
On macOS
Install wkhtmltopdf using Homebrew:
brew install --cask wkhtmltopdf
On Debian/Ubuntu
Install wkhtmltopdf using APT:
sudo apt-get install wkhtmltopdf
On Windows
Download the latest version of wkhtmltopdf from the wkhtmltopdf website.
After you’ve downloaded the installer, set the path to the wkhtmltopdf binary to your PATH
environment variable.
Installing Python-PDFKit
Install Python-PDFKit using Pip:
pip install pdfkit # or pip3 install pdfkit # for Python 3
Python-PDFKit provides several APIs to create a PDF document:
-
From a URL using
from_url
-
From a string using
from_string
-
From a file using
from_file
Creating a PDF from a URL
The from_url
method takes two arguments: the URL, and the output path. The following code snippet shows how to convert the Google home page to PDF using pdfkit:
import pdfkit pdfkit.from_url('https://google.com', 'example.pdf')
Place the code snippet in a file named url.py
and run it:
python url.py
# If you're using Python 3, run the following command:
python3 url.py
The output PDF will be saved in the current directory as example.pdf
.
Creating a PDF from a String
The from_string
method takes two arguments: the HTML string, and the output path. The following code snippet shows how to do this:
import pdfkit pdfkit.from_string('<h1>Hello World!</h1>', 'out.pdf')
Creating a PDF from a File
The from_file
method takes two arguments: the path to the HTML file, and the output path. The following code snippet shows how to do this:
import pdfkit pdfkit.from_file('index.html', 'index.pdf')
You’ll use an invoice template for the HTML file. You can download the template from here. The following image shows the invoice template.
It’s also possible to pass some additional parameters — like the page size, orientation, and margins. Add the options
parameter to do this:
options = { 'page-size': 'Letter', 'orientation': 'Landscape', 'margin-top': '0.75in', 'margin-right': '0.75in', 'margin-bottom': '0.75in', 'margin-left': '0.75in', 'encoding': "UTF-8", 'custom-header': [ ('Accept-Encoding', 'gzip') ], 'no-outline': None } pdfkit.from_file('index.html', 'index.pdf', options=options)
Conclusion
In this tutorial, you saw how to generate PDFs from HTML using wkhtmltopdf. If you’re looking to add more robust PDF capabilities, PSPDFKit offers a commercial JavaScript PDF library that can easily be integrated into your web application. It comes with 30+ features that let you view, annotate, edit, and sign documents directly in your browser. Out of the box, it has a polished and flexible UI that you can extend or simplify based on your unique use case.
You can also deploy our vanilla JavaScript PDF viewer or use one of our many web framework deployment options like React.js, Angular, and Vue.js. To see a list of all web frameworks, start your free trial. Or, launch our demo to see our viewer in action.