Skip to Content
NexPDF

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 in the browser with NexPDF for the web.

English

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 { loadNexPdf } from '@nexdevit/nexpdf-web/engine';

const nex = await loadNexPdf();
const a = await nex.PdfDocument.fromUrl('/v1.pdf');
const b = await nex.PdfDocument.fromUrl('/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`);
    const png = a.raw.compareOverlayPng(b.raw, page.page, 110);
    const img = document.createElement('img');
    img.src = URL.createObjectURL(new Blob([png], { type: 'image/png' }));
    document.body.append(img);
  }
}

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. Comparing renders every page of both documents, so run it in a Web Worker for long documents.

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

doc.raw.optimize() compresses uncompressed streams and removes objects nothing refers to. doc.raw.subsetFonts() replaces embedded TrueType, OpenType and CFF fonts with subsets of the glyphs the document uses. Both return a JSON report; save afterwards.

const r = JSON.parse(doc.raw.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`);

const small = doc.toBlob();

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, which is useful when you upload the result to a server that serves it:

const web = doc.toLinearizedBytes();
await fetch('/api/documents/42', { method: 'PUT', body: web });

Memory

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 holds

Reclaimed memory is reused by the engine for later work; WebAssembly memory is not given back to the browser while the engine is loaded. See Platform notes for how WebAssembly memory behaves.

On this page