Building dynamic reports with DocStudio
Build a multi-section data report with DocStudio: KPI tiles from aggregates, a table with percentages and totals, a repeater for highlights and page furniture.
This tutorial builds the business report example: three KPI tiles, a table by region and a list of highlights, all driven by one JSON object.
The data
{
"company": "Harbor & Pine Studio",
"period": "Q3 2026",
"regions": [
{ "name": "North America", "revenue": 482000, "target": 450000 },
{ "name": "Europe", "revenue": 351000, "target": 380000 }
],
"highlights": [{ "text": "Signed two multi-year agreements in North America." }]
}KPI tiles
Each tile is a stack with a small label and a value. The value is an aggregate over the regions array:
{
"id": "kpiRevenueV",
"type": "text",
"style": { "fontSize": 18, "fontWeight": "bold" },
"content": [
{
"fn": "format",
"args": [
"currency",
{ "fn": "sum", "args": [{ "path": "regions" }], "of": { "path": "revenue" } },
"USD",
0
]
}
]
}Three tiles sit in a grid with columns ["1fr", "1fr", "1fr"]. Outside a table you name the list and the per-item expression yourself, because $rows exists only inside a table.
The table
{
"id": "regions",
"type": "table",
"source": { "path": "regions" },
"zebra": true,
"repeatHeader": true,
"columns": [
{ "id": "region", "header": "Region", "cell": { "path": "name" } },
{
"id": "revenue",
"header": "Revenue",
"align": "end",
"cell": { "fn": "format", "args": ["currency", { "path": "revenue" }, "USD", 0] }
},
{
"id": "attain",
"header": "Attainment",
"align": "end",
"cell": {
"fn": "format",
"args": [
"percent",
{ "fn": "div", "args": [{ "path": "revenue" }, { "path": "target" }] },
1
]
}
}
]
}The attainment column divides two fields per row and formats the ratio as a percent with one decimal. The footer uses $rows to total the columns and to compute the overall attainment from the totals, not from the average of percentages.
The highlights list
A repeater renders one line per item and an empty block when the list is empty:
{
"id": "highlights",
"type": "repeater",
"source": { "path": "highlights" },
"as": "item",
"empty": [{ "id": "none", "type": "text", "content": ["No highlights this period"] }],
"children": [{ "id": "highlight", "type": "text", "content": ["• ", { "path": "text" }] }]
}Page furniture
A running header carries the company and period, and the footer shows Page {n} of {total}. Both repeat on every page.
Make it robust
- Set
config.missingDatatowarnso a missing field is reported. - Check
result.diagnosticsafter every render. - Keep numbers numeric in the data. Presentation is the formatters' job.
Render and ship
const result = renderDocument(template, data, { target: 'print' });Print it from the browser with printHtml, or generate a PDF on a server. See the reports guide for more patterns.
Related: the matching feature page and the quick start.