Platform notes
The main thread and Web Workers, memory, module size, bundlers and the limits of the WebAssembly engine in NexPDF for the web.
Main thread and workers
- Engine calls are synchronous:
render,text,ocr,toDocxand the rest return when the work is done, on the thread that called them. Promises appear only where something is fetched (loadNexPdf,fromFile,fromUrl,loadStandardFonts). - On the main thread a long call blocks the page: scrolling, clicks and animations wait. Rendering one page at screen resolution is usually fine there. Run OCR, conversions, signing and work on large documents in a Web Worker.
- The WebAssembly engine has no threads of its own. One engine runs one call at a time; for parallel work, start several workers.
- Each page load and each worker loads its own engine, with its own memory and its own license
state. Call
activateLicensein every one of them.location.hostnameis available in workers too, so the same call works. - A document lives in the memory of the engine that opened it and cannot be posted to another
worker. Send the file's bytes instead (as a transferable
ArrayBuffer) and open them there.
import { loadNexPdf } from '@nexdevit/nexpdf-web/engine';
const ready = loadNexPdf().then((nex) => {
nex.activateLicense(import.meta.env.VITE_NEXPDF_LICENSE, location.hostname);
return nex;
});
self.onmessage = async (e: MessageEvent<ArrayBuffer>) => {
const nex = await ready;
const doc = nex.PdfDocument.fromBytes(new Uint8Array(e.data));
try {
const report = doc.ocr({ dpi: 300 });
const pdf = doc.toBytes();
self.postMessage({ report, pdf }, [pdf.buffer]);
} finally {
doc.close();
}
};const worker = new Worker(new URL('./nexpdf.worker.ts', import.meta.url), { type: 'module' });
const bytes = await file.arrayBuffer();
worker.postMessage(bytes, [bytes]);
worker.onmessage = (e) => console.log(e.data.report.words, 'words recognised');Memory
- Opening a document copies the whole file into WebAssembly memory. Budget roughly the file size plus the decoded pages you touch.
- A rendered page at 144 dpi of an A4 page is about 1190 x 1684 pixels before PNG compression. Render the pages the user is looking at, not the whole document.
- The renderer keeps caches of parsed fonts, glyphs and decoded images, shared by every document in the same engine, so a logo or font used in many files is decoded once. Each cache is bounded by size and drops its least recently used entries first; together they can hold up to about 1.3 GB when many large, image-heavy files are rendered.
- WebAssembly memory grows when the engine needs more and is not given back to the browser while the
engine is loaded; memory freed by
close()is reused for the next documents. A long-lived tab that opened a very large file keeps that much memory until the page (or the worker) goes away. - Call
doc.close()when you are done with a document. Documents are also freed when garbage collected, but not at a predictable time. renderUrlcreates an object URL each time. Revoke it withURL.revokeObjectURLwhen the image no longer needs it.doc.memoryUsage()estimates the memory a document uses, anddoc.trimMemory()compacts its internal buffers after large edits. Closing the document frees everything it holds.
Module size
| File | Size | Contents |
|---|---|---|
nexpdf_bg.wasm | about 16 MB | The engine with the Liberation fonts compiled in |
nexpdf_lite_bg.wasm | about 12 MB | The engine without fonts (register them at run time) and without the neural OCR engine |
nexpdf.js | about 150 KB | The JavaScript glue |
- Serve the module with compression (gzip or Brotli) and a long cache lifetime; it changes only when you update the SDK.
- Load it when the user first needs it, not on every page: call
loadNexPdf()from the route or component that shows documents. - The lighter module is about 4 MB smaller. Documents that use non-embedded standard fonts and PDF/A
conversion then need
loadStandardFonts; see Installation.
Bundlers and frameworks
- The package entry points are TypeScript sources, compiled by your bundler.
loadNexPdf()finds the module withnew URL('nexpdf_bg.wasm', import.meta.url), which Vite and other modern bundlers turn into a URL of the emitted file. Otherwise pass the URL yourself.- The engine needs browser APIs. In Next.js, Nuxt and SvelteKit, use it from client-side code only.
- A Content Security Policy must allow WebAssembly compilation, for example
'wasm-unsafe-eval'inscript-src.
Differences from the native SDKs
The engine is the same as on every other platform, with these exceptions:
- OCR: Tesseract is not available. Neural OCR models load with
nex.wasm.ocrLoadModelin the standard module; the lighter module is built without the neural engine. See OCR. - Signing at B-B (
sign, external signers, test certificates) makes no network calls. B-T, B-LT, B-LTA and document timestamps go throughfetch(doc.signOnline,nex.completeExternalSignatureOnline,nex.timestampSignature,nex.addLtvOnline,nex.timestampDocument,nex.upgradeSignature), and the browser's cross-origin rules apply to those requests. Atsafunction takes the place of the native SDKs' TSA callback. See Long-term signatures. - Every function of the WebAssembly module is exported from
@nexdevit/nexpdf-web/engine(and asnex.wasm, withnex.WasmDocument), usable onceloadNexPdf()has resolved: templates (WasmDocument.templateDocxToPdf,templateHtmlToPdf,templateDocxFill,templateDocxFillWith),readBarcodesImageJson, the OCR model calls and the timestamp steps.C_FUNCTIONSmaps each C function ofnexpdf.hto its name.
See Platforms for every SDK.
Network
The engine makes no network calls of its own: license keys are verified offline and documents
never leave the device. The only requests are the ones you start: loadNexPdf fetching the module,
fromUrl fetching a document, loadStandardFonts fetching fonts, and the online signing calls
fetching time-stamps and OCSP, CRL and issuer data (through your fetch when you pass one).