Skip to content

Designing a PDF once and rendering it with data

Use the native document schema for PDF layout and the versioned template format for authoring that layout. A template expands into an ordinary document with the same properties and child order. Pagination, fonts, PDF encoding and layout checks run in the existing engine.

Check and render an invoice with the native CLI

Install the Beta 7 CLI on Linux. From the repository root, check the template against the example data and render its PDF. Installation guide

cd docs/agents/examples
pressjet template check invoice.template.json invoice.data.json &&
  pressjet template render invoice.template.json invoice.data.json invoice.pdf

The example flows 24 line items across two A5 pages, repeats the invoice number, and prints the total after the final item. The application supplies the formatted amounts and total. check returns JSON with ok, pageCount, up to three diagnostics (errors first), and omitted. The shell runs render only after a successful check. For a validated input, render alone runs one generation pass. Use - for either JSON input or for PDF output on stdout. The native CLI accepts up to 32 MiB per input file. Invoice template · Invoice data

Choose what needs a model

What the application already has What to send the model What the model returns
Data and a selected layout Nothing Render directly in the application
Data and several suitable layouts Allowed layout IDs, their descriptions and the user's request One ID, checked against the application's allowlist
Data and a new design brief Field names, types, nesting, display requirements and a short example if necessary A reusable v1 template
A layout and a request for original prose A brief and the facts required for the current section The new text fields only

For example, both bundled dispatch layouts accept {"pages":[{"label":"Box 41"},{"label":"Box 82"}]}. Choose dispatch-slips for one label per physical page, or dispatch-list for a flowing list. The section-report starter accepts {"title":"Status","sections":[{"heading":"Operations","paragraphs":[{"body":"Both shipments arrived."}]}]}.

pressjet-template check  dispatch-slips data.json
pressjet-template render dispatch-slips data.json slips.pdf

A custom template file can replace the starter name. With the SDK installed in a project, use the command from an npm script or ./node_modules/.bin/pressjet-template. In this repository, build the SDK and use node sdks/typescript/bin/pressjet-template.mjs; set PRESSJET_BINARY to a build of the matching native renderer.

A short prompt for a new layout

Return only {"v":1,"root":["document",…]}. Nodes: [kind, optional native properties,
...children]. A document contains pages; a page or view contains view, text or image.
Text can contain strings, spans and ["field","key"]. ["each","arrayKey",child]
repeats that child for records in order. Use literal Pressjet style properties.
Data: {rows:[{name:string,amount:string}]}. Create an A4 list with each name and
amount on the same line, in source order. The app supplies the formatted amounts.

One valid response is

{"v":1,"root":["document",["page",{"size":"A4","style":{"padding":32}},["each","rows",["text",["field","name"],"   ",["field","amount"]]]]]}
import { expandTemplate } from "pressjet/template";
import { checkLocal, renderLocal } from "pressjet/local";

const document = expandTemplate(savedTemplate, applicationData);
const report = await checkLocal(document);
if (!report.ok) throw new Error(JSON.stringify(report.diagnostics.slice(0, 3)));
const pdf = await renderLocal(document);

Keep the returned PDF bytes, the expanded tree, the full per-page report and the real data inside the application. During design, the CLI's check sends at most three diagnostics plus the page and omitted counts back to the agent. It exits with status 1 for a layout error. Use the full SDK report for inspection. Keep a tested template under application version control alongside examples with long fields, empty groups, image assets and page breaks. Layouts and data can both affect pagination and glyph coverage. Check new data when the application's quality contract requires it. The fast render path and the explicit check are separate calls.

Authoring contract v1

The envelope has exactly two members, v equal to 1 and root containing one document tuple. A missing properties object expands to {}. A repeat can occur in a child position anywhere the repeated child is valid. For each array element, that record becomes the scope, including inside nested repeats. The root scope is the supplied JSON object. Field names are literal own keys. A text binding accepts a string or a finite number. Supply dates, amounts and identifiers as display strings when their exact formatting matters. Literal text never undergoes interpolation. An image can bind its src to ["field","logo"]; its record supplies embedded base64 or a PNG/JPEG data URL. A barcode can bind its value to a string or numeric field in the same way. Other properties and styles are literals.

Missing fields, invalid record types and unknown directives fail the export. An empty repeat emits zero children, and the final document must contain a page. Both implementations bound template depth to 32, template and expansion work to 500,000 steps each, and aggregate text/property size to 16 Mi UTF-16 units per budget. Each text child is limited to 100,000 UTF-16 units. Actual PDF layout and render limits also apply. The JSON Schema describes the authored structure. Expansion checks data-dependent conditions, and native validation checks style and image values. Template schema · Native schema

Long original documents

Store original paragraphs once in application data. If a single response is too large, assign stable section IDs locally and request bounded groups in fresh calls with a common outline, style brief and just the required facts. Join by ID in source order and verify missing or duplicate sections before expanding once. Retry only the failed group. An editor that must read previous sections still pays for that context. With a bounded amount of context per group, total text tokens grow with the amount of new content. Page count alone cannot bound tokens because a page can contain one label or many paragraphs.

pressjet-template render section-report report-data.json report.pdf