DEV Community

Ezra Winslow
Ezra Winslow

Posted on Originally published at pdffields.com

How to Compare Two PDFs for Differences Locally (Word Redline + Pixel Diff Without Cloud Uploads)

When a vendor, landlord, or counterparty emails back a revised PDF saying "just a few minor edits," checking the changes usually forces an awkward trade-off: pay $239.88/year for Adobe Acrobat Pro, manually squint at two windows side by side, or upload confidential NDAs and financial exhibits to a free cloud PDF diff website.

Even worse, many popular online PDF diff checkers only extract raw text. If someone modifies a scanned invoice figure, swaps a signature block, alters a diagram, or shifts table alignment without changing the text layer, a text-only diff reports zero differences.

Combining word-level LCS redline diffing with canvas RGBA pixel-overlay diffing directly inside the browser solves both problems with zero server uploads.

Why Text-Only PDF Diff Misses Real Contract Changes

A PDF file can change in two independent ways:

  1. Text stream edits: Dollar amounts ($12,500 to $14,800), payment windows (Net 30 to Net 60), liability caps ($250,000 to $50,000), governing law (California to Delaware), or inserted clauses. These require a structured word-level redline and clickable change log so reviewers can audit every single phrase.
  2. Visual and raster edits: Scanned pages with no text layer, embedded charts, signature images, stamps, or subtle numeric edits baked into flat images. Catching those requires rendering both pages to identical offscreen canvases and comparing every RGBA pixel.

Building Both Engines 100% Locally in TypeScript

Modern browsers can run the entire pipeline on the user's device via pdf.js and pdf-lib with zero bytes leaving the browser tab.

1. Reading-Order Text Extraction + Fast Word LCS Diff

Raw pdf.js text items arrive in PDF stream order, which is not always reading order. Grouping items by vertical baseline (y descending within a 4pt tolerance) and sorting horizontally (x ascending) reconstructs clean paragraphs before running prefix/suffix-trimmed word-level Longest Common Subsequence (LCS) diffing:

export function diffWordTexts(leftText: string, rightText: string) {
  const a = leftText.trim().split(/\s+/).filter(Boolean);
  const b = rightText.trim().split(/\s+/).filter(Boolean);

  let prefix = 0;
  const minLen = Math.min(a.length, b.length);
  while (prefix < minLen && a[prefix] === b[prefix]) prefix++;

  let suffix = 0;
  while (
    suffix < minLen - prefix &&
    a[a.length - 1 - suffix] === b[b.length - 1 - suffix]
  ) {
    suffix++;
  }

  // Only run DP on the genuinely changed middle slice
  return computeMiddleLcs(
    a.slice(prefix, a.length - suffix),
    b.slice(prefix, b.length - suffix)
  );
}
Enter fullscreen mode Exit fullscreen mode

Trimming matching prefixes and suffixes first means even a 20-page contract with 6 localized edits diffs in milliseconds without blocking the UI thread.

2. Pixel-Level Visual Overlay Diff

For visual verification, render Page N of both PDFs onto matching offscreen HTML <canvas> elements (normalizing canvas width and height if page sizes differ), then inspect each RGBA pixel pair:

const delta =
  Math.abs(lr - rr) + Math.abs(lg - rg) + Math.abs(lb - rb);

if (delta > 45) {
  const leftLum = 0.299 * lr + 0.587 * lg + 0.114 * lb;
  const rightLum = 0.299 * rr + 0.587 * rg + 0.114 * rb;
  if (leftLum < rightLum) {
    // Removed original ink -> Redaction Crimson (#C8232C)
  } else {
    // Added revised ink -> Blueprint Cobalt (#1652E1)
  }
} else {
  // Unchanged background -> desaturated neutral gray so redlines stand out
}
Enter fullscreen mode Exit fullscreen mode

The threshold delta > 45 filters out subpixel antialiasing noise while immediately lighting up altered digits, moved clauses, swapped signatures, and scanned invoice edits.

Walkthrough Video

Watch the 45-second browser-local walkthrough: https://www.youtube.com/watch?v=EJfb1h18AbM

Try It Free on Your Own Two PDFs (Zero Uploads)

We shipped this complete Text Redline Diff, Visual Pixel Overlay, Side-by-Side Viewer, and Redline Audit Report PDF generator at PDFFields Compare Two PDFs.

  • Free full inspection on your own PDFs: Drop two PDF versions (or click the built-in 2-page Master Services Agreement sample pair with 6 hidden redlines) and inspect every word change, clickable change log entry, and visual pixel overlay locally with zero file uploads.
  • Export clean Redline Report PDF & CSV: Generate a clean audit report PDF or structured .CSV change log to share with legal, finance, or procurement stakeholders.
  • Need to lock or audit the final signed contract too? Use our local Flatten PDF & Metadata Scrubber and PDF Redaction Leak Checker directly in the same browser-local workspace.

Originally published at pdffields.com/compare-pdf.

Top comments (0)