STK-84Default Open source
Use Playwright or WeasyPrint to turn HTML into PDF
Build invoices, receipts and reports as HTML and print them to tagged PDF with Playwright in Node.js or WeasyPrint in Python.
Review by
2027-04-30
How to do it
Playwright is already the browser test pick. Its page.pdf() prints a page, and the tagged option writes an accessible PDF. WeasyPrint renders HTML and CSS to PDF without a browser. Its first stable version, 43, came out in November 2018, and 70.0 shipped on 8 September 2026. PyPI counted about 8.9 million downloads in the week to 20 October 2026. It writes PDF/UA and PDF/A variants. Its docs warn that untrusted HTML or CSS can read local files or run for a long time. Escape every value you put in the page, as Keep input out of templates says. Choose something else when a country requires a structured e-invoice, because a PDF is not one, as Send e-invoices where the law requires them says.
Examples
JavaScript example
import { chromium } from 'playwright';
export async function invoicePdf(html) {
const browser = await chromium.launch();
try {
const page = await browser.newPage();
await page.setContent(html, { waitUntil: 'load' });
return await page.pdf({ format: 'A4', printBackground: true, tagged: true });
} finally {
await browser.close();
}
}
Python example
from weasyprint import HTML
def invoice_pdf(html: str) -> bytes:
return HTML(string=html, base_url="https://example.com/").write_pdf(pdf_variant="pdf/ua-1")
How to check
A test prints a sample invoice to PDF, and a PDF text extractor finds the order number as text. Every PDF call sets tagged: true or a pdf/ua variant.
Sources
Related rules
- STK-41 Use Playwright for browser tests in Software choices
- APS-5 Keep input out of templates in Application security
- PAY-55 Send e-invoices where the law requires them in Payments and billing
- MED-25 Make generated PDFs accessible in Files and media
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/stack/product-services/html-to-pdf">Agent Wranglers software choices playbook</a>. In code you write, add a comment next to it with the same link.