How to generate an invoice PDF with an API
Published 2 min read
An invoice is data (a number, a customer, some lines, a total) that has to leave your system as a document. This guide builds one as an HTML template and renders it to PDF with the rustedf.cloud API, formatted for the country of the reader.
It covers commercial invoices and receipts. Tax and legal requirements for invoices differ by country and are outside its scope.
The template
A template is HTML and CSS with {{ }} for values and {% %} for logic. Save it in the
portal, where you get a live preview, or send it inline.
<style> @page { size: A4; margin: 20mm; @bottom-right { content: "Page " counter(page) " of " counter(pages); } } body { font-family: sans-serif; } table { width: 100%; border-collapse: collapse; } th, td { padding: 6px 4px; border-bottom: 1px solid #ccc; text-align: left; } .n { text-align: right; }</style>
<h1>Invoice {{ number }}</h1><p>{{ customer.name }}<br>Issued {{ issued | date(style="long") }}</p>
<table> <thead> <tr><th>Description</th><th class="n">Qty</th><th class="n">Amount</th></tr> </thead> <tbody> {% for item in items %} <tr> <td>{{ item.name }}</td> <td class="n">{{ item.qty | number }}</td> <td class="n">{{ item.amount | currency(code="USD") }}</td> </tr> {% endfor %} </tbody></table>
<h2>Total {{ total | currency(code="USD") }}</h2>The {% for %} loop repeats a row for each item, so the table grows with your data and
continues on the next page when it has to. The @page rule sets the paper and a page
counter in the corner.
The data
Send amounts as strings. A string such as "1234.50" avoids the surprises that floating
point numbers bring, and the currency filter rounds it to the digits of the currency.
{ "number": "2025-0042", "issued": "2025-03-14", "customer": { "name": "Bela Vista Market" }, "items": [ { "name": "Visual identity", "qty": 1, "amount": "4800.00" }, { "name": "Packaging design", "qty": 6, "amount": "3900.00" } ], "total": "8700.00"}Calculate the lines and the total in your own system and send the results. The template formats them; it does not need to be the place where your money is computed.
One template, every market
Add a locale to the request and the three formatting filters follow it.
curl -X POST https://api.rustedf.com/v1/render \ -H "Authorization: Bearer $RUSTEDF_KEY" \ -H "Content-Type: application/json" \ -d '{ "template_id": "0194f2c0-…", "locale": "pt-BR", "data": { "number": "2025-0042", "issued": "2025-03-14", "total": "8700.00" } }' \ -o invoice.pdfHere is how the same figure and date read for four readers:
| Locale | Currency | 1234.5 |
date(style="long") for 2024-03-15 |
|---|---|---|---|
en-US |
USD | $1,234.50 | March 15, 2024 |
pt-BR |
BRL | R$ 1.234,50 | 15 de março de 2024 |
de-DE |
EUR | 1.234,50 € | 15. März 2024 |
es-ES |
EUR | 1234,50 € | 15 de marzo de 2024 |
The locale comes from the request first, then from the saved template, then from your
account’s default. An invalid tag is a 400.
Logo and payment code
Upload a PNG or JPEG once, in the portal or with POST /v1/assets, and use it by
reference. A QR code for a payment link is one filter away:
<img src="asset:0194f2c0-…" width="120" alt="Company logo">{{ pay_url | qr(size=30) | safe }}safe is there because the QR code is markup the service draws itself. Your data stays
escaped.
Check it before a customer sees it
POST /v1/validate builds the document and lists anything the service did not fully
support, without returning the PDF and without using a credit. Run it on your template
once, and add "strict": true to your tests so a change that introduces an unsupported
property fails there instead of in production.
Continue with Templates, Locales and formatting and Images.
Keep reading
Getting started
HTML and CSS for PDF: what works and how to check it
A PDF is paged, not scrolled. See which HTML and CSS a PDF renderer supports, what to leave out, and how to validate a document before it ships.
3 min read
Getting started
HTML to PDF: library, headless browser or API? How to choose
Three ways to turn HTML into a PDF, what each asks you to run and maintain, what HTML and CSS you get, and a short list of questions that decide.
4 min read
Getting started
HTML to PDF API: generate a PDF with one request
Send HTML and data in one POST and get a PDF back. A curl example, a Node.js example, and what to know about images, limits and credits.
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