pdf-visual-compare 3.0.1 → 3.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,11 @@
1
- import { ComparePdfOptions } from './types/ComparePdfOptions.js';
1
+ import type { ComparePdfOptions } from './types/ComparePdfOptions.js';
2
+ /**
3
+ * Compares two PDF files or buffers and returns a boolean indicating whether they are similar.
4
+ *
5
+ * @param actualPdf - The file path or buffer of the actual PDF to compare.
6
+ * @param expectedPdf - The file path or buffer of the expected PDF to compare against.
7
+ * @param opts - Optional comparison options.
8
+ * @returns A promise that resolves to a boolean indicating whether the PDFs are similar.
9
+ * @throws Will throw an error if the compare threshold is less than 0.
10
+ */
2
11
  export declare function comparePdf(actualPdf: string | Buffer, expectedPdf: string | Buffer, opts?: ComparePdfOptions): Promise<boolean>;
3
- //# sourceMappingURL=comparePdf.d.ts.map
package/out/comparePdf.js CHANGED
@@ -1,56 +1,71 @@
1
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
2
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
3
- return new (P || (P = Promise))(function (resolve, reject) {
4
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
5
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
6
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
7
- step((generator = generator.apply(thisArg, _arguments || [])).next());
8
- });
9
- };
10
1
  import { existsSync } from 'node:fs';
11
2
  import { resolve } from 'node:path';
12
3
  import { pdfToPng } from 'pdf-to-png-converter';
13
4
  import { comparePng } from 'png-visual-compare';
14
5
  import { DEFAULT_DIFFS_FOLDER } from './const.js';
15
- export function comparePdf(actualPdf_1, expectedPdf_1) {
16
- return __awaiter(this, arguments, void 0, function* (actualPdf, expectedPdf, opts = {}) {
17
- var _a, _b, _c;
18
- validateInputFileType(actualPdf);
19
- validateInputFileType(expectedPdf);
20
- const pdfToPngConvertOpts = Object.assign({}, opts.pdfToPngConvertOptions);
21
- if (!pdfToPngConvertOpts.viewportScale) {
22
- pdfToPngConvertOpts.viewportScale = 2.0;
23
- }
24
- if (!pdfToPngConvertOpts.outputFileMaskFunc) {
25
- pdfToPngConvertOpts.outputFileMaskFunc = (pageNumber) => `comparePdf_${pageNumber}.png`;
26
- }
27
- const diffsOutputFolder = (_a = opts === null || opts === void 0 ? void 0 : opts.diffsOutputFolder) !== null && _a !== void 0 ? _a : DEFAULT_DIFFS_FOLDER;
28
- const compareThreshold = (_b = opts === null || opts === void 0 ? void 0 : opts.compareThreshold) !== null && _b !== void 0 ? _b : 0;
29
- const excludedAreas = (_c = opts === null || opts === void 0 ? void 0 : opts.excludedAreas) !== null && _c !== void 0 ? _c : [];
30
- if (compareThreshold < 0) {
31
- throw Error('Compare Threshold cannot be less than 0.');
32
- }
33
- let [actualPdfPngPages, expectedPdfPngPages] = yield Promise.all([
34
- pdfToPng(actualPdf, pdfToPngConvertOpts),
35
- pdfToPng(expectedPdf, pdfToPngConvertOpts),
36
- ]);
37
- if (actualPdfPngPages.length < expectedPdfPngPages.length) {
38
- [actualPdfPngPages, expectedPdfPngPages] = [expectedPdfPngPages, actualPdfPngPages];
6
+ /**
7
+ * Compares two PDF files or buffers and returns a boolean indicating whether they are similar.
8
+ *
9
+ * @param actualPdf - The file path or buffer of the actual PDF to compare.
10
+ * @param expectedPdf - The file path or buffer of the expected PDF to compare against.
11
+ * @param opts - Optional comparison options.
12
+ * @returns A promise that resolves to a boolean indicating whether the PDFs are similar.
13
+ * @throws Will throw an error if the compare threshold is less than 0.
14
+ */
15
+ export async function comparePdf(actualPdf, expectedPdf, opts = {}) {
16
+ // Validate input file types
17
+ validateInputFileType(actualPdf);
18
+ validateInputFileType(expectedPdf);
19
+ // Set default options
20
+ const pdfToPngConvertOpts = { ...opts.pdfToPngConvertOptions };
21
+ if (!pdfToPngConvertOpts.viewportScale) {
22
+ pdfToPngConvertOpts.viewportScale = 2.0;
23
+ }
24
+ if (!pdfToPngConvertOpts.outputFileMaskFunc) {
25
+ pdfToPngConvertOpts.outputFileMaskFunc = (pageNumber) => `comparePdf_${pageNumber}.png`;
26
+ }
27
+ const diffsOutputFolder = opts?.diffsOutputFolder ?? DEFAULT_DIFFS_FOLDER;
28
+ const compareThreshold = opts?.compareThreshold ?? 0;
29
+ const excludedAreas = opts?.excludedAreas ?? [];
30
+ if (compareThreshold < 0) {
31
+ throw Error('Compare Threshold cannot be less than 0.');
32
+ }
33
+ // Convert PDFs to PNGs
34
+ let [actualPdfPngPages, expectedPdfPngPages] = await Promise.all([
35
+ pdfToPng(actualPdf, pdfToPngConvertOpts),
36
+ pdfToPng(expectedPdf, pdfToPngConvertOpts),
37
+ ]);
38
+ // Ensure actualPdfPngPages is always the longer array to avoid index out of bounds errors
39
+ if (actualPdfPngPages.length < expectedPdfPngPages.length) {
40
+ [actualPdfPngPages, expectedPdfPngPages] = [expectedPdfPngPages, actualPdfPngPages];
41
+ }
42
+ let documentCompareResult = true;
43
+ actualPdfPngPages.forEach((pngPage, index) => {
44
+ const comparePngOpts = {
45
+ ...opts?.pdfToPngConvertOptions,
46
+ ...excludedAreas[index],
47
+ throwErrorOnInvalidInputData: false,
48
+ };
49
+ comparePngOpts.diffFilePath = resolve(diffsOutputFolder, `diff_${pngPage.name}`);
50
+ const pngPageOutputToCompareWith = expectedPdfPngPages.find((p) => p.name === pngPage.name);
51
+ const pageCompareResult = comparePng(pngPage.content, pngPageOutputToCompareWith?.content ?? '', comparePngOpts);
52
+ if (pageCompareResult > compareThreshold) {
53
+ documentCompareResult = false;
39
54
  }
40
- let documentCompareResult = true;
41
- actualPdfPngPages.forEach((pngPage, index) => {
42
- var _a;
43
- const comparePngOpts = Object.assign(Object.assign(Object.assign({}, opts === null || opts === void 0 ? void 0 : opts.pdfToPngConvertOptions), excludedAreas[index]), { throwErrorOnInvalidInputData: false });
44
- comparePngOpts.diffFilePath = resolve(diffsOutputFolder, `diff_${pngPage.name}`);
45
- const pngPageOutputToCompareWith = expectedPdfPngPages.find((p) => p.name === pngPage.name);
46
- const pageCompareResult = comparePng(pngPage.content, (_a = pngPageOutputToCompareWith === null || pngPageOutputToCompareWith === void 0 ? void 0 : pngPageOutputToCompareWith.content) !== null && _a !== void 0 ? _a : '', comparePngOpts);
47
- if (pageCompareResult > compareThreshold) {
48
- documentCompareResult = false;
49
- }
50
- });
51
- return documentCompareResult;
52
55
  });
56
+ return documentCompareResult;
53
57
  }
58
+ /**
59
+ * Validates the type of the input file. The input file can either be a Buffer or a string representing a file path.
60
+ * If the input file is a Buffer, the function returns without any error.
61
+ * If the input file is a string, the function checks if the file exists at the given path.
62
+ * If the file does not exist, an error is thrown.
63
+ * If the input file is neither a Buffer nor a string, an error is thrown.
64
+ *
65
+ * @param inputFile - The input file to validate. It can be a Buffer or a string representing a file path.
66
+ * @throws {Error} If the input file is a string and the file does not exist.
67
+ * @throws {Error} If the input file is neither a Buffer nor a string.
68
+ */
54
69
  function validateInputFileType(inputFile) {
55
70
  if (Buffer.isBuffer(inputFile)) {
56
71
  return;
@@ -65,4 +80,3 @@ function validateInputFileType(inputFile) {
65
80
  }
66
81
  throw Error(`Unknown input file type.`);
67
82
  }
68
- //# sourceMappingURL=comparePdf.js.map
package/out/const.d.ts CHANGED
@@ -1,2 +1 @@
1
1
  export declare const DEFAULT_DIFFS_FOLDER: string;
2
- //# sourceMappingURL=const.d.ts.map
package/out/const.js CHANGED
@@ -1,3 +1,2 @@
1
1
  import { resolve } from 'node:path';
2
2
  export const DEFAULT_DIFFS_FOLDER = resolve(`./comparePdfOutput`);
3
- //# sourceMappingURL=const.js.map
package/out/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
1
  export { comparePdf } from './comparePdf.js';
2
- export { PdfToPngOptions } from 'pdf-to-png-converter';
3
- export { Area, Color } from 'png-visual-compare';
4
- export { ComparePdfOptions } from './types/ComparePdfOptions.js';
5
- export { ExcludedPageArea } from './types/ExcludedPageArea.js';
6
- //# sourceMappingURL=index.d.ts.map
2
+ export type { ComparePdfOptions } from './types/ComparePdfOptions.js';
3
+ export type { ExcludedPageArea } from './types/ExcludedPageArea.js';
package/out/index.js CHANGED
@@ -1,2 +1 @@
1
1
  export { comparePdf } from './comparePdf.js';
2
- //# sourceMappingURL=index.js.map
@@ -1,9 +1,8 @@
1
- import { PdfToPngOptions } from 'pdf-to-png-converter';
2
- import { ExcludedPageArea } from './ExcludedPageArea.js';
1
+ import type { PdfToPngOptions } from 'pdf-to-png-converter';
2
+ import type { ExcludedPageArea } from './ExcludedPageArea.js';
3
3
  export type ComparePdfOptions = {
4
4
  diffsOutputFolder?: string;
5
5
  pdfToPngConvertOptions?: PdfToPngOptions;
6
6
  excludedAreas?: readonly ExcludedPageArea[];
7
7
  compareThreshold?: number;
8
8
  };
9
- //# sourceMappingURL=ComparePdfOptions.d.ts.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=ComparePdfOptions.js.map
@@ -1,4 +1,4 @@
1
- import { Area, Color } from 'png-visual-compare';
1
+ import type { Area, Color } from 'png-visual-compare';
2
2
  export type ExcludedPageArea = {
3
3
  pageNumber: number;
4
4
  excludedAreas?: Area[];
@@ -6,4 +6,3 @@ export type ExcludedPageArea = {
6
6
  diffFilePath?: string;
7
7
  matchingThreshold?: number;
8
8
  };
9
- //# sourceMappingURL=ExcludedPageArea.d.ts.map
@@ -1,2 +1 @@
1
1
  export {};
2
- //# sourceMappingURL=ExcludedPageArea.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pdf-visual-compare",
3
- "version": "3.0.1",
3
+ "version": "3.1.0",
4
4
  "description": "Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.",
5
5
  "keywords": [
6
6
  "pdf",
@@ -34,19 +34,20 @@
34
34
  "predocker:run": "npm run clean",
35
35
  "docker:run": "docker run --rm -it -v $PWD/test-results:/usr/pkg/test-results test-pdf-visual-compare",
36
36
  "docker:test": "vitest run",
37
- "license-checker": "npx license-checker --production --onlyAllow 'MIT; MIT OR X11; BSD; ISC; Apache-2.0; Unlicense'",
37
+ "test:license": "npx --yes license-checker --production --onlyAllow \"ISC; MIT; MIT OR X11; BSD; Apache-2.0; Unlicense\"",
38
38
  "lint": "eslint .",
39
39
  "lint:fix": "npm run lint -- --fix",
40
- "pretest": "npm run clean",
40
+ "pretest": "npm run clean && npm run lint && npm run test:license && npm run build",
41
41
  "test": "vitest run --coverage",
42
42
  "test:docker": "npm run docker:build && npm run docker:run"
43
43
  },
44
44
  "dependencies": {
45
- "pdf-to-png-converter": "~3.8.0",
46
- "png-visual-compare": "~3.0.0"
45
+ "pdf-to-png-converter": "~3.9.0",
46
+ "png-visual-compare": "~3.1.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/node": "^24.5.2",
50
+ "@types/pngjs": "^6.0.5",
50
51
  "@typescript-eslint/eslint-plugin": "^8.44.0",
51
52
  "@typescript-eslint/parser": "^8.44.0",
52
53
  "@vitest/coverage-v8": "^3.2.4",
@@ -60,4 +61,4 @@
60
61
  "node": ">=20",
61
62
  "yarn": "please-use-npm"
62
63
  }
63
- }
64
+ }
@@ -1 +0,0 @@
1
- {"version":3,"file":"comparePdf.d.ts","sourceRoot":"","sources":["../src/comparePdf.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AAYjE,wBAAsB,UAAU,CAC5B,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,WAAW,EAAE,MAAM,GAAG,MAAM,EAC5B,IAAI,GAAE,iBAAsB,GAC7B,OAAO,CAAC,OAAO,CAAC,CA0DlB"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"comparePdf.js","sourceRoot":"","sources":["../src/comparePdf.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC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,MAAM,KAAK,CAAC,uBAAuB,SAAS,EAAE,CAAC,CAAC;QACpD,CAAC;IACL,CAAC;IACD,MAAM,KAAK,CAAC,0BAA0B,CAAC,CAAC;AAC5C,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"const.d.ts","sourceRoot":"","sources":["../src/const.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,oBAAoB,QAAgC,CAAC"}
package/out/const.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"const.js","sourceRoot":"","sources":["../src/const.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,MAAM,CAAC,MAAM,oBAAoB,GAAG,OAAO,CAAC,oBAAoB,CAAC,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAG7C,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAGjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,8BAA8B,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC"}
package/out/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComparePdfOptions.d.ts","sourceRoot":"","sources":["../../src/types/ComparePdfOptions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEzD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sBAAsB,CAAC,EAAE,eAAe,CAAC;IACzC,aAAa,CAAC,EAAE,SAAS,gBAAgB,EAAE,CAAC;IAC5C,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ComparePdfOptions.js","sourceRoot":"","sources":["../../src/types/ComparePdfOptions.ts"],"names":[],"mappings":""}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ExcludedPageArea.d.ts","sourceRoot":"","sources":["../../src/types/ExcludedPageArea.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEjD,MAAM,MAAM,gBAAgB,GAAG;IAC7B,UAAU,EAAE,MAAM,CAAC;IACnB,aAAa,CAAC,EAAE,IAAI,EAAE,CAAC;IACvB,iBAAiB,CAAC,EAAE,KAAK,CAAC;IAC1B,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,iBAAiB,CAAC,EAAE,MAAM,CAAC;CAC5B,CAAC"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"ExcludedPageArea.js","sourceRoot":"","sources":["../../src/types/ExcludedPageArea.ts"],"names":[],"mappings":""}