compileDocument()
Compile a self-contained HTVG document to SVG. This function accepts a complete document with metadata and content in a single object.A complete HTVG document object or JSON string containing:
meta: Compilation options (width, height, fonts, etc.)content: The root element tree to render
Examples
Using JSON strings
You can also pass a JSON string directly:compile()
Compile an element tree with separate options. This is useful when you want to reuse the same element tree with different compilation settings.The root element tree to render. Can be an Element object or JSON string.
Compilation options object:
width(number, required): Output width in pixelsheight(number, optional): Output height in pixels (auto-computed if omitted)fontSize(number, optional): Default font size (default: 16)fontFamily(string, optional): Default font family for text elementsfonts(FontSource[], optional): Custom fonts to register
Same as
compileDocument() return valueExamples
With custom fonts
Error Handling
Both functions throw errors if:- WASM module is not initialized (call
init()first) - Invalid JSON format
- Invalid element structure
- Missing required fields
Performance Tips
- Call
init()once at application startup - Reuse element structures when possible
- Use
compile()when rendering the same content at different sizes - In serverless environments, initialize WASM outside the request handler when possible