
WeasyPrint is a Python library that renders HTML and CSS to PDF. It is not a browser: its layout engine is written in Python and built for paged media, so CSS features browsers ignore (@page, margin boxes, running headers, page counters, footnotes) work.
Kozea created it. CourtBouillon maintains it. BSD licence, Python 3.10+. The latest release is 70.0, from 8th September 2026. My machine still runs 68.1.
Why use it
- Write the document as a web page. HTML for structure, CSS for the look. An LLM writes both well, and you diff them like code.
- Print CSS that works. Page size, margins, a logo in the header of every page but the first, "Page 3 of 8" in the footer, a table header that repeats after a page break.
- No headless Chrome. No browser to install, launch or babysit. 1
pip installplus a few system libraries (Pango). - Deterministic. The same HTML gives the same PDF every time.
Install and use
brew install pango # system dependency on macOS
pip install weasyprint
from weasyprint import HTML
HTML(filename="brief.html").write_pdf("brief.pdf")
# from a string, with a base URL so relative images resolve
HTML(string=html, base_url=str(project_dir)).write_pdf("brief.pdf")
Or from the command line: weasyprint brief.html brief.pdf.
The print CSS that does most of the work:
@page {
size: A4;
margin: 22mm 18mm 20mm;
@top-left { content: ""; width: 28mm; height: 7mm;
background: url("logo.png") no-repeat left center; background-size: contain; }
@bottom-right { content: "Page " counter(page) " of " counter(pages);
font-family: "Lato", sans-serif; font-size: 8pt; }
}
@page :first { @top-left { content: none; } } /* no header logo on the cover */
thead { display: table-header-group; } /* repeat the header row on each page */
.footnote { float: footnote; }
Limits
- Not a browser. No JavaScript. Charts must arrive as SVG or images, not as a script that draws them.
- CSS support is partial. Flexbox and grid work, with gaps (see below). Check what you rely on against the docs.
- Speed. Fine for documents of a few dozen pages. A 500-page report takes a while.
- System libraries. Pango and friends must be present, which trips up a fresh machine or a minimal container.
Alternatives
| Tool | When it's the better choice |
|---|---|
| Headless Chrome (Playwright) | The page needs JavaScript, or must match the browser pixel for pixel |
| Typst | Typeset documents written in its own markup, very fast |
| ReportLab | PDFs drawn in code, no HTML |
| Pandoc | Markdown to PDF through LaTeX |
How I use it in NicAI
Every branded PDF my agents produce goes through WeasyPrint:
- 1-page A4 sheets for my B2B work: HTML from a template, then PDF.
- Internal briefs and handover documents, as a PDF twin of the Markdown source.
- Reports generated from my SQLite databases.
- Markdown to PDF in 1 keystroke. An Alfred keyword takes the Markdown on my clipboard and writes a branded PDF (logo on every page, page numbers, table of contents from 3 sections up). The Word version of the same command goes through Pandoc, see Convert Markdown files to Microsoft Word.
The QA loop matters more than the render, because none of these faults raises an error. After every build the agent renders each page to PNG (pdftoppm -r 110 -png) and looks at it. A script also checks at 300 dpi that the header, body and footer ink sit inside the page margins.
Every PDF is written to a temp file and renamed over the main file, so a PDF I have open in Preview never shows half a document.
Gotchas I hit
⚠️ WARNING:
gapon a flex row isn't counted in the container's width. A row of logos withdisplay: flex; gap: 4mmpaints past the right margin. Space the children withmargininstead, and give images an explicitwidthandheightin mm.⚠️ WARNING: Margin boxes (
@top-left,@bottom-right) don't inherit the font frombody. Declarefont-familyin each one, or the footer renders in the default serif.
- Inline SVG ignores CSS aimed at its
<text>elements. Setfont-family,font-sizeandfillas attributes on each element. - Floats overlap in a card grid. Use
display: inline-block. - Footnotes: no
margin-topon@footnote, or every note jumps to the next page. - No CSS shadows in documents meant as PDF. Shadows render poorly in print, and when a page goes through headless Chrome instead they become grey blocks. My rule: no
box-shadoworfilterin any PDF template.
Further reading

