Skip to content
Lamina PDF
0.9.0
GitHub
Open the web app

Fill an invoice from JSON

You’ll make an invoice from two JSON files: a template with the heading, text and a table, and the data with the number, line items and total. A program or a language model can write either, and the schema checks them before anything is drawn.

The template is a document with placeholders that point into the data. This table row is repeated once per line item, and each cell takes a value from the current item:

"rows": [{
"$each": "/items",
"do": { "cells": [
{ "text": { "$item": "/item" } },
{ "text": { "$item": "/description" } },
{ "text": { "$item": "/quantity" }, "align": "end" },
{ "text": { "$item": "/amount" }, "align": "end" }
] }
}]

Each example fills the template from the data, gives it its two fonts by the names it uses, and returns the PDF’s bytes. Both write the same bytes:

packages/sdk/examples/invoice-from-json.ts
// An invoice from a JSON template and its data: the template lays out the heading, the text and
// a table, and the data fills in the number, the line items and the total.
//
// The table runs onto a second page with its header row repeated, and the total stays with the
// last rows. The template names its two fonts, `regular` and `bold`; their bytes come beside it.
// The output is checked against PDF/UA-1, which the template asks for.
import { Builder, type BuilderOptions, type FlowTemplate, Font } from "@laminapdf/sdk";
/**
* The invoice's bytes. Needs `init({ text: true })`.
*
* @param template - the invoice template, a `laminapdf.document/1` document with placeholders.
* @param data - the values its placeholders read.
* @param regular - the font the template calls `regular`.
* @param bold - a collection whose second face (index 1) is the one it calls `bold`.
* @param options - can fix the file ID, the producer and the creation date.
*/
export function invoiceFromJson(
template: FlowTemplate,
data: unknown,
regular: Uint8Array,
bold: Uint8Array,
options: BuilderOptions = {},
): Uint8Array<ArrayBuffer> {
// The builder checks the profiles it is given: the ones the template asks for.
using b = new Builder({ ...options, profiles: template.profiles ?? [] });
b.flow(template, {
data,
fonts: { regular: Font.fromBytes(regular), bold: Font.fromCollection(bold, 1) },
});
using doc = b.finish();
return doc.save();
}

The result is two A4 pages that meet PDF/UA-1. The table’s header row repeats at the top of the second page, and the total stays with the last rows. The Result tab shows both pages, with a link to the PDF.

  • fill (in TypeScript, builder.flow given data) replaces each placeholder with the value it points to, then reads the result as a document. A pointer is a path into the data (RFC 6901): /invoice/number is the number inside invoice, and /items/0 the first line item.
  • { "$data": "/total" } puts a value from the data where text belongs, and { "$item": "/amount" } puts one from the current item of an $each.
  • { "$each": "/items", "do": … } repeats a block or a table row once per item of a list. { "$if": "/invoice/paid", "do": … } keeps its block only when that value is there and isn’t false or null. This invoice’s data says it isn’t paid, so the “Paid in full” line is left out.
  • The template names its text styles once, such as body and strong, and each style names a font. The fonts’ bytes come beside the template, through Resources in Rust and fonts in TypeScript, so the template holds no files.
  • The template asks for PDF/UA-1 in its profiles, so each example passes them to the builder, which checks the rules for accessible PDFs before it writes anything.
  • The line items: edit the data. The template doesn’t change, and the table runs onto as many pages as the items need.
  • Totals, dates and currency: put them in the data already formatted, such as "total": "4,001.10 €". Templates have no arithmetic or formatting by design: whatever writes the data does the sums.
  • A misspelt key: reading the document fails with invalid-document. Its data.pointer names the value, such as /blocks/2/spaceAftr, and its didYouMean the closest key the format knows. In TypeScript, tsc refuses a misspelt key in a template you write in your code as a FlowTemplate; a template read from a file is checked when flow reads it.
  • A value the data lacks: filling fails with template-data-missing and the pointer the template used, so a gap never reaches the PDF.
  • Check what a model writes: validate the document against laminapdf.document/1’s schema. The laminapdf crate and @laminapdf/sdk both ship it as docs/schemas/document.schema.json.

The command-line tool fills the template and lays it out in one command, giving each font by the name the template uses:

Terminal window
laminapdf build invoice.json --data invoice-data.json --font regular=NotoSans-Regular.ttf --font bold=NotoSans-RegularBold.ttc#1 -o invoice.pdf

#1 takes the bold face of a font collection. Make and change PDFs has the rest.

Next: Pages that flow, with every block a document can hold.