Skip to Content
NexPDF

Opening and saving

Open PDFs, pictures, Office files and e-books in an Electron window with NexPDF, open encrypted files with a password, save the edited document, and work with documents in the main process with the document API.

English

License module

Opening PDFs and pictures, saving and downloading are part of the viewer module and work without a key. For Office files and e-books, see Conversion.

Open a document

The window opens a document in four ways:

WayCall
At startopenPdfWindow({ file, password })
From code, in an open windowpdf.open(file, { password })
By the userThe window's own Open button, or File, Open…
As bytes, into any engineopenDocument(engine, bytes, fileName, { password })
main.js
const { app } = require('electron');
const { openPdfWindow } = require('@nexdevit/nexpdf-electron');

app.whenReady().then(async () => {
  const pdf = await openPdfWindow({ file: '/path/to/contract.pdf' });
  await pdf.open('/path/to/appendix.pdf');           // replaces the document and reloads the window
});

Each engine holds one document, so opening another one replaces it. Open a second window for a second document.

openDocument sends bytes you already have, for example a file downloaded by your app. fileName (default document.pdf) is the name the window shows and the default name for downloads:

const { openDocument } = require('@nexdevit/nexpdf-electron');

const res = await fetch('https://intranet.example.com/contracts/42.pdf');
await openDocument(pdf.engine, Buffer.from(await res.arrayBuffer()), 'contract-42.pdf');
pdf.window.webContents.reload();                    // show it in the window

Formats

FormatOpened as
PDFThe document itself
PNG, JPEG, TIFFA PDF with one page per picture
Word, Excel, PowerPoint (.docx, .xlsx, .pptx)A PDF converted on open
EPUB, FictionBook, XPS/OpenXPS, CBZA PDF converted on open

The File, Open… dialog lists PDF, picture and Office files. E-books, XPS files and comics open with pdf.open(file) or openDocument. A converted document gets the name of the source file with .pdf.

Encrypted documents

Pass the user (open) password or the owner password (1.0.1 and later). RC4, AES-128 and AES-256 files are supported.

try {
  await pdf.open('/path/to/locked.pdf', { password: 's3cret' });
} catch (e) {
  if (e.passwordRequired) {
    console.log(e.wrongPassword ? 'wrong password' : 'a password is needed');
  } else {
    throw e;
  }
}

The window's own Open button asks the user for the password. File, Open… sends none, so encrypted files opened with it fail with an error box. See Error handling.

Save

WayWhat it does
pdf.save(file)Writes the current document, with every edit, to file
pdf.bytes() / documentBytes(engine)The current document, with every edit, as a Buffer
File, Save As…Asks where to save, then writes the PDF
The toolbar's DownloadAsks where to save, starting from the document's name
const { app } = require('electron');
const path = require('node:path');

await pdf.save(path.join(app.getPath('documents'), 'contract-edited.pdf'));
const bytes = await pdf.bytes();                   // e.g. to upload it

The More menu's Download for the web (Fast Web View) saves a linearized PDF, which shows its first page before the whole file has downloaded. pdf.save and pdf.bytes write the regular form.

Signed files

After the user signs, the engine keeps the signed file byte for byte, and pdf.save writes it unchanged. Any edit after signing rewrites the file and breaks the signature. See Signatures.

Without a window

startEngine with openDocument and documentBytes runs the same engine without a window, for example to convert a file the reader can open into a PDF:

const fs = require('node:fs');
const { startEngine, openDocument, documentBytes } = require('@nexdevit/nexpdf-electron');

const engine = await startEngine({ license });
try {
  await openDocument(engine, fs.readFileSync('proposal.docx'), 'proposal.docx');
  fs.writeFileSync('proposal.pdf', await documentBytes(engine));
} finally {
  engine.close();
}

The document API in the main process

1.1 adds documents() and openDocumentFile(): the document API of the Node.js SDK (@nexdevit/nexpdf-node) inside the main process, with no window and no helper process. Its engine is NexPDF compiled to WebAssembly, so it needs no native addon and runs wherever Electron does. Use it to render, read, search, annotate, fill, sign and save documents in code.

main.js
const fs = require('node:fs');
const { documents, openDocumentFile } = require('@nexdevit/nexpdf-electron');

const nex = documents();                       // loads the engine once
nex.activateLicense(fs.readFileSync('license.xml', 'utf8'), 'com.acme.docs');   // a `web` key

const doc = await openDocumentFile('/path/to/in.pdf', { password });   // a path or bytes
try {
  const png = doc.render(0, { dpi: 110 });     // PNG bytes
  const hits = doc.search('contract');
  doc.addAnnotation({ page: 0, kind: 'Highlight', rect: [72, 700, 300, 720] });
  await doc.save('/path/to/out.pdf');
} finally {
  doc.close();
}
CallReturns
documents()The Node.js SDK's API: PdfDocument, WasmDocument, activateLicense, licenseStatus, version, verifySignatures, pkcs12Info, addLtv, detectKind, externalSignatureData and completeExternalSignature
openDocumentFile(source, { password })A PdfDocument, the same as PdfDocument.open in the Node.js SDK

source is a path, a Uint8Array or an ArrayBuffer: a PDF, a PNG, JPEG or TIFF picture, a Word, Excel or PowerPoint file, an EPUB, FictionBook or XPS book, or a CBZ comic. A missing or wrong password rejects with passwordRequired (and wrongPassword) set, as openDocument does.

PdfDocument has camelCase methods with parsed results (render, text, search, annotations, fillField, sign, save, toBytes, …), and doc.raw is the WasmDocument with every function of the C SDK, taking and returning the same JSON strings. The guides for 1.1 features (templates, extraction, barcodes, XFA, comment threads, collaboration, comparison, long-term signatures, validation) use doc.raw. See the Node.js guides for every call.

The document API needs @nexdevit/nexpdf-node 1.1 installed next to this package (see Installation), and it has its own license check: activate a key issued for platform web with your app id as the host (see License). Opening, rendering, text, search and reading annotations work without a key.

Documents opened this way are independent of any window. To show one, write it with doc.toBytes() and send the bytes to a window with openDocument(pdf.engine, bytes, name), then reload the window as above.

On this page