Skip to Content
NexPDF

API reference

Every function, option and type of the NexPDF Electron SDK, written from index.d.ts and index.js of NexPDF 1.0.0.

English

NexPDF 1.0.0

@nexdevit/nexpdf-electron is a CommonJS module for the Electron main process. It exports five functions: openPdfWindow, startEngine, openDocument, documentBytes and findBinary. The reader itself runs in the engine; see the guides for what each mode does and the license module it needs.

const { openPdfWindow, startEngine, openDocument, documentBytes, findBinary } = require('@nexdevit/nexpdf-electron');

Windows

openPdfWindow

openPdfWindow(options?: PdfWindowOptions): Promise<PdfWindow>

Starts an engine, opens a BrowserWindow with the NexPDF reader on it and resolves once the page has loaded. Each window has its own engine; closing the window, or quitting the app, stops it.

The window:

  • is 1280 x 860 with the title NexPDF, unless options.window says otherwise;
  • runs with contextIsolation: true, sandbox: true and nodeIntegration: false. Extra webPreferences in options.window are merged over these;
  • stays on the engine's origin: links to other http and https origins open in the user's browser, and new windows are denied;
  • asks where to save every download, starting from the file's name.

Unless menu is false, it replaces the application menu with the app menu (macOS), a File menu, and the standard Edit, View and Window menus. The File menu has:

ItemShortcutWhat it does
Open…CmdOrCtrl+OOpens a PDF, PNG, JPEG, TIFF, DOCX, XLSX or PPTX file in the window
Save As…CmdOrCtrl+Shift+SWrites the current document to a PDF file
Close (macOS) or QuitCloses the window or quits

Errors from these items are shown in an error box.

PdfWindowOptions

OptionTypeDefaultMeaning
filestringnoneA PDF, picture or Office file to open at start
configstringnoneThe path of a JSON file with the UI configuration; see UI configuration
licensestringnoneThe license key, or the path of the XML license file
binarystringfound by findBinaryThe path of the nexpdf-webviewer engine
menubooleantrueInstall the File menu (Open, Save As)
windowBrowserWindowConstructorOptionsExtra BrowserWindow options

PdfWindow

MemberTypeMeaning
windowBrowserWindowThe Electron window
engineNexPdfEngineThe engine behind the window
open(file)(file: string) => Promise<void>Opens another file in the window and reloads it
save(file)(file: string) => Promise<void>Writes the current document, with every edit, to file
bytes()() => Promise<Buffer>The current document as PDF bytes
const pdf = await openPdfWindow({ file: 'contract.pdf', license, menu: false });
await pdf.open('appendix.pdf');
await pdf.save('appendix-edited.pdf');
pdf.window.setTitle('Appendix');

Engine

startEngine

startEngine(options?: EngineOptions): Promise<NexPdfEngine>

Starts the nexpdf-webviewer engine as a child process bound to 127.0.0.1 on a free port, and resolves when it reports its address. It rejects when the binary is not found, when the process exits early (with its output in the message), or after 15 seconds without starting. Use it without a window, for example for batch work in the main process, or to host the reader in a window of your own.

const engine = await startEngine({ license });
await openDocument(engine, fs.readFileSync('scan.pdf'), 'scan.pdf');
const bytes = await documentBytes(engine);
engine.close();

EngineOptions

OptionTypeMeaning
binarystringThe path of the engine; default: found by findBinary
licensestringThe license key, or the path of the XML license file (sets NEXPDF_LICENSE)
uiConfigstringThe path of a JSON file with the UI configuration (sets NEXPDF_UI_CONFIG)
filestringA document to open at start (sets NEXPDF_FILE to its absolute path)
envRecord<string, string>Extra environment variables for the engine

The engine gets the main process environment, then env, then NEXPDF_ADDR=127.0.0.1:0.

NexPdfEngine

MemberTypeMeaning
urlstringThe engine's loopback URL, for example http://127.0.0.1:53122
portnumberIts port
processChildProcessThe engine process
close()() => voidStops the engine

openDocument

openDocument(engine: NexPdfEngine, bytes: Uint8Array, fileName?: string): Promise<void>

Opens bytes (a Buffer or Uint8Array) in a running engine, in place of its current document. PDF, PNG, JPEG, TIFF, DOCX, XLSX and PPTX files are accepted. fileName defaults to document.pdf. It rejects with could not open <name>: HTTP <status> when the engine refuses the file, for example an encrypted PDF (no password is sent) or an Office file without the conversion module. A window on that engine shows the new document after a reload.

documentBytes

documentBytes(engine: NexPdfEngine): Promise<Buffer>

The engine's current document, with every edit, as PDF bytes.

findBinary

findBinary(explicit?: string): string

Returns the path of the engine, the first of these that exists:

  1. explicit;
  2. the NEXPDF_BINARY environment variable;
  3. process.resourcesPath/nexpdf/nexpdf-webviewer (nexpdf-webviewer.exe on Windows);
  4. bin/<platform>-<arch>/ inside the package;
  5. bin/ inside the package;
  6. target/release/ of the NexPDF source tree, when the package runs from it.

It throws NexPDF engine not found with the list of places it looked in.

Environment variables

The engine reads these from its environment. startEngine sets the first four from its options; pass the others in env, or set them in the main process before openPdfWindow.

VariableMeaning
NEXPDF_ADDRThe listening address; always 127.0.0.1:0 (a free port)
NEXPDF_LICENSEThe license key, or the path of the XML license file
NEXPDF_FILEA document to open at start
NEXPDF_UI_CONFIGThe path of a JSON file with the UI configuration
NEXPDF_APP_IDThe app id the license is checked against, default nexpdf-webviewer
ANTHROPIC_API_KEYLets the assistant write answers with page citations
NEXPDF_BINARYRead by findBinary, not the engine: the path of the engine

UI configuration

The reader takes the shared NexPDF UI configuration: modes, hidden items, your own buttons, tools, theme, accent colour and language. The keys are listed under Viewer in the web SDK, and the same file works for the web viewer.

const fs = require('node:fs');
const path = require('node:path');

const config = path.join(app.getPath('userData'), 'nexpdf-ui.json');
fs.writeFileSync(config, JSON.stringify({
  modeConfig: { availableModes: ['viewer', 'annotations', 'forms'] },
  toolbarConfig: { hiddenItems: ['share', 'language'] },
  globalConfig: { accentColor: '#0F766E', language: 'pt', brand: { title: 'Acme Docs' } },
}));
await openPdfWindow({ file, config });

index.d.ts types config and uiConfig as object | string, and an object is passed to the engine as JSON text. In 1.0.0 the engine reads NEXPDF_UI_CONFIG as a file path, so a configuration object is ignored. Write the configuration to a file and pass its path, as above.

Custom items (toolbarConfig.customItems) are shown, but the SDK has no channel to tell the main process that one was tapped: the reader sends customItemTapped to an embedding page, and the Electron window is not one. Use your own application menu for app actions.

On this page