Case study · pdfcn-rs
pdfcn-rs: PDFs as components, no browser ↗
HAML-like templates, JSON data and Tailwind-style utilities turned into PDF in memory, in pure Rust and with no browser. Today it is a public API with an in-browser playground.
The problem
Generating invoices, catalogues or reports as PDF usually ends in one of two places: drawing coordinates by hand with a low-level library, or starting a headless Chromium to print HTML. The first is slow to maintain; the second weighs hundreds of megabytes, is slow to start and sits badly in a serverless function.
I wanted to write a PDF the way you write an interface, with components and utility classes, and generate it inside a Vercel Function or a Lambda.
Decisions
No Chromium. HTML and CSS are laid out by a layout engine written in Rust, with no browser process and no dynamic system dependencies. Everything happens in memory.
A pipeline of small pieces. An indentation-based HAML-like template parser (%h1, .flex.gap-2, attributes in parentheses), an expression engine for {{ }}, - if, - for and - include, a component registry that expands to markup, and a Tailwind-style utility scanner with no Node. Each crate has one job.
Escape once, at the edge. Templates resolve data without escaping, and escaping happens when the final HTML is rendered. No double escaping, no gaps.
A small, explicit API. Only template is required; data, page, theme, fonts, images, svgs and factur_x are optional. Fonts and images arrive as base64 and nothing is fetched at render time, so the output depends only on the request. Template errors come back as a plain-text 422, not as a broken PDF.
What is built
- A site with a playground: template and data editors, a preview and eleven ready examples, from an executive dashboard to a boarding pass or a certificate.
POST /api/generate-pdffor one document andPOST /api/generate-pdf-batchfor up to 50 per request, with shared fields and one error per document instead of a global one.- Around thirty components:
DocumentLayout,Card,Table,InvoiceTable,Badge,Alert, form fields,Pagination,SignatureBlock,PageFooter… - Gradients, rings, shadows, radii, opacity, transforms and text shadows, with Inter, Source Serif 4 and JetBrains Mono bundled.
- Bar, line, pie and sparkline charts, QR codes, barcodes (code128, EAN, UPC-A, code39, ITF) and inline SVG.
- Light and dark themes with tokens: changing
primaryrebrands utilities and components. - Clickable links (
https,mailto,tel) and internal jumps to#idinside the PDF itself. - Factur-X invoices: the same PDF carries the EN 16931 XML that the client’s accounting system reads, with profiles from
minimumtoextended. - A CLI with
new,add,buildanddev(live-reload preview) and Node bindings through napi-rs.
How fast it is
Each PDF takes between 28 and 70 ms. I measured it with the repository’s benchmark (cargo bench, release build), 20 samples per case:
| Example | Template → HTML | HTML → PDF |
|---|---|---|
| Invoice | 0.08 ms | 28 ms |
| Catalogue | 0.3 ms | 69 ms |
| Showcase (3 pages) | 1.5 ms | 61 ms |
Almost all the time goes to the layout engine and writing the PDF; the template is negligible. These are single-run times on a cloud machine, with no warm-up between documents. The pdfcn release binary is 19 MB.
Limits, in writing
The site and the repository say what it does not do: the API does not allow include partials, QR values top out around a kilobyte, images must be sent as base64 and links inside CSS-transformed elements are not repositioned. An invalid barcode draws an explicit marker instead of failing silently. Nor does it sign documents: certificate signing (PAdES/X.509) is not implemented. I’d rather have them written down than discover them in production.
What I learned
The component-and-utility mental model works as well on paper as on screen, and taking the browser out of the way changes what is possible in serverless. Putting a playground in front of the API explained it better than any README, and documenting the limits as precisely as the features still saves more time than any feature.