Compare and optimise
Compare two PDF versions page by page, make files smaller by pruning objects and subsetting fonts, save for Fast Web View and watch memory with NexPDF for Node.js.
License module
Comparing documents, linearized saving, memoryUsage and trimMemory work without a key. optimize and
subsetFonts need the editor module (Startup tier and above).
Compare two versions
doc.raw.compareJson(other.raw, dpi) compares two documents page by page: whether the text changed
and how much of the rendered page differs. doc.raw.compareOverlayPng(other.raw, pageIndex, dpi)
draws the differences of one page as PNG bytes.
import { writeFile } from 'node:fs/promises';
import { PdfDocument } from '@nexdevit/nexpdf-node';
const a = await PdfDocument.open('v1.pdf');
const b = await PdfDocument.open('v2.pdf');
const diff = JSON.parse(a.raw.compareJson(b.raw, 72));
console.log(diff.pages_a, 'pages before,', diff.pages_b, 'after');
for (const page of diff.pages) {
if (page.text_changed || page.visual_diff > 0.001) {
console.log(`page ${page.page + 1}: ${(page.visual_diff * 100).toFixed(2)}% of the pixels changed`);
await writeFile(`diff-${page.page + 1}.png`, a.raw.compareOverlayPng(b.raw, page.page, 110));
}
}
a.close();
b.close();visual_diff is the share of differing pixels, from 0 to 1. A low resolution (72 dpi) is enough to
find changed pages; the overlay resolution is clamped to 18-300 dpi.
To see which words changed (inserted, deleted, replaced and moved text, and an annotated PDF of the changes), use Word-level comparison.
Make files smaller
optimize() compresses uncompressed streams and removes objects nothing refers to, and returns its
report as an object. doc.raw.subsetFonts() replaces embedded TrueType, OpenType and CFF fonts with
subsets of the glyphs the document uses, and returns a JSON string. Save afterwards.
const doc = await PdfDocument.open('large.pdf');
const r = doc.optimize();
console.log(r.streams_compressed, 'streams compressed,', r.objects_pruned, 'objects pruned,',
r.bytes_saved, 'bytes saved');
const f = JSON.parse(doc.raw.subsetFonts());
console.log(`${f.fonts_subset} fonts subset: ${f.bytes_before} -> ${f.bytes_after} bytes`);
await doc.save('small.pdf');Fonts that may still be needed for new text are left whole and counted in fonts_skipped.
Fast Web View
Linearized files let a browser show the first page before the rest downloads:
await doc.save('web.pdf', { linearized: true });
const webBytes = doc.toLinearizedBytes(); // the same bytes, e.g. for object storageMemory
memoryUsage() estimates the WebAssembly memory a document uses, and trimMemory() compacts the
document's internal buffers after large edits and returns the approximate number of bytes reclaimed:
console.log(Math.round(doc.memoryUsage() / 1024), 'KiB');
const freed = doc.trimMemory();
doc.close(); // frees everything the document holdsReclaimed memory is reused by the engine for later work; WebAssembly memory is not given back to the operating system while the engine is loaded. See Platform notes for threading and memory.