Quick start
Render your first document with DocStudio in a few lines of TypeScript: a template, some data, and one renderDocument call that returns HTML.
This page renders a document from a template and data. It needs only npm install docstudio.
One call
import { renderDocument } from 'docstudio';
const template = {
schemaVersion: '1.0.0',
body: [{ id: 'hello', type: 'text', content: ['Hello ', { path: 'name' }] }],
};
const result = renderDocument(template, { name: 'Ada' }, { target: 'print', title: 'Greeting' });
if (!result.ok) {
throw new Error(result.diagnostics.map((item) => item.message).join('; '));
}
console.log(result.html);renderDocument returns { ok, html, diagnostics }. It does not throw for template or data problems. When ok is false, read diagnostics.
The same thing in two steps
Use compile and render when you want the resolved tree, for example to send it to a PDF service:
import { compile } from 'docstudio/core';
import { render } from 'docstudio/render';
const compiled = compile({ template, data: { name: 'Ada' } });
if (compiled.tree === undefined) throw new Error(JSON.stringify(compiled.diagnostics));
const { html } = render(compiled.tree, { target: 'screen' });compile returns { ok, tree, diagnostics, meta, trace }. tree is the resolved document tree: plain JSON with no bindings, conditions or repeaters left.
The three targets
| Target | Output |
|---|---|
screen |
A paper sheet on a grey background, used for previews |
print |
HTML with @page size and margins, for the browser print dialog |
pdf |
HTML prepared for the PDF service |