Generating Documents
Generating Documents
Section titled “Generating Documents”fabricadabra’s Orders and Clients features turn your live records into polished
PDFs — invoices, receipts, and contracts — by running a Typst template
against a structured data payload. This tutorial covers that payload’s shape,
how your template reads it, and how to develop against realistic data without a
live order to point at.
The data convention
Section titled “The data convention”A document-generation script is an ordinary Typst file with one defining trait:
instead of reading loose sys.inputs.<name> params, it reads a single JSON
input named data:
#let data = json(bytes(sys.inputs.data))The generation engine builds that JSON from the order/client, its line items,
and the account’s business profile, then passes it as --input data=<json>. Your
template’s whole job is to lay that structure out.
The shape for invoices and receipts
Section titled “The shape for invoices and receipts”An order-scoped document (invoice or receipt) receives:
business: { name, address, email, website, abn, colours: { primary, accent }, payment }document: { id, issued, due, currency, tax_inclusive }client: { name, address }subject: stringitems: [{ index, description, note, quantity, unit_price, discount, tax, total }]payments_received: [{ label, amount }] // empty for an invoice; the paid amount for a receiptnotes: { customer_facing: string | none }custom: { … } // your frontmatter params, keyed by nameNotes on the fields:
business.colours.primaryis your brand colour as a hex string — feed it torgb(...)for a themed accent, with a fallback for when it’s unset.items[].totalis the fully-computed per-line amount (after discount and tax);discountandtaxare the absolute dollar components if you want a breakdown. Don’t recompute the maths yourself — the engine already agrees with the order UI and the Stripe invoice.- A receipt is the same script as an invoice. The only difference is that
payments_receivedis populated, so the balance-due block resolves to zero. Render both from one template. customholds the values entered for your frontmatter params. A base field (business/client/items/document) can never be shadowed by a custom param — custom values always live underdata.custom.
The shape for contracts
Section titled “The shape for contracts”A client-scoped contract aggregates line items across possibly-many orders, so its shape differs in two places — read them carefully:
business: { name, address, email, website, abn, colours, payment }document: { id, issued, title? } // `title` present only when it covers ordersclient: { names: [string] } // a LIST of parties — not a single `name`items: [{ index, description, quantity, unit_price, total }] // may be emptynotes: { customer_facing: string | none }custom: { … }The key trap: a contract has client.names (an array of party names), whereas
an invoice has client.name (one string). Use data.client.names.join(", ").
Reading it safely
Section titled “Reading it safely”Fields can be absent or none, so guard access with .at(key, default: …) and
in checks rather than assuming presence:
#let theme = if "colours" in data.business and data.business.colours != none and data.business.colours.primary != none { rgb(data.business.colours.primary)} else { rgb("#2b6cb0")}
#let schedule = data.custom.at("payment_schedule", default: ())#for row in schedule [ #row.label — #row.due_date #linebreak() ]Developing against sample data
Section titled “Developing against sample data”You don’t need a real order to build a template. The sample-data endpoint returns
exactly the shaped data structure described above, over canned fixtures:
GET /accounts/:accountId/documents/sample-data?kind=order&type=invoiceGET /accounts/:accountId/documents/sample-data?kind=order&type=receiptGET /accounts/:accountId/documents/sample-data?kind=client&type=contractThe fixtures deliberately exercise the awkward corners a template must handle —
multiple line items, per-line discount and tax, a muted note under a
description, a due date, customer-facing notes, and a non-USD currency — so you’re
not just testing a trivial happy path. The sample business is the fictional
Fabrikam Florals (a .example domain), never a real tenant’s data; the endpoint
executes nothing and spends no quota.
Drop that JSON into a local file and compile against it to iterate on layout:
typst compile template.typ out.pdf --input data="$(cat sample.json)"The shared helper library
Section titled “The shared helper library”The starter templates import their common helpers rather than each carrying a copy:
#import "/.fabricadabra/doc/0.1.0.typ": doc-data, custom-param, doc-theme, fmt-money, fmt-address| Helper | Does |
|---|---|
doc-data(placeholder: …) |
Reads the payload, falling back to your skeleton when there is none — so the template still runs in the editor |
custom-param(data, name, default: …) |
Reads one of your frontmatter params from either route: nested under data.custom during generation, or a top-level input on a plain Run |
doc-theme(data, fallback: …) |
The business’s brand colour, with a fallback |
fmt-money, fmt-address |
Formatting only — no rescaling, and absent address parts are skipped |
The platform injects this file into every server-side compile, so it is not in your project and there is nothing extra in your file tree. That does mean a typst compile on your own machine can’t see it — fetch it first, into the path the import expects:
mkdir -p .fabricadabra/doccurl -o .fabricadabra/doc/0.1.0.typ https://<your-host>/api/reference/typst-library/0.1.0GET /api/reference/typst-library lists the published versions.
Versions are pinned and never change. A template importing 0.1.0.typ keeps rendering exactly as it does today; a later platform release adds a new file beside it rather than altering what you have already sent to a client. Moving to a newer version is you editing that one import line.
You are not obliged to use any of it — a template that defines its own helpers works exactly as well, and drops the import.
Next steps
Section titled “Next steps”- The Invoice and Contract examples are complete, working templates built on exactly this convention — start from one and adapt.
- Parameters and inputs covers how your custom
data.custom.*fields are declared in frontmatter.