compare-pdf-ts
v0.0.3
Published
In-memory PDF comparison tool designed for Node.js environments.
Readme
compare-pdf-ts
Node package that compares PDF files via pixel comparison. Largely inspired by the pdf-compare package, however this package differs in that it does everything in-memory without the need for reading/writing files.
Dependencies
This package requires the following dependencies in your project:
Setup
Install this package and required dependencies:
npm install compare-pdf-ts
npm install pdf-dist
npm install @napi-rs/canvasUsage
import { comparePdfs } from "compare-pdf-ts";
const pdf1 = readFileSync("./example1.pdf");
const pdf2 = readFileSync("./example2.pdf");
const { equal, diffs } = await comparePdfs(pdf1, pdf2);
if (!equal) {
diffs.forEach(({ diffPng, pageNumber }) => {
writeFileSync(`page-${pageNumber}-diff.png`, diffPng);
});
}Comparison Options
You can optionally pass some options when calling the comparePdfs function to tailor its behavior:
const { equal, diffs } = await comparePdfs(pdf1, pdf2, {
pngScale: 1.5
diffThreshold: 0.2
});The options type:
type ComparePdfsOptions = {
pngScale: number;
considerAntiAliasing: boolean;
includeDiffMask: boolean;
diffThreshold: number;
diffAlpha: number;
diffAntiAliasingColor: [number, number, number];
diffColor: [number, number, number];
diffColorAlt: [number, number, number];
};The default option values:
const DEFAULT_COMPARE_PDFS_OPTIONS: ComparePdfsOptions = {
pngScale: 1.0,
considerAntiAliasing: false,
includeDiffMask: false,
diffThreshold: 0.1,
diffAlpha: 0.1,
diffAntiAliasingColor: [255, 255, 0],
diffColor: [255, 0, 0],
diffColorAlt: [255, 0, 0],
};