xhtml2pdf
PDF generator using HTML and CSS
What it is and what it does
xhtml2pdf is a pure-Python HTML-to-PDF converter that wraps reportlab, html5lib, and pypdf to turn HTML5 and CSS 2.1 markup into PDF documents. It lets developers who know HTML and CSS generate PDF templates without learning a specialized PDF library or adopting a different technology stack. The tool is platform-independent and installs as a standard Python package.
The package handles the full pipeline: parsing HTML5 with html5lib, applying CSS styling, rendering text and images via reportlab (which requires a system graphics backend like cairo), and assembling the final PDF. It also integrates pyHanko for PDF signing, python-bidi for right-to-left text, arabic-reshaper for Arabic script support, and svglib for SVG embedding, making it suitable for multilingual and complex document layouts.
Use it for:
- Generate invoice or receipt PDFs from web templates without duplicating design logic between HTML and PDF systems.
- Build report generators that accept HTML input and produce downloadable PDF files for distribution.
- Create multilingual PDF documents with proper text directionality and script handling for Arabic and bidirectional content.
- Embed SVG graphics and images in PDF output while maintaining CSS styling from the source HTML.
- Automate PDF certificate or document generation in web applications by reusing existing HTML/CSS templates.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Converts HTML and CSS to PDF documents using Python, enabling developers with web skills to generate PDF templates without learning specialized PDF libraries.
Yes, if you need to convert HTML/CSS to PDF and want to avoid learning a specialized PDF library. The permissive Apache 2.0 license and low install friction make it accessible. However, the aging maintenance status (last release 537 days ago) warrants caution in production: verify that its rendering quality and feature set meet your specific layout and CSS requirements before committing to it as a core dependency. No known security vulnerabilities as of the fact sheet date.
Install
xhtml2pdf on PyPI
pip
pip install xhtml2pdfuv
uv add xhtml2pdfpoetry
poetry add xhtml2pdfInstalling xhtml2pdf
Before you install
Low friction install with a pure-Python wheel. Maintenance is aging—last release was 537 days ago and the last commit was 2026-01-19—but the repository remains active and not archived. The project carries 9 runtime dependencies including reportlab, html5lib, and pypdf, which are themselves mature libraries.
License in practice
Licensed under Apache License 2.0 (permissive). You may use, modify, and distribute xhtml2pdf and derivative works freely, provided you include a copy of the license and document any changes. No restrictions on commercial use.
Quickstart
pip install xhtml2pdf
from xhtml2pdf import pisa
from io import BytesIO
html_string = '<html><body><h1>Hello</h1></body></html>'
pdf_file = BytesIO()
pisa.CreatePDF(html_string, pdf_file)
pdf_file.seek(0)
Requires Python 3.8+. For bitmap and vector rendering, reportlab needs a system-level graphics backend; cairo (installed via OS package manager) is recommended, or legacy RenderPM can be used as an alternative.
Verify before relying
- Whether the aging maintenance status (537 days since release) affects stability or security in production use cases.
- How well CSS 3 features beyond CSS 2.1 are actually supported in practice.
- Performance characteristics when rendering large or complex HTML documents.
Package facts
| License | Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for… (full text in the JSON record) (permissive) |
| Python support | supports the current Python release (>=3.8) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 9 — arabic-reshaper, html5lib, Pillow, pyHanko, pyhanko-certvalidator, pypdf, python-bidi, reportlab, svglib |
| Maintenance | aging — 537 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 4,345,363/month — #2,326 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: xhtml2pdf-0.2.17-py3-none-any.whl
Keywords: PDF, HTML, HTML, XML, CSS
Tags
More Utilities packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
charset-normalizerDetects and normalizes text encoding from…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
pluggyPluggy provides a plugin system that lets you…
permissive · top 100 on PyPI
PygmentsPygments is a syntax highlighter that colorizes…
permissive · top 100 on PyPI
sixSix provides utility functions to write Python…
permissive · top 100 on PyPI
weasyprintWeasyPrint converts HTML and CSS into PDF…
permissive · top 1,000 on PyPI
md2pdfConverts Markdown files to PDF with custom CSS…
permissive · top 15,000 on PyPI
pdfkitPython wrapper for wkhtmltopdf that converts…
permissive · top 5,000 on PyPI
rst2pdfConverts reStructuredText documents directly to…
permissive · top 15,000 on PyPI
markdown-pdfConverts markdown text to PDF files, with…
agpl · top 15,000 on PyPI
django-weasyprintProvides Django class-based views and response…
permissive · top 15,000 on PyPI
pyth3Converts marked-up text between formats…
permissive · top 5,000 on PyPI
pynlinerConverts CSS stylesheets to inline styles in…
permissive · top 15,000 on PyPI
pdfrw2pdfrw2 reads, writes, and manipulates PDF files…
permissive · top 15,000 on PyPI
svglibSvglib reads SVG files and converts them to…
copyleft · top 5,000 on PyPI