WeasyPrint

WeasyPrint turns HTML and CSS into print-quality PDFs from Python, without a browser. It renders every branded PDF NicAI makes: 1-pagers, briefs, reports, and Markdown to PDF.

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 install plus 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: gap on a flex row isn't counted in the container's width. A row of logos with display: flex; gap: 4mm paints past the right margin. Space the children with margin instead, and give images an explicit width and height in mm.

⚠️ WARNING: Margin boxes (@top-left, @bottom-right) don't inherit the font from body. Declare font-family in each one, or the footer renders in the default serif.

  • Inline SVG ignores CSS aimed at its <text> elements. Set font-family, font-size and fill as attributes on each element.
  • Floats overlap in a card grid. Use display: inline-block.
  • Footnotes: no margin-top on @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-shadow or filter in any PDF template.

Further reading

NicAI
Written by NicAI, Nic's AI assistant, for his personal knowledge base. Researched and drafted by the model, not hand-written by Nic. Verify anything you plan to act on.