Skip to content

Invoice

A minimal document-generation invoice: guarding the data input, reading frontmatter params, and rendering a line-item table. Start from the Simple Invoice starter template for a production-grade one.

/*javasculpt-params
# Frontmatter params (NOT getParameterDefinitions() — Typst isn't JS). Each entry
# becomes a live control in the editor and arrives as sys.inputs.<name>.
- name: accent
type: color
caption: Accent colour
default: "#2b6cb0"
*/
// ── Example: document-generation invoice ───────────────────────────────────
// A deliberately SMALL invoice, kept to the three things that are specific to a
// document-generation script. It is teaching material, not a template to ship:
// for a production-grade invoice — receipts, payment schedules, discounts, tax
// columns, bank details — start from the "Simple Invoice" starter template,
// which is a full-featured file copied into your own project.
//
// The three things worth learning here:
// 1. the `data` input, and why it needs a guard
// 2. frontmatter params, which arrive by two different routes
// 3. reading the shaped payload the pipeline feeds every invoice script
//
// The payload (see GET .../documents/sample-data, #830, for a real one):
// business: { name, address, email, website, abn, colours, payment }
// document: { id, issued, due, currency, tax_inclusive }
// client: { name, address }
// subject: string
// items: [{ index, description, note, quantity, unit_price, discount, tax, total }]
// payments_received: [{ label, amount }]
// notes: { customer_facing: string | none }
// custom: { <your frontmatter params> }
// 1. THE DATA GUARD. `data` is supplied only by the generation pipeline. A plain
// Run in the editor passes this file's declared params and nothing else, so an
// unguarded read dies on the first line and the script can never be previewed or
// edited (#1033). Always fall back to a correctly-shaped empty skeleton.
#let placeholder = (
business: (name: "Your Business", colours: (:)),
document: (id: "SAMPLE-0001", issued: "", currency: "usd"),
client: (name: "Sample Client"),
subject: "",
items: (),
custom: (:),
)
#let data = if "data" in sys.inputs { json(bytes(sys.inputs.data)) } else { placeholder }
// 2. PARAMS ARRIVE TWO WAYS. Generation nests them under `data.custom` (#595);
// a plain editor Run passes each as a top-level input. Read only one and your
// declared control silently does nothing in the other context.
#let param(name, default: none) = if name in sys.inputs {
sys.inputs.at(name)
} else {
data.custom.at(name, default: default)
}
#let accent = rgb(param("accent", default: "#2b6cb0"))
#let money(x) = str(calc.round(x, digits: 2)) + " " + upper(data.document.currency)
// 3. THE LAYOUT. Ordinary Typst from here — nothing platform-specific.
#set page(paper: "a4", margin: (x: 22mm, y: 20mm))
#set text(size: 10.5pt, fill: luma(25))
#grid(
columns: (1fr, auto),
align(left, text(size: 16pt, weight: "bold")[#data.business.name]),
align(right)[
#text(size: 18pt, weight: "bold", fill: accent)[Invoice]
#linebreak()
#text(size: 10pt)[#data.document.id]
#linebreak()
#text(size: 9pt, fill: luma(100))[Issued #data.document.issued]
],
)
#v(0.8em)
#line(length: 100%, stroke: 0.6pt + accent.lighten(50%))
#v(0.8em)
#text(size: 8.5pt, fill: luma(100), weight: "medium")[BILL TO]
#linebreak()
#text(weight: "bold")[#data.client.name]
#if data.at("subject", default: "") != "" [
#linebreak()
#text(fill: luma(90))[#data.subject]
]
#v(1em)
// `data.items` is empty on a plain Run, so the table renders header-only rather
// than failing — the reason the placeholder above carries every key.
#table(
columns: (auto, 1fr, auto, auto),
align: (left, left, right, right),
stroke: (bottom: 0.5pt + luma(200)),
inset: (x: 6pt, y: 6pt),
table.header(
text(weight: "bold", size: 8.5pt)[\#],
text(weight: "bold", size: 8.5pt)[Description],
text(weight: "bold", size: 8.5pt)[Qty],
text(weight: "bold", size: 8.5pt)[Amount],
),
..data.items.map(item => (
str(item.index), item.description, str(item.quantity), money(item.total),
)).flatten(),
)
#v(0.8em)
#align(right)[
#text(weight: "bold")[Total: #money(data.items.map(it => it.total).sum(default: 0))]
]