pdf-visual-compare 1.4.0 → 2.2.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 +1 -1
- package/out/compare.pdf.d.ts.map +1 -1
- package/out/compare.pdf.js +27 -51
- package/out/compare.pdf.js.map +1 -1
- package/out/const.js +2 -5
- package/out/const.js.map +1 -1
- package/out/index.js +3 -22
- package/out/index.js.map +1 -1
- package/out/types/compare.options.js +1 -2
- package/out/types/excluded.page.area.js +1 -2
- package/out/types/index.js +1 -2
- package/package.json +19 -22
package/README.md
CHANGED
|
@@ -22,7 +22,7 @@ npm install -D pdf-visual-compare
|
|
|
22
22
|
const result: boolean = await comparePdf('./pdf1.pdf', './pdf2.pdf', {
|
|
23
23
|
diffsOutputFolder?: string; // Folder to write output PNG files with differences
|
|
24
24
|
pdfToPngConvertOptions?: {
|
|
25
|
-
viewportScale: 2.0, // The desired scale of PNG viewport. Default value is
|
|
25
|
+
viewportScale: 2.0, // The desired scale of PNG viewport. Default value is 2.0.
|
|
26
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.
|
package/out/compare.pdf.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.pdf.d.ts","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":"
|
|
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
CHANGED
|
@@ -1,21 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const png_visual_compare_1 = __importDefault(require("png-visual-compare"));
|
|
10
|
-
const pngjs_1 = require("pngjs");
|
|
11
|
-
const const_1 = require("./const");
|
|
12
|
-
async function comparePdf(actualPdfFilePathOrBuffer, expectedPdfFilePathOrBuffer, opts) {
|
|
13
|
-
if (!Buffer.isBuffer(actualPdfFilePathOrBuffer) && !(0, fs_1.existsSync)(actualPdfFilePathOrBuffer)) {
|
|
14
|
-
throw Error('Actual PDF file not found.');
|
|
15
|
-
}
|
|
16
|
-
if (!Buffer.isBuffer(expectedPdfFilePathOrBuffer) && !(0, fs_1.existsSync)(expectedPdfFilePathOrBuffer)) {
|
|
17
|
-
throw Error('Expected PDF file not found.');
|
|
18
|
-
}
|
|
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);
|
|
19
9
|
const pdfToPngConvertOpts = { ...opts?.pdfToPngConvertOptions };
|
|
20
10
|
if (!pdfToPngConvertOpts.viewportScale) {
|
|
21
11
|
pdfToPngConvertOpts.viewportScale = 2.0;
|
|
@@ -23,57 +13,43 @@ async function comparePdf(actualPdfFilePathOrBuffer, expectedPdfFilePathOrBuffer
|
|
|
23
13
|
if (!pdfToPngConvertOpts.outputFileMask) {
|
|
24
14
|
pdfToPngConvertOpts.outputFileMask = 'comparePdf';
|
|
25
15
|
}
|
|
26
|
-
const diffsOutputFolder = opts?.diffsOutputFolder
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
const compareThreshold = opts?.compareThreshold
|
|
30
|
-
? opts?.compareThreshold
|
|
31
|
-
: 0;
|
|
32
|
-
const excludedAreas = opts?.excludedAreas
|
|
33
|
-
? opts.excludedAreas
|
|
34
|
-
: [];
|
|
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 : [];
|
|
35
19
|
if (compareThreshold < 0) {
|
|
36
20
|
throw Error('Compare Threshold cannot be less than 0.');
|
|
37
21
|
}
|
|
38
22
|
let [actualPdfPngPages, expectedPdfPngPages] = await Promise.all([
|
|
39
|
-
|
|
40
|
-
|
|
23
|
+
pdfToPng(actualPdfFilePathOrBuffer, pdfToPngConvertOpts),
|
|
24
|
+
pdfToPng(expectedPdfFilePathOrBuffer, pdfToPngConvertOpts),
|
|
41
25
|
]);
|
|
42
26
|
if (actualPdfPngPages.length < expectedPdfPngPages.length) {
|
|
43
27
|
[actualPdfPngPages, expectedPdfPngPages] = [expectedPdfPngPages, actualPdfPngPages];
|
|
44
28
|
}
|
|
45
29
|
let documentCompareResult = true;
|
|
46
30
|
actualPdfPngPages.forEach((pngPage, index) => {
|
|
47
|
-
const comparePngOpts = { ...opts?.pdfToPngConvertOptions, ...excludedAreas[index] };
|
|
48
|
-
comparePngOpts.diffFilePath =
|
|
31
|
+
const comparePngOpts = { ...opts?.pdfToPngConvertOptions, ...excludedAreas[index], throwErrorOnInvalidInputData: false };
|
|
32
|
+
comparePngOpts.diffFilePath = resolve(diffsOutputFolder, `diff_${pngPage.name}`);
|
|
49
33
|
const pngPageOutputToCompareWith = expectedPdfPngPages.find((p) => p.name === pngPage.name);
|
|
50
|
-
|
|
51
|
-
if (pngPageOutputToCompareWith) {
|
|
52
|
-
bufferToCompareWith = pngPageOutputToCompareWith.content;
|
|
53
|
-
}
|
|
54
|
-
else {
|
|
55
|
-
const originalPngMetaData = pngjs_1.PNG.sync.read(pngPage.content);
|
|
56
|
-
bufferToCompareWith = getEmptyPngBuffer(originalPngMetaData.width, originalPngMetaData.height);
|
|
57
|
-
}
|
|
58
|
-
const pageCompareResult = (0, png_visual_compare_1.default)(pngPage.content, bufferToCompareWith, comparePngOpts);
|
|
34
|
+
const pageCompareResult = comparePng(pngPage.content, pngPageOutputToCompareWith?.content ?? '', comparePngOpts);
|
|
59
35
|
if (pageCompareResult > compareThreshold) {
|
|
60
36
|
documentCompareResult = false;
|
|
61
37
|
}
|
|
62
38
|
});
|
|
63
39
|
return documentCompareResult;
|
|
64
40
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
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.`);
|
|
75
51
|
}
|
|
76
52
|
}
|
|
77
|
-
|
|
53
|
+
throw Error(`Unknown input file type.`);
|
|
78
54
|
}
|
|
79
55
|
//# sourceMappingURL=compare.pdf.js.map
|
package/out/compare.pdf.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.pdf.js","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compare.pdf.js","sourceRo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,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"}
|
package/out/const.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.DEFAULT_DIFFS_FOLDER = void 0;
|
|
4
|
-
const path_1 = require("path");
|
|
5
|
-
exports.DEFAULT_DIFFS_FOLDER = (0, path_1.resolve)(`./comparePdfOutput`);
|
|
1
|
+
import { resolve } from 'node:path';
|
|
2
|
+
export const DEFAULT_DIFFS_FOLDER = resolve(`./comparePdfOutput`);
|
|
6
3
|
//# sourceMappingURL=const.js.map
|
package/out/const.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"const.js","sourceRoot":"","sources":["../src/const.ts"],"names":[],"mappings":"
|
|
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"}
|
package/out/index.js
CHANGED
|
@@ -1,23 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
-
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
-
};
|
|
16
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
-
};
|
|
19
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
const compare_pdf_1 = __importDefault(require("./compare.pdf"));
|
|
21
|
-
exports.default = compare_pdf_1.default;
|
|
22
|
-
__exportStar(require("./types"), exports);
|
|
1
|
+
import comparePdf from './compare.pdf';
|
|
2
|
+
export default comparePdf;
|
|
3
|
+
export * from './types';
|
|
23
4
|
//# sourceMappingURL=index.js.map
|
package/out/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,UAAU,MAAM,eAAe,CAAC;AACvC,eAAe,UAAU,CAAC;AAC1B,cAAc,SAAS,CAAC"}
|
package/out/types/index.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-visual-compare",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pdf",
|
|
@@ -24,36 +24,33 @@
|
|
|
24
24
|
"/out"
|
|
25
25
|
],
|
|
26
26
|
"scripts": {
|
|
27
|
-
"
|
|
27
|
+
"prebuild": "npm run clean",
|
|
28
|
+
"build": "tsc --pretty",
|
|
28
29
|
"clean": "rimraf ./out ./coverage ./test-results ./comparePdfOutput",
|
|
29
30
|
"docker:build": "docker build --compress -t test-pdf-visual-compare .",
|
|
30
31
|
"predocker:run": "npm run clean",
|
|
31
|
-
"docker:run": "docker run --rm -it -v test-results:/usr/pkg/test-results test-pdf-visual-compare",
|
|
32
|
-
"docker:test": "
|
|
32
|
+
"docker:run": "docker run --rm -it -v $PWD/test-results:/usr/pkg/test-results test-pdf-visual-compare",
|
|
33
|
+
"docker:test": "vitest run",
|
|
33
34
|
"license-checker": "npx license-checker --production --onlyAllow 'MIT; MIT OR X11; BSD; ISC; Apache-2.0; Unlicense'",
|
|
34
35
|
"lint": "eslint .",
|
|
35
36
|
"lint:fix": "npm run lint -- --fix",
|
|
36
|
-
"pretest": "npm run
|
|
37
|
-
"test": "
|
|
38
|
-
"test:
|
|
39
|
-
"test:docker": "npm run docker:build && npm run docker:run",
|
|
40
|
-
"tsc": "tsc --pretty"
|
|
37
|
+
"pretest": "npm run clean",
|
|
38
|
+
"test": "vitest run --coverage",
|
|
39
|
+
"test:docker": "npm run docker:build && npm run docker:run"
|
|
41
40
|
},
|
|
42
41
|
"dependencies": {
|
|
43
|
-
"pdf-to-png-converter": "^
|
|
44
|
-
"png-visual-compare": "^
|
|
45
|
-
"pngjs": "^6.0.0"
|
|
42
|
+
"pdf-to-png-converter": "^3.3.0",
|
|
43
|
+
"png-visual-compare": "^2.1.0"
|
|
46
44
|
},
|
|
47
45
|
"devDependencies": {
|
|
48
|
-
"@types/
|
|
49
|
-
"@
|
|
50
|
-
"@
|
|
51
|
-
"@
|
|
52
|
-
"
|
|
53
|
-
"
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"
|
|
57
|
-
"typescript": "^4.9.3"
|
|
46
|
+
"@types/node": "^22.5.1",
|
|
47
|
+
"@typescript-eslint/eslint-plugin": "^8.3.0",
|
|
48
|
+
"@typescript-eslint/parser": "^8.3.0",
|
|
49
|
+
"@vitest/coverage-v8": "^2.0.5",
|
|
50
|
+
"eslint": "^9.9.1",
|
|
51
|
+
"rimraf": "^6.0.1",
|
|
52
|
+
"ts-node": "^10.9.2",
|
|
53
|
+
"typescript": "^5.5.4",
|
|
54
|
+
"vitest": "^2.0.5"
|
|
58
55
|
}
|
|
59
56
|
}
|