docstudio/render API reference
Every public export of docstudio/render in DocStudio 0.2.0: functions, constants, types and interfaces with signatures.
Import from docstudio/render.
Never throws. Problems are returned as diagnostics.
Functions
barcodeBars
Encodes text as barcode bars for code128, code39 or ean13; returns undefined when the value cannot be encoded.
barcodeBars(format: BarcodeFormat, value: string): Bars | undefinedbarcodeSvg
Draws barcode bars as an SVG string.
barcodeSvg(bars: Bars, label: string, startsWithBar?: boolean): stringbase64
Encodes bytes as base64 text.
base64(bytes: Uint8Array<ArrayBufferLike>): stringcreateFontRegistry
Creates a registry of font faces that are embedded into rendered documents.
createFontRegistry(): FontRegistrycreateStyleSheet
Creates a stylesheet builder that turns style declarations into short class names.
createStyleSheet(prefix?: string): StyleSheetencodeQr
Encodes text as a QR symbol; returns undefined for empty or too long text.
encodeQr(text: string): QrSymbol | undefinedescapeHtml
Escapes text for use in an HTML attribute.
escapeHtml(value: string): stringescapeText
Escapes text for use as HTML content.
escapeText(value: string): stringfontFaceCss
Builds the CSS font-face rules for the families a document uses.
fontFaceCss(registry: FontRegistry | undefined, families: ReadonlySet<string>): stringimageSource
Resolves an image reference through an asset resolver into a data URI, checking size and type.
imageSource(reference: string, resolver: AssetResolver | undefined, maxBytes: number): ImageSourcematchesMime
Checks that image bytes really are the declared media type.
matchesMime(mime: string, bytes: Uint8Array<ArrayBufferLike>): booleanqrSvg
Draws a QR symbol as an SVG string.
qrSvg(symbol: QrSymbol, label: string): stringrender
Renders a resolved document tree to HTML and CSS for the screen, print or pdf target.
render(rdt: unknown, options: RenderOptions): RenderResultimport { compile } from 'docstudio/core';
import { render, type RenderTarget } from 'docstudio/render';
export function run(target: RenderTarget): string {
const compiled = compile({
template: {
schemaVersion: '1.0.0',
page: {
footer: { height: 10, children: [{ id: 'pg', type: 'pageNumber', format: 'Page {n}' }] },
},
body: [{ id: 'hello', type: 'text', content: ['Hello'] }],
},
data: {},
});
if (compiled.tree === undefined) throw new Error('did not compile');
const rendered = render(compiled.tree, { target, title: 'Hello', lang: 'en' });
if (rendered.diagnostics.some((item) => item.severity === 'error')) {
throw new Error('the renderer reported errors');
}
return rendered.html;
}styleCss
Converts a resolved style into whitelisted CSS declarations.
styleCss(style: ResolvedStyle): StyleCssConstants
DEFAULT_MAX_IMAGE_BYTES
The default maximum size of one embedded image.
const DEFAULT_MAX_IMAGE_BYTES: numberMAX_FONT_BYTES
The maximum size of one font file.
const MAX_FONT_BYTES: numberQR_MAX_BYTES
The maximum number of bytes a QR symbol can hold in this implementation.
const QR_MAX_BYTES: 213RENDER_DEPENDS_ON
Declares that render depends only on core.
const RENDER_DEPENDS_ON: "ds:core"RENDER_MODULE_ID
The identifier of the render module.
const RENDER_MODULE_ID: "ds:render"Types
BarcodeFormat
export type BarcodeFormat = 'code128' | 'code39' | 'ean13';FontFormat
export type FontFormat = 'woff2' | 'woff' | 'truetype' | 'opentype';ImageSource
export type ImageSource =
| { readonly ok: true; readonly src: string }
| {
readonly ok: false;
readonly code: 'ASSET_NOT_FOUND' | 'ASSET_REJECTED';
readonly message: string;
};RenderTarget
export type RenderTarget = 'screen' | 'print' | 'pdf';ResolvedAsset
export type ResolvedAsset =
| { readonly kind: 'bytes'; readonly mime: string; readonly bytes: Uint8Array }
| { readonly kind: 'url'; readonly url: string };Interfaces
AssetResolver
export interface AssetResolver {
resolve(id: string): ResolvedAsset | undefined;
resolveUrl?(url: string): ResolvedAsset | undefined;
allowRemote?(url: string): boolean;
}Bars
export interface Bars {
readonly modules: number;
readonly runs: readonly number[];
}FontFace
export interface FontFace {
readonly family: string;
readonly weight?: number;
readonly style?: 'normal' | 'italic';
readonly format: FontFormat;
readonly data: Uint8Array;
readonly unicodeRange?: string;
}FontRegistry
export interface FontRegistry {
register(face: FontFace): Diagnostic | undefined;
faces(): readonly FontFace[];
}PdfLayout
export interface PdfLayout {
readonly width: number;
readonly height: number;
readonly margins: {
readonly top: number;
readonly right: number;
readonly bottom: number;
readonly left: number;
};
readonly headerTemplate: string | undefined;
readonly footerTemplate: string | undefined;
readonly fonts: readonly string[];
}QrSymbol
export interface QrSymbol {
readonly version: number;
readonly size: number;
readonly mask: number;
readonly modules: readonly (readonly boolean[])[];
readonly functions: readonly (readonly boolean[])[];
}RenderOptions
export interface RenderOptions {
readonly target: RenderTarget;
readonly title?: string;
readonly lang?: string;
readonly assets?: AssetResolver;
readonly maxImageBytes?: number;
readonly fonts?: FontRegistry;
}RenderResult
export interface RenderResult {
readonly html: string;
readonly diagnostics: readonly Diagnostic[];
readonly pdf: PdfLayout | undefined;
}StyleCss
export interface StyleCss {
readonly declarations: string;
readonly rejected: readonly string[];
}StyleSheet
export interface StyleSheet {
className(declarations: string): string;
css(): string;
}