WeasyPrint
WeasyPrint is an actively maintained Python library (current version 68.1) that transforms HTML and CSS documents into high-quality PDF files. It acts as an 'Awesome Document Factory' by rendering web content using web standards, making it suitable for generating reports, invoices, books, and other printable documents with precise layout control. Releases are frequent, often several times a year.
Warnings
- breaking WeasyPrint versions 67.0 and higher require Python 3.10 or newer. Python 3.9 and older are no longer supported.
- breaking The `default_url_fetcher()` function is deprecated since v68.0 and will be removed in v69.0. Additionally, custom URL fetchers returning dictionaries are deprecated; they should return `URLFetcherResponse` objects instead.
- breaking WeasyPrint 63.0 replaced its underlying HTML parsing library `html5lib` with `tinyhtml5`. While `tinyhtml5` is a fork of `html5lib`, specific advanced features or internal APIs of `html5lib` may no longer be available or behave differently.
- gotcha WeasyPrint relies on external system libraries like Pango, Cairo, and GDK-Pixbuf (often provided by GTK+ on Windows) for rendering. These are *not* installed by `pip` and must be installed separately based on your operating system.
- gotcha Using WeasyPrint with untrusted HTML or CSS can lead to various security vulnerabilities, including local file access, system information leaks, and infinite network requests/loops.
- gotcha Some antivirus software may incorrectly flag WeasyPrint as malware (false positive).
Install
-
pip install weasyprint -
sudo apt install weasyprint -
brew install python pango libffi pip install weasyprint -
Follow the official documentation for GTK+ installation using MSYS2, then: pip install weasyprint
Imports
- HTML
from weasyprint import HTML
- CSS
from weasyprint import HTML, CSS
- default_url_fetcher
from weasyprint import default_url_fetcher
Quickstart
from weasyprint import HTML, CSS
html_content = """
<!DOCTYPE html>
<html>
<head>
<title>My Document</title>
<style>
@page { size: A4; margin: 2cm; }
body { font-family: sans-serif; }
h1 { color: #336699; }
p { line-height: 1.5; }
</style>
</head>
<body>
<h1>Hello, WeasyPrint!</h1>
<p>This is a test document generated from HTML and CSS into a PDF.</p>
<img src="https://weasyprint.org/static/logo.png" alt="WeasyPrint Logo">
</body>
</html>
"""
# You can also load from a URL: HTML('https://weasyprint.org/')
# Or from a file: HTML(filename='my_document.html')
# Create an HTML object from a string
html = HTML(string=html_content)
# Optionally, add external CSS (or inline as shown above)
# css = CSS(filename='style.css') # or CSS(string='body { font-size: 12pt; }')
# Write the PDF to a file
html.write_pdf('output.pdf')
print("PDF generated successfully as output.pdf")