Skip to main content
OfficeJS is the same in a page. Two things change: there is no file system, and you supply the fonts.

Edit only

If you only edit and save, import from @reductoai/docx/officejs. It needs no fonts and no layout.

Edit, lay out and render

For toPdf(), readPage() and updateFields(), import from @reductoai/docx/browser and install a layout host once:
browserLayoutHost sets the default host for every WordDocument in the page. The host keeps one layout for each document and lays out again only after an edit.

The layout engine

Layout runs in @reductoai/docx/engine.wasm, a file in the package. Vite, webpack and Next.js copy it into your build, so you set nothing. A page with an import map for @reductoai/docx/engine.wasm also needs nothing more. The engine draws EMF and WMF pictures itself. With another bundler, or to serve the file from another place, give its URL once, before the first layout:
To set it for one call, pass engine: browserLayoutHost({ fonts, engine: "/engine.wasm" }). layoutDocxInBrowser and createBrowserEditorHost take the same option. When the file is not at the URL, layout throws no layout engine with the URL that failed. esbuild does not copy the file. Load .wasm files as files and resolve the import against the bundle:
In TypeScript, declare the import: declare module "*.wasm" { const file: string; export default file; }. Cloudflare Workers import the file as a compiled module. Pass it to setLayoutEngine(engine), and set REDUCTO_API_KEY as a secret with the nodejs_compat flag. Start the first layout inside a request that waits for its result: a Worker cannot finish a promise that an ended request started. Content-Security-Policy. The page must allow WebAssembly: add 'wasm-unsafe-eval' to script-src. Without it the engine does not start and the error names the directive. Engine failures. If the engine fails while it opens or lays out a document (a WebAssembly trap, which Safari 18.0 sometimes hits), the call starts a fresh engine and tries once more, in the same Session. If that fails too, the call throws the first error. License and bad-file errors are not retried.

Host options

browserLayoutHost takes the same options as layoutDocxInBrowser:

Run it in a Web Worker

Layout of a long document takes time. Put OfficeJS in a worker so the page stays responsive:
The worker must be a module worker (type: "module").

Differences from Node