pdf-visual-compare 2.2.0 → 2.4.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.
- package/README.md +19 -19
- package/out/comparePdf.d.ts +3 -0
- package/out/comparePdf.d.ts.map +1 -0
- package/out/comparePdf.js +68 -0
- package/out/comparePdf.js.map +1 -0
- package/out/index.d.ts +5 -2
- package/out/index.d.ts.map +1 -1
- package/out/index.js +1 -2
- package/out/index.js.map +1 -1
- package/out/types/ComparePdfOptions.d.ts +9 -0
- package/out/types/ComparePdfOptions.d.ts.map +1 -0
- package/out/types/ComparePdfOptions.js +2 -0
- package/out/types/ComparePdfOptions.js.map +1 -0
- package/out/types/{excluded.page.area.d.ts → ExcludedPageArea.d.ts} +2 -2
- package/out/types/ExcludedPageArea.d.ts.map +1 -0
- package/out/types/ExcludedPageArea.js +2 -0
- package/out/types/ExcludedPageArea.js.map +1 -0
- package/package.json +24 -13
- package/out/compare.pdf.d.ts +0 -3
- package/out/compare.pdf.d.ts.map +0 -1
- package/out/compare.pdf.js +0 -55
- package/out/compare.pdf.js.map +0 -1
- package/out/types/compare.options.d.ts +0 -8
- package/out/types/compare.options.d.ts.map +0 -1
- package/out/types/compare.options.js +0 -2
- package/out/types/compare.options.js.map +0 -1
- package/out/types/excluded.page.area.d.ts.map +0 -1
- package/out/types/excluded.page.area.js +0 -2
- package/out/types/excluded.page.area.js.map +0 -1
- package/out/types/index.d.ts +0 -5
- package/out/types/index.d.ts.map +0 -1
- package/out/types/index.js +0 -2
- package/out/types/index.js.map +0 -1
package/README.md
CHANGED
|
@@ -1,45 +1,45 @@
|
|
|
1
1
|
# pdf-visual-compare
|
|
2
2
|
|
|
3
|
-
Visual regression testing library for PDFs in
|
|
3
|
+
Visual regression testing library for PDFs in JavaScript/TypeScript without binary and OS dependencies.
|
|
4
4
|
|
|
5
5
|
[](https://github.com/dichovsky/pdf-visual-compare/actions/workflows/test.yml)
|
|
6
6
|
|
|
7
|
-
## Getting
|
|
7
|
+
## Getting Started
|
|
8
8
|
|
|
9
|
-
Installation
|
|
9
|
+
### Installation
|
|
10
10
|
|
|
11
11
|
```sh
|
|
12
12
|
npm install -D pdf-visual-compare
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
|
|
15
|
+
### Example
|
|
16
16
|
|
|
17
17
|
```javascript
|
|
18
|
-
|
|
18
|
+
const result = await comparePdf('./pdf1.pdf', './pdf2.pdf');
|
|
19
19
|
|
|
20
|
-
|
|
20
|
+
// If you want to configure the comparing process, use the following options
|
|
21
21
|
|
|
22
|
-
|
|
23
|
-
diffsOutputFolder
|
|
24
|
-
pdfToPngConvertOptions
|
|
22
|
+
const result = await comparePdf('./pdf1.pdf', './pdf2.pdf', {
|
|
23
|
+
diffsOutputFolder: 'diffs', // Folder to write output PNG files with differences
|
|
24
|
+
pdfToPngConvertOptions: {
|
|
25
25
|
viewportScale: 2.0, // The desired scale of PNG viewport. Default value is 2.0.
|
|
26
|
-
disableFontFace: false, //When `false`, fonts will be rendered using a built-in font renderer that constructs the glyphs with primitive path commands. Default value is true.
|
|
26
|
+
disableFontFace: false, // When `false`, fonts will be rendered using a built-in font renderer that constructs the glyphs with primitive path commands. Default value is true.
|
|
27
27
|
useSystemFonts: false, // When `true`, fonts that aren't embedded in the PDF document will fallback to a system font. Default value is false.
|
|
28
28
|
pdfFilePassword: 'pa$$word', // Password for encrypted PDF.
|
|
29
29
|
outputFolder: 'output/folder', // Folder to write output PNG files. If not specified, PNG output will be available only as a Buffer content, without saving to a file.
|
|
30
30
|
outputFileMask: 'buffer', // Output filename mask. Default value is 'buffer'.
|
|
31
|
-
pagesToProcess: [1, 3, 11],
|
|
32
|
-
strictPagesToProcess: false // When `true`, will throw an error if specified page number in pagesToProcess is invalid, otherwise will skip invalid page. Default value is false.
|
|
31
|
+
pagesToProcess: [1, 3, 11], // Subset of pages to convert (first page = 1), other pages will be skipped if specified.
|
|
32
|
+
strictPagesToProcess: false, // When `true`, will throw an error if specified page number in pagesToProcess is invalid, otherwise will skip invalid page. Default value is false.
|
|
33
33
|
verbosityLevel: 0 // Verbosity level. ERRORS: 0, WARNINGS: 1, INFOS: 5. Default value is 0.
|
|
34
|
-
}
|
|
35
|
-
excludedAreas
|
|
36
|
-
compareThreshold
|
|
34
|
+
},
|
|
35
|
+
excludedAreas: [], // Areas list to exclude from comparing for each PDF page. Empty array by default.
|
|
36
|
+
compareThreshold: 0.1 // Comparing threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.1 by default.
|
|
37
37
|
});
|
|
38
|
-
|
|
39
38
|
```
|
|
40
39
|
|
|
41
|
-
##
|
|
40
|
+
## Support
|
|
41
|
+
|
|
42
|
+
If you want to support my work, you can buy me a coffee.
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
[](https://buymeacoffee.com/dichovsky)
|
|
44
45
|
|
|
45
|
-
[](https://buymeacoffee.com/dichovsky)
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1,68 @@
|
|
|
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
|
+
import { existsSync } from 'node:fs';
|
|
11
|
+
import { resolve } from 'node:path';
|
|
12
|
+
import { pdfToPng } from 'pdf-to-png-converter';
|
|
13
|
+
import comparePng from 'png-visual-compare';
|
|
14
|
+
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];
|
|
39
|
+
}
|
|
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
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function validateInputFileType(inputFile) {
|
|
55
|
+
if (Buffer.isBuffer(inputFile)) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (typeof inputFile === 'string') {
|
|
59
|
+
if (existsSync(inputFile)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
throw Error(`PDF file not found: ${inputFile}`);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
throw Error(`Unknown input file type.`);
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=comparePdf.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"comparePdf.js","sourceRoot":"","sources":["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O,SAAS,KAAK,QAAQ,EAAE,CAAC;QAChC,IAAI,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YACxB,OAAO;QACX,CAAC;aAAM,CAAC;YACJ,MAAM,KAAK,CAAC,uBAAuB,SAAS,EAAE,CAAC,CAAC;QACpD,CAAC;IACL,CAAC;IACD,MAAM,KAAK,CAAC,0BAA0B,CAAC,CAAC;AAC5C,CAAC"}
|
package/out/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
import comparePdf from './
|
|
1
|
+
import { comparePdf } from './comparePdf.js';
|
|
2
2
|
export default comparePdf;
|
|
3
|
-
export
|
|
3
|
+
export { PdfToPngOptions } from 'pdf-to-png-converter';
|
|
4
|
+
export { Area, Color } from 'png-visual-compare';
|
|
5
|
+
export { ComparePdfOptions } from './types/ComparePdfOptions.js';
|
|
6
|
+
export { ExcludedPageArea } from './types/ExcludedPageArea.js';
|
|
4
7
|
//# sourceMappingURL=index.d.ts.map
|
package/out/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,eAAe,UAAU,CAAC;AAG1B,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
CHANGED
package/out/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,eAAe,UAAU,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { PdfToPngOptions } from 'pdf-to-png-converter';
|
|
2
|
+
import { ExcludedPageArea } from './ExcludedPageArea.js';
|
|
3
|
+
export type ComparePdfOptions = {
|
|
4
|
+
diffsOutputFolder?: string;
|
|
5
|
+
pdfToPngConvertOptions?: PdfToPngOptions;
|
|
6
|
+
excludedAreas?: readonly ExcludedPageArea[];
|
|
7
|
+
compareThreshold?: number;
|
|
8
|
+
};
|
|
9
|
+
//# sourceMappingURL=ComparePdfOptions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ComparePdfOptions.js","sourceRoot":"","sources":["../../src/types/ComparePdfOptions.ts"],"names":[],"mappings":""}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Area, Color } from '
|
|
1
|
+
import { Area, Color } from 'png-visual-compare';
|
|
2
2
|
export type ExcludedPageArea = {
|
|
3
3
|
pageNumber: number;
|
|
4
4
|
excludedAreas?: Area[];
|
|
@@ -6,4 +6,4 @@ export type ExcludedPageArea = {
|
|
|
6
6
|
diffFilePath?: string;
|
|
7
7
|
matchingThreshold?: number;
|
|
8
8
|
};
|
|
9
|
-
//# sourceMappingURL=
|
|
9
|
+
//# sourceMappingURL=ExcludedPageArea.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ExcludedPageArea.js","sourceRoot":"","sources":["../../src/types/ExcludedPageArea.ts"],"names":[],"mappings":""}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-visual-compare",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pdf",
|
|
@@ -18,10 +18,17 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"author": "Igor Magdich <magdich.igor@gmail.com>",
|
|
21
|
-
"
|
|
22
|
-
"
|
|
21
|
+
"type": "module",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"default": "./out/index.js",
|
|
25
|
+
"types": "./out/index.d.ts"
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
"main": "./out/index.js",
|
|
29
|
+
"types": "./out/index.d.ts",
|
|
23
30
|
"files": [
|
|
24
|
-
"
|
|
31
|
+
"./out"
|
|
25
32
|
],
|
|
26
33
|
"scripts": {
|
|
27
34
|
"prebuild": "npm run clean",
|
|
@@ -39,18 +46,22 @@
|
|
|
39
46
|
"test:docker": "npm run docker:build && npm run docker:run"
|
|
40
47
|
},
|
|
41
48
|
"dependencies": {
|
|
42
|
-
"pdf-to-png-converter": "^3.
|
|
43
|
-
"png-visual-compare": "^2.1
|
|
49
|
+
"pdf-to-png-converter": "^3.6.1",
|
|
50
|
+
"png-visual-compare": "^2.2.1"
|
|
44
51
|
},
|
|
45
52
|
"devDependencies": {
|
|
46
|
-
"@types/node": "^22.
|
|
47
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
48
|
-
"@typescript-eslint/parser": "^8.
|
|
49
|
-
"@vitest/coverage-v8": "^2.
|
|
50
|
-
"eslint": "^9.
|
|
53
|
+
"@types/node": "^22.10.1",
|
|
54
|
+
"@typescript-eslint/eslint-plugin": "^8.17.0",
|
|
55
|
+
"@typescript-eslint/parser": "^8.17.0",
|
|
56
|
+
"@vitest/coverage-v8": "^2.1.8",
|
|
57
|
+
"eslint": "^9.16.0",
|
|
51
58
|
"rimraf": "^6.0.1",
|
|
52
59
|
"ts-node": "^10.9.2",
|
|
53
|
-
"typescript": "^5.
|
|
54
|
-
"vitest": "^2.
|
|
60
|
+
"typescript": "^5.7.2",
|
|
61
|
+
"vitest": "^2.1.8"
|
|
62
|
+
},
|
|
63
|
+
"engines": {
|
|
64
|
+
"node": ">=20",
|
|
65
|
+
"yarn": "please-use-npm"
|
|
55
66
|
}
|
|
56
67
|
}
|
package/out/compare.pdf.d.ts
DELETED
|
@@ -1,3 +0,0 @@
|
|
|
1
|
-
import { ComparePdfOptions } from './types';
|
|
2
|
-
export default function comparePdf(actualPdfFilePathOrBuffer: string | ArrayBufferLike, expectedPdfFilePathOrBuffer: string | ArrayBufferLike, opts?: ComparePdfOptions): Promise<boolean>;
|
|
3
|
-
//# sourceMappingURL=compare.pdf.d.ts.map
|
package/out/compare.pdf.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compare.pdf.d.ts","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAiB,EAAqC,MAAM,SAAS,CAAC;AAE/E,wBAA8B,UAAU,CACtC,yBAAyB,EAAE,MAAM,GAAG,eAAe,EACnD,2BAA2B,EAAE,MAAM,GAAG,eAAe,EACrD,IAAI,CAAC,EAAE,iBAAiB,GACvB,OAAO,CAAC,OAAO,CAAC,CA8ClB"}
|
package/out/compare.pdf.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { existsSync } from 'node:fs';
|
|
2
|
-
import { resolve } from 'node:path';
|
|
3
|
-
import { pdfToPng } from 'pdf-to-png-converter';
|
|
4
|
-
import comparePng from 'png-visual-compare';
|
|
5
|
-
import { DEFAULT_DIFFS_FOLDER } from './const';
|
|
6
|
-
export default async function comparePdf(actualPdfFilePathOrBuffer, expectedPdfFilePathOrBuffer, opts) {
|
|
7
|
-
inputFileTypeGuard(actualPdfFilePathOrBuffer);
|
|
8
|
-
inputFileTypeGuard(expectedPdfFilePathOrBuffer);
|
|
9
|
-
const pdfToPngConvertOpts = { ...opts?.pdfToPngConvertOptions };
|
|
10
|
-
if (!pdfToPngConvertOpts.viewportScale) {
|
|
11
|
-
pdfToPngConvertOpts.viewportScale = 2.0;
|
|
12
|
-
}
|
|
13
|
-
if (!pdfToPngConvertOpts.outputFileMask) {
|
|
14
|
-
pdfToPngConvertOpts.outputFileMask = 'comparePdf';
|
|
15
|
-
}
|
|
16
|
-
const diffsOutputFolder = opts?.diffsOutputFolder ? opts.diffsOutputFolder : DEFAULT_DIFFS_FOLDER;
|
|
17
|
-
const compareThreshold = opts?.compareThreshold ? opts?.compareThreshold : 0;
|
|
18
|
-
const excludedAreas = opts?.excludedAreas ? opts.excludedAreas : [];
|
|
19
|
-
if (compareThreshold < 0) {
|
|
20
|
-
throw Error('Compare Threshold cannot be less than 0.');
|
|
21
|
-
}
|
|
22
|
-
let [actualPdfPngPages, expectedPdfPngPages] = await Promise.all([
|
|
23
|
-
pdfToPng(actualPdfFilePathOrBuffer, pdfToPngConvertOpts),
|
|
24
|
-
pdfToPng(expectedPdfFilePathOrBuffer, pdfToPngConvertOpts),
|
|
25
|
-
]);
|
|
26
|
-
if (actualPdfPngPages.length < expectedPdfPngPages.length) {
|
|
27
|
-
[actualPdfPngPages, expectedPdfPngPages] = [expectedPdfPngPages, actualPdfPngPages];
|
|
28
|
-
}
|
|
29
|
-
let documentCompareResult = true;
|
|
30
|
-
actualPdfPngPages.forEach((pngPage, index) => {
|
|
31
|
-
const comparePngOpts = { ...opts?.pdfToPngConvertOptions, ...excludedAreas[index], throwErrorOnInvalidInputData: false };
|
|
32
|
-
comparePngOpts.diffFilePath = resolve(diffsOutputFolder, `diff_${pngPage.name}`);
|
|
33
|
-
const pngPageOutputToCompareWith = expectedPdfPngPages.find((p) => p.name === pngPage.name);
|
|
34
|
-
const pageCompareResult = comparePng(pngPage.content, pngPageOutputToCompareWith?.content ?? '', comparePngOpts);
|
|
35
|
-
if (pageCompareResult > compareThreshold) {
|
|
36
|
-
documentCompareResult = false;
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
return documentCompareResult;
|
|
40
|
-
}
|
|
41
|
-
function inputFileTypeGuard(inputFile) {
|
|
42
|
-
if (Buffer.isBuffer(inputFile)) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (typeof inputFile === 'string') {
|
|
46
|
-
if (existsSync(inputFile)) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
else {
|
|
50
|
-
throw Error(`PDF file not found.`);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
throw Error(`Unknown input file type.`);
|
|
54
|
-
}
|
|
55
|
-
//# sourceMappingURL=compare.pdf.js.map
|
package/out/compare.pdf.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compare.pdf.js","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,QAAQ,EAAiB,MAAM,sBAAsB,CAAC;AAC/D,OAAO,UAAiC,MAAM,oBAAoB,CAAC;AACnE,OAAO,EAAE,oBAAoB,EAAE,MAAM,SAAS,CAAC;AAG/C,MAAM,CAAC,OAAO,CAAC,KAAK,UAAU,UAAU,CACtC,yBAAmD,EACnD,2BAAqD,EACrD,IAAwB;IAExB,kBAAkB,CAAC,yBAAyB,CAAC,CAAC;IAC9C,kBAAkB,CAAC,2BAA2B,CAAC,CAAC;IAEhD,MAAM,mBAAmB,GAAoB,EAAE,GAAG,IAAI,EAAE,sBAAsB,EAAE,CAAC;IACjF,IAAI,CAAC,mBAAmB,CAAC,aAAa,EAAE,CAAC;QACvC,mBAAmB,CAAC,aAAa,GAAG,GAAG,CAAC;IAC1C,CAAC;IACD,IAAI,CAAC,mBAAmB,CAAC,cAAc,EAAE,CAAC;QACxC,mBAAmB,CAAC,cAAc,GAAG,YAAY,CAAC;IACpD,CAAC;IAED,MAAM,iBAAiB,GAAW,IAAI,EAAE,iBAAiB,CAAC,CAAC,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,oBAAoB,CAAC;IAC1G,MAAM,gBAAgB,GAAW,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC,CAAC,CAAC,CAAC;IACrF,MAAM,aAAa,GAAuB,IAAI,EAAE,aAAa,CAAC,CAAC,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC,CAAC,EAAE,CAAC;IAExF,IAAI,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kB,CAAC,SAAc;IACxC,IAAI,MAAM,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAC/B,OAAO;IACT,CAAC;IACD,IAAI,OAAO,SAAS,KAAK,QAAQ,EAAE,CAAC;QAClC,IAAI,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YAC1B,OAAO;QACT,CAAC;aAAM,CAAC;YACN,MAAM,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACrC,CAAC;IACH,CAAC;IACD,MAAM,KAAK,CAAC,0BAA0B,CAAC,CAAC;AAC1C,CAAC"}
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { ExcludedPageArea, PdfToPngOptions } from '.';
|
|
2
|
-
export type ComparePdfOptions = {
|
|
3
|
-
diffsOutputFolder?: string;
|
|
4
|
-
pdfToPngConvertOptions?: PdfToPngOptions;
|
|
5
|
-
excludedAreas?: ExcludedPageArea[];
|
|
6
|
-
compareThreshold?: number;
|
|
7
|
-
};
|
|
8
|
-
//# sourceMappingURL=compare.options.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compare.options.d.ts","sourceRoot":"","sources":["../../src/types/compare.options.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,GAAG,CAAC;AAEtD,MAAM,MAAM,iBAAiB,GAAG;IAC9B,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sBAAsB,CAAC,EAAE,eAAe,CAAC;IACzC,aAAa,CAAC,EAAE,gBAAgB,EAAE,CAAC;IACnC,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"compare.options.js","sourceRoot":"","sources":["../../src/types/compare.options.ts"],"names":[],"mappings":""}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"excluded.page.area.d.ts","sourceRoot":"","sources":["../../src/types/excluded.page.area.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,GAAG,CAAC;AAEhC,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":"excluded.page.area.js","sourceRoot":"","sources":["../../src/types/excluded.page.area.ts"],"names":[],"mappings":""}
|
package/out/types/index.d.ts
DELETED
package/out/types/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AACvD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAC7D,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/out/types/index.js
DELETED
package/out/types/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":""}
|