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.
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, unlessoptions.windowsays otherwise; - runs with
contextIsolation: true,sandbox: trueandnodeIntegration: false. ExtrawebPreferencesinoptions.windoware merged over these; - stays on the engine's origin: links to other
httpandhttpsorigins 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:
| Item | Shortcut | What it does |
|---|---|---|
| Open… | CmdOrCtrl+O | Opens a PDF, PNG, JPEG, TIFF, DOCX, XLSX or PPTX file in the window |
| Save As… | CmdOrCtrl+Shift+S | Writes the current document to a PDF file |
| Close (macOS) or Quit | Closes the window or quits |
Errors from these items are shown in an error box.
PdfWindowOptions
| Option | Type | Default | Meaning |
|---|---|---|---|
file | string | none | A PDF, picture or Office file to open at start |
config | string | none | The path of a JSON file with the UI configuration; see UI configuration |
license | string | none | The license key, or the path of the XML license file |
binary | string | found by findBinary | The path of the nexpdf-webviewer engine |
menu | boolean | true | Install the File menu (Open, Save As) |
window | BrowserWindowConstructorOptions | Extra BrowserWindow options |
PdfWindow
| Member | Type | Meaning |
|---|---|---|
window | BrowserWindow | The Electron window |
engine | NexPdfEngine | The 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
| Option | Type | Meaning |
|---|---|---|
binary | string | The path of the engine; default: found by findBinary |
license | string | The license key, or the path of the XML license file (sets NEXPDF_LICENSE) |
uiConfig | string | The path of a JSON file with the UI configuration (sets NEXPDF_UI_CONFIG) |
file | string | A document to open at start (sets NEXPDF_FILE to its absolute path) |
env | Record<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
| Member | Type | Meaning |
|---|---|---|
url | string | The engine's loopback URL, for example http://127.0.0.1:53122 |
port | number | Its port |
process | ChildProcess | The engine process |
close() | () => void | Stops 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): stringReturns the path of the engine, the first of these that exists:
explicit;- the
NEXPDF_BINARYenvironment variable; process.resourcesPath/nexpdf/nexpdf-webviewer(nexpdf-webviewer.exeon Windows);bin/<platform>-<arch>/inside the package;bin/inside the package;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.
| Variable | Meaning |
|---|---|
NEXPDF_ADDR | The listening address; always 127.0.0.1:0 (a free port) |
NEXPDF_LICENSE | The license key, or the path of the XML license file |
NEXPDF_FILE | A document to open at start |
NEXPDF_UI_CONFIG | The path of a JSON file with the UI configuration |
NEXPDF_APP_ID | The app id the license is checked against, default nexpdf-webviewer |
ANTHROPIC_API_KEY | Lets the assistant write answers with page citations |
NEXPDF_BINARY | Read 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.