12. Document Generation
Three functions render a document from your data. All of them return a Base64 string, never a file, a byte array, or a media library id.
The generated bytes come back encoded so process code can forward them — store them in a text field, post them to an external API, or hand them to the frontend. Nothing is written to storage on your behalf.
generatePdf(slug, id, templateId, header?, footer?)
async· returnsstring(Base64)
Renders an HTML template against one record and prints it to PDF.
Prop
Type
const base64 = await context.functions.generatePdf(
"invoices",
context.record.ID,
"invoice-template"
);
await context.functions.update("invoices", context.record.ID, { PdfData: base64 });With a header and footer:
const base64 = await context.functions.generatePdf(
"invoices", context.record.ID, "invoice-template",
'<div style="font-size:9px;width:100%;text-align:center">ACME d.o.o.</div>',
'<div style="font-size:9px;width:100%;text-align:right">Page <span class="pageNumber"></span> of <span class="totalPages"></span></div>'
);Header and footer are rendered by headless Chrome, so the usual pageNumber, totalPages, date, and title placeholder classes are available. Keep the font size small — Chrome allocates limited margin space.
generateWord(slug, id, templateId)
async· returnsstring(Base64)
Renders a template against one record as a Word document.
Prop
Type
const base64 = await context.functions.generateWord("contracts", context.record.ID, "contract-template");Word output is HTML wrapped in the legacy WordML envelope and should be named with a .doc extension. Word opens it correctly; tools that strictly expect the modern .docx package format will not. Name the file accordingly when you hand it on.
generateExcel(slug, fields?, filter?)
async· returnsstring(Base64)
Exports an entity's rows to a spreadsheet. No template involved.
Prop
Type
const base64 = await context.functions.generateExcel("orders", ["ID", "ClientName", "Amount", "Status"]);generateExcel takes a flat string, not the { field, operator, value } object that get uses. Passing an object silently produces an unfiltered export.
The format is and_Field_operator_Value, with conditions concatenated directly:
// Status equals "open"
await context.functions.generateExcel("orders", null, "and_Status_eq_open");
// Status equals "open" AND Amount greater than 1000
await context.functions.generateExcel("orders", null, "and_Status_eq_openand_Amount_gt_1000");Use the short operator codes: eq, neq, gt, gte, lt, lte, like, startswith, endswith, in, notin, isnull, isnotnull. Because the underscore is the delimiter, an underscore inside a value must be escaped as ~_.
The export is capped at 5,000 rows and the limit cannot be raised from process code. Larger datasets are truncated with no error. Narrow the export with a filter, or build the file yourself with XLSX — see Excel Templates.
Using the result
Base64 is a string, so it fits anywhere text does.
// Store it on the record
await context.functions.update("invoices", id, { PdfData: base64 });
// Post it to an external system
await context.functions.http({
method: "POST",
url: context.env.ARCHIVE_URL + "/documents",
headers: { "Authorization": "Bearer " + context.env.ARCHIVE_TOKEN },
body: { filename: "invoice-" + id + ".pdf", contentBase64: base64 }
});The email function sends a subject and body only — there is no attachment parameter. To deliver a generated document, store it and email a link, or push it to a system that handles delivery.
Large documents held as Base64 sit entirely in memory and inflate by roughly a third versus the raw bytes. Generate them one at a time rather than building an array of them in a loop.