QR codes and barcodes in a PDF generated from HTML
Published 2 min read
A ticket needs a code a scanner can read. A shipping label needs a barcode. An invoice is easier to pay with a QR code that opens the payment link. You can draw all three with the template language of rustedf.cloud, without generating an image first.
Two filters
{{ pay_url | qr(size=30) | safe }}{{ number | barcode(width=60, height=15) | safe }}qr draws a QR code and barcode draws a Code 128 barcode, both from any text or number in
your data. The safe filter is there because the code is markup the service draws itself.
Your own data stays escaped.
A ticket, end to end
<style> @page { size: A4; margin: 20mm; } body { font-family: sans-serif; } .ticket { width: 120mm; border: 2px solid #111; padding: 8mm; } h1 { margin: 0 0 4mm; }</style>
<div class="ticket"> <h1>{{ event.name }}</h1> <p>{{ event.date | date(style="long") }}<br>{{ holder }}</p> {{ ticket_url | qr(size=40) | safe }} <p>{{ code }}</p> {{ code | barcode(width=70, height=18) | safe }}</div>{ "event": { "name": "Open Studio Night", "date": "2026-12-05" }, "holder": "Marina Costa", "ticket_url": "https://example.com/t/8f3a-91c2", "code": "TKT-0042-8F3A"}Each ticket is one render with its own data. To produce a batch, call the endpoint once per ticket.
Sizes and limits
| Filter | Arguments | Holds |
|---|---|---|
qr |
size in millimetres (5 to 200, 30 by default), level of error correction L, M, Q or H (M), margin of quiet zone in squares (0 to 10, 4) |
Up to 1000 bytes of text. |
barcode |
width and height in millimetres (20 to 200 and 5 to 100; 60 and 15), margin of quiet zone in modules (0 to 20, 10) |
Up to 80 letters, digits and punctuation, without accents. |
A few things follow from this:
- A code is a set of vector rectangles, so it stays sharp at any size, on screen or printed.
- A higher error-correction
levellets a QR code survive more damage, at the price of a denser pattern. Keep the default unless you know a printed code will be scuffed. - A barcode does not take accents. If a value can have them, use a plain code (an order number, a tracking code) rather than a name.
- Leave the quiet zone alone. Scanners need empty space around a code to find it.
Bad input
A missing or empty value draws nothing, so a document without a link simply has no code. A value that cannot be held, such as text over the limit or accents in a barcode, is an error rather than a code that quietly fails to scan. All the codes of one document together are limited to 4 MiB of markup, far more than a page needs.
Check before printing
Scan a printed test, not only the screen. And run POST /v1/validate on your template: it
reports what the renderer could not draw, without returning the PDF or using a credit.
See Templates for the full list of filters and Validating a document.
Keep reading
PDF layout
Page breaks in a PDF with CSS: keep blocks and tables together
Control where a PDF page ends: force a new page, keep a signature or a total in one piece, and let long tables run across pages with a repeated header.
2 min read
PDF layout
Page numbers, headers and footers in a PDF with HTML and CSS
Add "Page 2 of 5", a running title and a confidential note to every page of a PDF with the CSS @page rule. Working examples and what to check.
2 min read
Code and integrations
Generate a PDF in ASP.NET Core: MVC, Razor Pages and minimal APIs
Turn a Razor view into a PDF in ASP.NET Core with one API call: a view renderer, a print-ready view, an MVC action, a Razor Pages handler and the traps to avoid.
3 min read
Code and integrations
Convert a web page to PDF in C# without a headless browser
Fetch a page with HttpClient and turn its HTML into a PDF with one API call in C#. Which pages this suits, which it does not, and how to check before you rely on it.
3 min read