PDFKit Python: Generate PDF from HTML Easily

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

What is PDFKit for Python

PDFKit is a powerful Python library that wraps the wkhtmltopdf utility, which uses the WebKit engine to convert HTML documents into PDF files. This library lets developers create professional PDF documents while preserving complex layouts, modern CSS styles, embedded fonts, SVG graphics, and even JavaScript.

The main advantage of PDFKit is that it lets you use familiar web technologies (HTML, CSS) to create documents that can then be converted to PDF with high rendering quality. This makes it an ideal tool for generating reports, invoices, tickets, certificates, and other documents with dynamic content.

PDFKit Library Features

Supported Technologies

  • Modern HTML5 and CSS3
  • Bootstrap, Tailwind CSS, and other CSS frameworks
  • SVG graphics and web fonts
  • Complex table formatting
  • Flexbox and Grid layouts
  • CSS media queries
  • Basic JavaScript support

Data Sources for Conversion

PDFKit supports conversion from three main sources:

  • Web page URLs
  • Local HTML files
  • HTML strings in application memory

Installation and Setup

Installing the Python Library

pip install pdfkit

Installing wkhtmltopdf

Ubuntu/Debian:

sudo apt update
sudo apt install wkhtmltopdf

macOS (via Homebrew):

brew install --cask wkhtmltopdf

Windows: Download the installer from the official site https://wkhtmltopdf.org/downloads.html. Versions are available for Windows Vista and later, both 32-bit and 64-bit.

CentOS/RHEL/Amazon Linux:

# For CentOS 7
sudo yum install wkhtmltopdf

# For newer versions
sudo dnf install wkhtmltopdf

Verifying Installation

After installation, make sure wkhtmltopdf is available on your system:

wkhtmltopdf --version

If the command is not found, you'll need to configure the path to the executable in your Python code.

Core Methods and Functions

Creating PDF from Different Sources

import pdfkit

# From a URL
pdfkit.from_url("https://example.com", "output.pdf")

# From an HTML file
pdfkit.from_file("template.html", "output.pdf")

# From an HTML string
html_content = "<h1>Heading</h1><p>Document content</p>"
pdfkit.from_string(html_content, "output.pdf")

Configuring the Path to wkhtmltopdf

If wkhtmltopdf is not in your system PATH, you can specify the path manually:

config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
pdfkit.from_string(html_content, "output.pdf", configuration=config)

Generating PDF in Memory

For web applications and APIs, you often need to get the PDF as bytes:

# Returns PDF as bytes
pdf_bytes = pdfkit.from_string(html_content, False)

# Can be sent via HTTP response
from flask import Response
return Response(pdf_bytes, mimetype='application/pdf')

Configuring Generation Options

Basic Formatting Options

options = {
    'page-size': 'A4',
    'margin-top': '0.75in',
    'margin-right': '0.75in',
    'margin-bottom': '0.75in',
    'margin-left': '0.75in',
    'encoding': "UTF-8",
    'no-outline':                    

Recommendations