Skip to Content
NexPDF

Platform notes

The main thread and Web Workers, memory, module size, bundlers and the limits of the WebAssembly engine in NexPDF for the web.

English

Main thread and workers

  • Engine calls are synchronous: render, text, ocr, toDocx and 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 activateLicense in every one of them. location.hostname is 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.
nexpdf.worker.ts
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();
  }
};
main.ts
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.
  • renderUrl creates an object URL each time. Revoke it with URL.revokeObjectURL when the image no longer needs it.
  • doc.memoryUsage() estimates the memory a document uses, and doc.trimMemory() compacts its internal buffers after large edits. Closing the document frees everything it holds.

Module size

FileSizeContents
nexpdf_bg.wasmabout 16 MBThe engine with the Liberation fonts compiled in
nexpdf_lite_bg.wasmabout 12 MBThe engine without fonts (register them at run time) and without the neural OCR engine
nexpdf.jsabout 150 KBThe 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 with new 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' in script-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.ocrLoadModel in 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 through fetch (doc.signOnline, nex.completeExternalSignatureOnline, nex.timestampSignature, nex.addLtvOnline, nex.timestampDocument, nex.upgradeSignature), and the browser's cross-origin rules apply to those requests. A tsa function 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 as nex.wasm, with nex.WasmDocument), usable once loadNexPdf() has resolved: templates (WasmDocument.templateDocxToPdf, templateHtmlToPdf, templateDocxFill, templateDocxFillWith), readBarcodesImageJson, the OCR model calls and the timestamp steps. C_FUNCTIONS maps each C function of nexpdf.h to 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).

On this page