Skip to main content
This page builds a read-only document viewer. There is no editor here: you lay out the document, then paint the layout on canvases.

1. Lay out

With a bundler other than Vite, webpack or Next.js, first give the engine’s URL. See The layout engine.
ir is a LayoutDocument: pages with positioned lines, runs, table cells, images and vector shapes. Everything is placed; a painter only draws. Do this in a worker for long documents. The IR is plain data, so postMessage can send it to the page.

2. Load fonts and images

loadFonts registers each face that the layout used as a FontFace, under a private name. Pass the provider’s bytes, or the canvas falls back to a local font and the text no longer fits the layout.

3. Paint one page

drawPage sizes the canvas buffer for the device pixel ratio. fitWidth is the CSS width; the aspect ratio stays the same. For thumbnails, use drawThumb(canvas, page, images, layers, { fitWidth: 160 }). Put the text layer on top of the canvas, inside a position: relative box.

4. Or use the scrolling viewer

DocumentPreviewer does the rest: a page stack that scrolls, zoom (fit width, fit page, or a percent), paint only of the pages in view, sharp tiles at any zoom, and pinch zoom.

5. Show page 1 before the layout ends

For long documents, paint pages as the layout releases them. Pass a pageSink:
With a sink, the result has pages: []; the pages went to the sink. In a worker, do the same split: call layoutDocxInBrowser with a sink in the worker, and post each page to the page with its manifest and the face bytes it needs (fonts.bytes(id) for each file in the manifest). On the page, call addFonts(manifest, bytesFor) before you paint. Send a face once; the page keeps it.

Track changes and comments in the viewer

Pass the view to the layout:
"balloons" scales each page and adds a markup margin, as Word prints with All Markup.

PowerPoint

@reductoai/pptx lays out a .pptx deck to the same LayoutDocument. The same viewer, PDF writer and PNG export work for slides. See /pptx/.