Skip to Content
NexPDF

Platform notes

The event loop and worker threads, memory, packaging, serverless and the limits of the WebAssembly engine in NexPDF for Node.js.

English

Event loop and worker threads

  • Engine calls are synchronous and run on the calling thread. PdfDocument.open and save are async only because they read and write files. While render, ocr or toDocx runs, the event loop waits: other requests in the same process are not served.
  • The WebAssembly engine has no threads of its own. One engine runs one call at a time.
  • For parallel work, use worker_threads or several processes. Each worker and each process loads its own engine, with its own memory and its own license state; call activateLicense in every one of them.
  • A document lives in the memory of the engine that opened it and cannot be passed to another worker. Pass file paths or bytes (a transferable ArrayBuffer) instead.
render-worker.mjs
import { parentPort } from 'node:worker_threads';
import { activateLicense, PdfDocument } from '@nexdevit/nexpdf-node';

activateLicense(process.env.NEXPDF_LICENSE_KEY, process.env.NEXPDF_APP_ID);

parentPort.on('message', async (path) => {
  const doc = await PdfDocument.open(path);
  try {
    const png = doc.render(0, { dpi: 144 });
    parentPort.postMessage(png, [png.buffer]);
  } finally {
    doc.close();
  }
});
main.mjs
import { Worker } from 'node:worker_threads';

const worker = new Worker(new URL('./render-worker.mjs', import.meta.url));
worker.postMessage('contract.pdf');
worker.once('message', (png) => console.log(png.length, 'bytes of PNG'));

A pool of a few long-lived workers (for example one per CPU core) runs independent files in parallel.

Start-up

Importing the package reads wasm/nexpdf_bg.wasm and compiles it synchronously, before your first call. It takes a noticeable moment on every cold start and in every new worker, so import the package once at start-up and keep workers alive between jobs rather than starting one per file.

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 large documents page by page instead of keeping every PNG.
  • 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 on an engine that renders many large, image-heavy files.
  • WebAssembly memory grows when the engine needs more and is not given back to the operating system while the engine is loaded; memory freed by close() is reused for the next documents. A long-running process that once opened a very large file keeps that much memory. When that matters, process large files in a worker and end the worker afterwards.
  • Call doc.close() when you are done with a document. Documents are also freed when garbage collected, but not at a predictable time.
  • doc.memoryUsage() estimates the memory a document uses, and doc.trimMemory() compacts its internal buffers after large edits.

Packaging and size

  • The package is plain JavaScript plus one WebAssembly file, wasm/nexpdf_bg.wasm (more than 10 MB). The same file runs on every operating system and CPU architecture Node.js supports.
  • The module compiles in the Liberation fonts used for non-embedded standard fonts and PDF/A.
  • Bundlers must not separate wasm/nexpdf.js from wasm/nexpdf_bg.wasm: the first reads the second from its own folder. Mark @nexdevit/nexpdf-node as external and ship it in node_modules.
  • For Docker images, see Installation. Any base image with Node.js 18 or later works; there is no native library and no glibc requirement.

Serverless

Function runtimes work like any other Node.js host. Ship the package with its wasm/ folder, import it and call activateLicense once at module level, outside the handler, so warm invocations reuse the engine. Work with bytes (PdfDocument.fromBytes and toBytes) to stay in memory, or use /tmp for files. Give the function enough memory for the documents you process, and a timeout long enough for OCR.

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 ocrLoadModel, and ocr() with engine: 'auto' uses them. See OCR.
  • Signing at B-B (sign, addLtv, external signers, test certificates) makes no network calls. B-T, B-LT, B-LTA and document timestamps go through fetch (doc.signOnline, completeExternalSignatureOnline, timestampSignature, addLtvOnline, timestampDocument, upgradeSignature). A tsa function takes the place of the native SDKs' TSA callback. See Long-term signatures.
  • Every function of the WebAssembly module is exported: templateDocxFill, templateDocxFillWith, readBarcodesImageJson, the OCR model calls (ocrLoadModel, ocrModelsJson, ocrUnloadModels), the timestamp steps and every static method of WasmDocument. C_FUNCTIONS maps each C function of nexpdf.h to its name.

See Platforms for every SDK.

Network

The engine makes no network calls: license keys are verified offline and documents never leave the process. Requests are made only by NexPdfClient from @nexdevit/nexpdf-node/client and the NexPdfSync client (the WebViewer server you point them at), and by the online signing calls, which fetch time-stamps and OCSP, CRL and issuer data (through your fetch when you pass one).

On this page