Rendering a document
Render a DocStudio template to HTML with renderDocument or with compile and render, choose a target, and read the diagnostics when something is wrong.
renderDocument
import { renderDocument } from 'docstudio';
const result = renderDocument(template, data, {
target: 'print',
title: 'Invoice INV-1042',
lang: 'en',
});Options:
| Option | Meaning |
|---|---|
target |
screen, print or pdf |
title |
The document title |
lang |
The document language, for example ar |
assets |
A resolver that turns asset: identifiers into image bytes |
fonts |
A font registry created with createFontRegistry from docstudio/render |
maxImageBytes |
Upper bound for one image |
compile |
Compile options such as limits, config, formatters and components |
The result is { ok, html, diagnostics }.
Diagnostics
Every problem comes back as a diagnostic: a code, a severity of error or warning, a message and a JSON Pointer path into the template. A template that cannot be compiled returns ok: false and an empty html.
import { compile } from 'docstudio/core';
const result = compile({
template: {
schemaVersion: '1.0.0',
config: { missingData: 'warn' },
body: [{ id: 'who', type: 'text', content: [{ path: 'customer.name' }] }],
},
data: {},
});
const warnings = result.diagnostics.filter((item) => item.severity === 'warning');config.missingData decides what a missing value does: blank, placeholder, warn or error.
Customize the pipeline
Pass compile options to register your own formatters, supply a component store for reusable components or lower the resource limits:
import { createFormatterRegistry, registerFormatter } from 'docstudio/core';
const formatters = createFormatterRegistry({ locale: 'en' });
registerFormatter(formatters, 'mrn', (value) =>
typeof value === 'string' ? `MRN-${value.padStart(8, '0')}` : undefined,
);
const result = renderDocument(template, data, { compile: { formatters } });Then use it in a template with { "fn": "format", "args": ["mrn", { "path": "patient.id" }] }.