Axonpack
@axonpdf/core

Reference

Every export, engine method, callback, tree field and error in @axonpdf/core.

Exports

ExportKind
createEngine()function
loadPageWidth()function
Enginetype
Callbackstype
RenderArgstype
RenderCallbacktype
EngineProfiletype

The WebAssembly module loads once per process, on the first call to createEngine or loadPageWidth. Node, browsers and Workers each get their own loader through the package's imports map, so browser bundles never include Node's built-in modules.

createEngine

createEngine(): Promise<Engine>

Loads the module if needed and returns a new engine. Each engine keeps its own fonts and images. Call free() when you're done with it.

loadPageWidth

loadPageWidth(): Promise<(props: object) => number>

Returns a function that measures a page from its props (size, orientation, dpi) the way the engine lays it out. The result is the width in points, or NaN for a size the engine rejects.

Engine

MethodWhat it does
addFont(family, weight, italic, bytes)Registers a TTF, OTF, WOFF or WOFF2 font as one face of family. Throws Font collection is not supported for a TTC file and Unknown font format for anything else it can't read.
addImage(id, bytes)Registers a PNG, JPEG, GIF, WebP or SVG image under id. The tree refers to it as { "asset": id }. Throws Invalid image: <why> when the bytes can't be read.
render(treeJson, onChunk, callbacks?)Renders the tree in one call. onChunk gets each piece of the PDF in order, as pages finish. treeJson can be a function that returns the string, so the string can be freed while the engine works.
begin(treeJson, onChunk, callbacks?)Starts a render that stops whenever a Rows node needs its next batch. Returns that node's id, or undefined when the file is complete. Leave callbacks.rows out.
pushRows(id, batchJson)Gives the Rows node id its next batch, {"children": [...], "done": bool}, and carries on. Returns the same as begin.
hyphenateBuiltin(word)Splits a word at its soft hyphens, or else with the English patterns.
pages()The width and height in points of each page the current or last render wrote.
profile(on)Turns the engine's phase timing on (from zero) or off, for every engine in the process.
profileReport()What the timing measured: an EngineProfile.
memoryBytes()The size of the WebAssembly memory in bytes. It only grows, so this is also its peak.
free()Frees the engine. It can't be used afterwards.

Callbacks

Functions the engine calls back by id while it renders. Each one returns JSON.

FieldCalled withReturns
render(id, args: RenderArgs){"children": [...], "readsTotal": bool}
paint(id, width, height): the Canvas content box, undefined while only measuringthe painter calls as a JSON list
hyphenate(id, word)the parts as a JSON list of strings
rows(id)the next batch, {"children": [...], "done": bool}

An error thrown in a callback is held until the engine returns, then thrown from render, begin or pushRows.

RenderArgs

FieldTypeNotes
pageNumbernumberAlways set.
totalPagesnumber | string | undefinedundefined until the page count is known. In a document with Rows it is a marker string that text can show; the engine swaps it for the number when the file is finished.
subPageNumbernumber | undefinedThe page's number within its <Page>.
subPageTotalPagesnumber | undefinedHow many pages its <Page> made.
isLastboolean | undefinedWhether this is the last page; known only in the final call for a page.

EngineProfile

Milliseconds spent in each engine phase while timing was on: parse, layout, text, paint, write and callbacks (your functions that the engine called), plus pages, each page's time to its last byte.

The tree

The tree is JSON with "v": 1 and a DOCUMENT root whose children are PAGE nodes. Each node is { "type", "props", "style", "children" }, with text as { "type": "TEXT_INSTANCE", "value" }. Styles travel as written (objects, nested arrays, units as strings, @media keys) and the engine resolves them. @axonpdf/react documents the components and props the types stand for.

Document props the engine reads beyond the ones the components take:

PropMeaning
compressfalse writes the content, font and drawing streams undeflated.
encryptionSeed32 random bytes as an array of numbers. The engine has no random source, so a binding passes these when the document has a password.
countPagestrue lays the pages out once to count them before drawing them.
emoji{"<emoji>": {"asset": id}}, the image for each emoji in the text.
hyphenationCallbackThe id of the document-wide hyphenation function.

Errors

MessageWhen
unsupported document version <n>The tree's v isn't 1.
the root must be a DOCUMENT, not <type>The root node has another type.
Invalid page size: <size>A page size isn't a known name or a length.
Invalid border radius: <value>The borderRadius shorthand was given something other than a length, such as '50%'.
Font family not registered: <family>. Please register it calling Font.register() method.Text names a family no font was added for.
Could not resolve font for <family>, fontWeight <n>, fontStyle <style>The family has no face in that style.
a render function was used but no render callback is setThe tree has a render id and callbacks.render is missing.
[layout] A page layout must render its children exactly once (found <n>).A page layout template returned no slot, or more than one.

The engine writes warnings to the console: Node of type <TYPE> can't wrap between pages and it's bigger than available page height for an unbreakable node taller than the page, and skipping a node the engine cannot read: <why> for a node it can't parse.

On this page