Renderer

HTML and CSS output for screen, print and pdf. Rendering and PDF in the DocStudio developer reference.

Public from docstudio/render: render, isRdt, escapeHtml, escapeText, styleCss, createStyleSheet and the types RenderOptions, RenderResult, RenderTarget. Decisions: ADR 0003, 0004 and 0018.

render(rdt, { target, title?, lang? }) returns { html, diagnostics }. It is pure and synchronous, uses no DOM or Node API, and is byte-deterministic. rdt may be any value; anything that is not a version 1 resolved document tree gives INVALID_RDT and an empty document.

Safety

All text and attribute values are escaped and stripped of control characters; newlines become <br>. Styles are emitted as deduplicated classes in one <style> element, never inline. Only whitelisted properties are serialized and each value is revalidated: colors are hex or letters only, font families are quoted names from a restricted character set, lengths are finite and bounded, enums match exactly. Rejected values are dropped and reported. There is no raw HTML path.

Units

Font size and letter spacing are points. All other lengths are millimetres. Sizes and grid tracks may also be auto, percent or fr strings. Padding and margin are logical: block start, inline end, block end, inline start. Page margins are physical.

Node rendering

Every RDT node kind except tables (R3) renders to one element with a hook class (ds-text, ds-stack, ...), a deduplicated style class and a data-ds-id attribute carrying the escaped node id.

Kind Output
text <p>; literal runs as escaped text, bound runs wrapped in <bdi>; newlines become <br>
image <img> with a data: URI built from resolver bytes, or an allowed https address; alt escaped
divider, shape <hr> and <div> using borders and background colors
stack, row, grid, section flex column, flex row, CSS grid (tracks in millimetres, auto, percent or fr) and <section>
fixed A relatively positioned box; direct children with a frame are placed absolutely in millimetres from the physical left and top, with rotation and z-index. Frames elsewhere are ignored
pageNumber A span with the format filled in for one page; target-specific handling follows in R4
qr Inline SVG generated without dependencies: byte mode, error correction level M, versions 1 to 10, up to 213 UTF-8 bytes
barcode Inline SVG: Code 128 (set B, checksum), Code 39 and EAN-13 (12 or 13 digits with check digit validation), up to 80 characters

Nodes that are not objects, have no kind or have an unknown kind are skipped with an INVALID_RDT warning. Nesting is limited to 64 levels and the node count to maxJsonNodes.

Images and assets

Images are referenced by asset:<id> or an https:// address. An AssetResolver supplied in the options maps identifiers to bytes with a MIME type, or to an https address. resolve may return undefined; if it throws the image is skipped without echoing the message. Bytes must match the declared type by content signature (PNG, JPEG, GIF, WebP, or an SVG document) and must not exceed maxImageBytes (default 5 MB); SVG is only ever embedded through <img>, so scripts in it cannot run. A raw https address is used only when the resolver's allowRemote returns true for it; addresses with credentials, quotes or whitespace are refused. Without a resolver every image is reported as ASSET_NOT_FOUND. Server-side network access rules (private address blocking) are the PDF service's job (R6).

Tables

A table node renders a real <table> with a <colgroup>, a <thead> header row, a <tbody class="ds-body"> with one row per RDT row, and footer rows in a separate <tbody class="ds-footer"> (not a <tfoot>, which Chromium repeats on every page; footer rows are totals and appear once, after the last body row).

  • Column width (fixed millimetres or percent up to 100) goes on the <col>; invalid widths are dropped. Column align (start, center, end) and the column style apply to the header and every cell.
  • Bound cells are wrapped in <bdi>; text is escaped and newlines become <br>.
  • Footer cell spans are clamped to the remaining columns; extra cells are dropped. Short rows are padded with empty cells.
  • repeatHeader (default true) keeps thead as a table header group so Chromium repeats it on every page; false renders it as an ordinary row group. zebra shades even body rows. rowBreak avoid (default) sets break-inside: avoid on rows; auto lets rows split.
  • With no rows and an emptyText, a single spanning message row is rendered; with neither, only the header.
  • A cell over 5,000 characters reports a LIMIT_EXCEEDED warning, because a row taller than a page is split by the browser regardless of break-inside (ADR 0003).
  • Rows and cells count toward the node limit.
  • Alignment and padding use logical properties, so right-to-left pages mirror the column order and alignment automatically.

The base stylesheet (box sizing, zero body margin, table defaults) is emitted first, then the deduplicated style classes.

Page, targets and fonts

render(rdt, { target, assets?, fonts?, title?, lang?, maxImageBytes? }) takes one of three targets. Page size, margins and background come from the RDT page; values are re-validated and fall back to A4 and 15 mm when out of range. Header and footer come from the RDT regions (height in millimetres, clamped to 0 to 500).

Target Output
screen A sheet (.ds-sheet) of the page size on a grey background, with margins as padding, header at the top and footer at the bottom, and page number formats shown for one page
print @page size and margins, backgrounds forced to print, and header and footer repeated in flow with a wrapper table (thead and tfoot as header and footer groups). A page number in a header or footer is not rendered in flow; its format becomes an @page margin box (@top-center or @bottom-center) using counter(page) and counter(pages), with literal text restricted to letters, digits, spaces and .,:()/-
pdf result.pdf: width, height and margins in millimetres with the header and footer heights added to the top and bottom margins, plus headerTemplate and footerTemplate strings for Chromium. The main document carries only the body and an @page rule with the reserved margins; use it with preferCSSPageSize

PDF templates are self-contained: a <style> element with their own classes and font faces, padding that places the content inside the reserved margin, and page numbers as <span class="pageNumber"> and <span class="totalPages"> (the classes Chromium fills in). Every family used in a template is also declared in the main document and preloaded by a hidden element, as Chromium otherwise falls back to a system font (ADR 0003). result.pdf.fonts lists the families used so the service can wait for them.

Fonts

createFontRegistry() returns a registry; registry.register({ family, format, data, weight?, style?, unicodeRange? }) validates and stores a face or returns a FONT_REJECTED diagnostic. Accepted: a family name of letters, digits, spaces, _ and - up to 64 characters; format woff2, woff, truetype or opentype with matching content signature; weight 1 to 1000; style normal or italic; a well-formed unicode-range; data up to 8 MB. The package ships no fonts; hosts register the faces they have licensed (open-licence Noto fonts are used in tests). Only faces whose family the document uses are emitted, once per document, as @font-face rules with data: URIs and font-display: block.

Nastaliq families (any family name containing "Nastaliq") get a line height of at least 2.0, as lines collide below that (ADR 0004). Naskh is the recommended Urdu default; the renderer does not choose fonts, the template or configuration does.

Right to left

html carries dir from the page direction and templates carry direction. Padding, margin, alignment and table padding use logical properties; bound values are isolated with <bdi>; fixed-region frames use physical left and top (ADR 0004).

Class names across regions

In the screen and print targets the body, header and footer share one style sheet, so a class name always means one set of declarations. In the pdf target the header and footer templates are separate documents that Chromium evaluates in one style scope, so their classes use the prefixes ds-h and ds-f. A page number node renders as a block element, so textAlign and width apply to it in the screen and pdf targets.