Write your first template
Build a DocStudio template from scratch: page setup, header and footer, bound text, a table with a total, and the data it expects.
A template is a JSON object with a schemaVersion, an optional config and page, and a body array of nodes. Every node has a unique id and a type.
A short invoice
{
"schemaVersion": "1.0.0",
"name": "Short invoice",
"config": { "page": { "size": "A4", "margins": 18 }, "locale": "en" },
"page": {
"footer": {
"height": 12,
"children": [{ "id": "pages", "type": "pageNumber", "format": "Page {n} of {total}" }]
}
},
"body": [
{
"id": "title",
"type": "text",
"content": ["Invoice ", { "path": "number" }],
"style": { "fontSize": 20, "fontWeight": "bold" }
},
{
"id": "lines",
"type": "table",
"source": { "path": "items" },
"repeatHeader": true,
"columns": [
{ "id": "name", "header": "Item", "cell": { "path": "name" } },
{ "id": "qty", "header": "Qty", "cell": { "path": "qty" }, "align": "end" },
{
"id": "total",
"header": "Total",
"align": "end",
"cell": { "fn": "mul", "args": [{ "path": "qty" }, { "path": "price" }] }
}
],
"footer": [
{
"cells": [
{ "text": "Total", "span": 2 },
{
"expr": {
"fn": "sum",
"args": [{ "path": "$rows" }],
"of": { "fn": "mul", "args": [{ "path": "qty" }, { "path": "price" }] }
},
"align": "end"
}
]
}
]
}
]
}The data
{
"number": "INV-1042",
"items": [
{ "name": "Design review", "qty": 2, "price": 120 },
{ "name": "Template setup", "qty": 1, "price": 480 }
]
}What each part does
config.pagesets the paper size (A3,A4,A5,A6,Letter,Legalor{ "w", "h" }in millimetres) and margins in millimetres.page.footerrepeats on every page.pageNumbersupports{n}and{total}.contentis an array of segments. A string is literal text.{ "path": "number" }reads the data.{ "fn": "mul", "args": [...] }calls a built-in function.$rowsinside a table footer is the list of data rows.- Footer cells use
spanto cover several columns.
Render it with renderDocument. The schema is checked when you render, so a typo such as an unknown property is reported as a diagnostic with the JSON Pointer of the problem.
Build it visually instead
Paste the template into the designer as the default template and edit it on the canvas. The JSON tab shows the same document.