Last modified: Feb 07, 2026 By Alexander Williams
Python HTML to PDF Conversion Guide
Converting HTML to PDF is a common task. You need it for reports, invoices, or archiving web pages. Python offers great tools for this job.
This guide shows you how to do it. We will cover popular libraries and best practices. You will learn to create professional PDFs from your HTML content.
Why Convert HTML to PDF with Python?
PDFs are perfect for sharing. They keep their format on any device. HTML is great for creating content on the web.
Combining them is powerful. You can design in HTML and share as PDF. Python automates this process easily.
Use cases are everywhere. Generate invoices from templates. Create reports from data. Save web pages for offline reading.
Top Python Libraries for HTML to PDF
Several libraries can help. We will focus on two main ones. They are WeasyPrint and pdfkit (which uses wkhtmltopdf).
1. WeasyPrint
WeasyPrint is a modern library. It renders HTML and CSS to PDF. It does not need an external tool like a browser.
It supports modern web standards. This includes CSS Flexbox and Grid. It is a great choice for clean, styled PDFs.
Install it using pip.
pip install weasyprint
2. pdfkit (wkhtmltopdf)
pdfkit is a wrapper for wkhtmltopdf. wkhtmltopdf uses the WebKit engine. It renders HTML like a web browser would.
It is very reliable for complex pages. You must install the wkhtmltopdf tool separately on your system.
Install the Python wrapper first.
pip install pdfkit
Converting HTML Strings to PDF
Often, you have HTML as a Python string. This is common when using templates. Both libraries handle this well.
Using WeasyPrint
Here is a basic example with WeasyPrint. We create a simple HTML string and convert it.
from weasyprint import HTML
# Define your HTML content as a string
html_string = """
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Arial; }
h1 { color: darkblue; }
</style>
</head>
<body>
<h1>My First PDF Report</h1>
<p>This PDF was generated from an HTML string using Python.</p>
</body>
</html>
"""
# Create an HTML object and write to PDF
HTML(string=html_string).write_pdf('output_from_string.pdf')
print("PDF generated successfully!")
The HTML() class takes the string. The write_pdf() method creates the file. The styling in the <style> tag is applied.
Using pdfkit
First, ensure wkhtmltopdf is installed on your system. Then you can use pdfkit similarly.
import pdfkit
html_string = "<h1>Hello PDF</h1><p>From pdfkit.</p>"
# Configure the path to the wkhtmltopdf executable if needed
# config = pdfkit.configuration(wkhtmltopdf='/usr/local/bin/wkhtmltopdf')
# pdfkit.from_string(html_string, 'output_pdfkit.pdf', configuration=config)
pdfkit.from_string(html_string, 'output_pdfkit.pdf')
print("PDF created with pdfkit.")
Converting HTML Files and URLs to PDF
You can also convert existing HTML files or live web pages.
Convert a Local HTML File
This is useful for pre-designed templates.
from weasyprint import HTML
# Convert a local HTML file
HTML(filename='template.html').write_pdf('report_from_file.pdf')
Convert a Web Page (URL)
You can save any webpage as a PDF. This is great for archiving.
from weasyprint import HTML
import requests
url = 'https://example.com'
# Fetch the HTML content
response = requests.get(url)
html_content = response.text
# Convert the fetched HTML to PDF
HTML(string=html_content).write_pdf('webpage_archive.pdf')
Advanced Styling and Page Control
For professional documents, you need control. You can set page size, margins, and headers.
WeasyPrint uses CSS for this. You use the @page rule in your CSS.
from weasyprint import HTML
html_with_css = """
<!DOCTYPE html>
<html>
<head>
<style>
@page {
size: A4;
margin: 2cm;
@top-center {
content: "Company Confidential Report";
}
}
body { font-family: 'DejaVu Sans', sans-serif; }
.header { font-size: 24px; }
</style>
</head>
<body>
<div class="header">Advanced PDF</div>
<p>This has custom page size and a header.</p>
</body>
</html>
"""
HTML(string=html_with_css).write_pdf('styled_document.pdf')
Handling Common Issues
You might face some problems. Here are solutions.
Missing Fonts: PDFs need embedded fonts. Use web-safe fonts or provide font files with @font-face in CSS.
Images Not Loading: Use absolute paths (http:// or full file paths) for image sources in your HTML.
Complex JavaScript: Most HTML-to-PDF tools do not run JavaScript. Use pre-rendered HTML if your content relies on JS.
Choosing the Right Library
How do you pick between WeasyPrint and pdfkit?
Choose WeasyPrint if: You want a pure Python solution. Your design uses modern CSS. You need easy installation (just pip).
Choose pdfkit/wkhtmltopdf if: You need perfect rendering of complex, JavaScript-heavy pages (though JS execution is limited). You are already using wkhtmltopdf in other projects.
Beyond Generation: Working with Existing PDFs
Your work with PDFs might not stop at creation. You may need to read or edit existing files.
For instance, you might need to extract data from a PDF form. Our guide on Extract PDF Form Text Fields with Python PdfReader can help with that.
Or, you might want to add bookmarks to your newly created PDF for better navigation. Learn how with Python PdfWriter.add_bookmark: Add Bookmarks to PDFs.
Sometimes, you need to read metadata from a PDF to understand its origin. The Python PdfReader.getDocumentInfo: Extract PDF Metadata guide covers this in detail.
Conclusion
Converting HTML to PDF in Python is straightforward. Libraries like WeasyPrint and pdfkit do the heavy lifting.
You can convert strings, files, or live web pages. With CSS, you control the PDF's look and layout.
Start by generating simple reports. Then move to complex documents with headers and custom fonts.
Remember the key points. Use absolute paths for assets. Stick to supported CSS features. Choose the library that fits your project's needs.
Now you have the knowledge. Go ahead and automate your PDF creation workflow.