pdf-visual-compare 0.0.10 → 1.1.1
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 +42 -1
- package/out/compare.pdf.d.ts +1 -1
- package/out/compare.pdf.js +26 -18
- package/out/compare.pdf.js.map +1 -1
- package/out/types/compare.options.d.ts +2 -1
- package/package.json +12 -12
package/README.md
CHANGED
|
@@ -1,4 +1,45 @@
|
|
|
1
1
|
# pdf-visual-compare
|
|
2
|
-
|
|
2
|
+
|
|
3
|
+
Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.
|
|
3
4
|
|
|
4
5
|
[](https://github.com/dichovsky/pdf-visual-compare/actions/workflows/test.yml)
|
|
6
|
+
|
|
7
|
+
## Getting started
|
|
8
|
+
|
|
9
|
+
Installation:
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install -D pdf-visual-compare
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Example
|
|
16
|
+
|
|
17
|
+
```javascript
|
|
18
|
+
const result: boolean = await comparePdf('./pdf1.pdf', './pdf2.pdf');
|
|
19
|
+
|
|
20
|
+
// If you want to configure comparing process, use the following props
|
|
21
|
+
|
|
22
|
+
const result: boolean = await comparePdf('./pdf1.pdf', './pdf2.pdf', {
|
|
23
|
+
diffsOutputFolder?: string; // Folder to write output PNG files with differences
|
|
24
|
+
pdfToPngConvertOptions?: {
|
|
25
|
+
viewportScale: 2.0, // The desired scale of PNG viewport. Default value is 1.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.
|
|
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
|
+
pdfFilePassword: 'pa$$word', // Password for encrypted PDF.
|
|
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
|
+
outputFileMask: 'buffer', // Output filename mask. Default value is 'buffer'.
|
|
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
|
+
verbosityLevel: 0 // Verbosity level. ERRORS: 0, WARNINGS: 1, INFOS: 5. Default value is 0.
|
|
34
|
+
};
|
|
35
|
+
excludedAreas?: ExcludedPageArea[]; // Areas list to exclude from comparing for each PDF page. Empty array by default.
|
|
36
|
+
compareThreshold?: number; // Comparing threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.1 by default.
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Buy Me A Coffee
|
|
42
|
+
|
|
43
|
+
In case you want support my work
|
|
44
|
+
|
|
45
|
+
[](https://buymeacoffee.com/dichovsky)
|
package/out/compare.pdf.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { ComparePdfOptions } from './types';
|
|
2
|
-
export default function comparePdf(
|
|
2
|
+
export default function comparePdf(actualPdfFilePathOrBuffer: string | ArrayBufferLike, expectedPdfFilePathOrBuffer: string | ArrayBufferLike, opts?: ComparePdfOptions): Promise<boolean>;
|
package/out/compare.pdf.js
CHANGED
|
@@ -7,35 +7,43 @@ const fs_1 = require("fs");
|
|
|
7
7
|
const path_1 = require("path");
|
|
8
8
|
const pdf_to_png_converter_1 = require("pdf-to-png-converter");
|
|
9
9
|
const png_visual_compare_1 = __importDefault(require("png-visual-compare"));
|
|
10
|
-
async function comparePdf(
|
|
11
|
-
|
|
12
|
-
if (!(0, fs_1.existsSync)(actualPdfFilePathResolved)) {
|
|
10
|
+
async function comparePdf(actualPdfFilePathOrBuffer, expectedPdfFilePathOrBuffer, opts) {
|
|
11
|
+
if (!Buffer.isBuffer(actualPdfFilePathOrBuffer) && !(0, fs_1.existsSync)(actualPdfFilePathOrBuffer)) {
|
|
13
12
|
throw Error('Actual PDF file not found.');
|
|
14
13
|
}
|
|
15
|
-
|
|
16
|
-
if (!(0, fs_1.existsSync)(expectedPdfFilePathResolved)) {
|
|
14
|
+
if (!Buffer.isBuffer(expectedPdfFilePathOrBuffer) && !(0, fs_1.existsSync)(expectedPdfFilePathOrBuffer)) {
|
|
17
15
|
throw Error('Expected PDF file not found.');
|
|
18
16
|
}
|
|
19
|
-
const pdfToPngConvertOpts = opts?.pdfToPngConvertOptions ?? {
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
const pdfToPngConvertOpts = opts?.pdfToPngConvertOptions ?? {
|
|
18
|
+
viewportScale: 2.0,
|
|
19
|
+
outputFileMask: 'comparePdf',
|
|
20
|
+
};
|
|
21
|
+
const diffsOutputFolder = opts?.diffsOutputFolder
|
|
22
|
+
? opts.diffsOutputFolder
|
|
23
|
+
: (0, path_1.resolve)(`./comparePdfOutput`);
|
|
24
|
+
const compareThreshold = opts?.compareThreshold
|
|
25
|
+
? opts?.compareThreshold
|
|
26
|
+
: 0;
|
|
27
|
+
const excludedAreas = opts?.excludedAreas
|
|
28
|
+
? opts.excludedAreas
|
|
29
|
+
: [];
|
|
30
|
+
if (compareThreshold < 0) {
|
|
31
|
+
throw Error('Compare Threshold cannot be less than 0.');
|
|
32
|
+
}
|
|
22
33
|
const [actualPdfPngPages, expectedPdfPngPages] = await Promise.all([
|
|
23
|
-
(0, pdf_to_png_converter_1.pdfToPng)(
|
|
24
|
-
(0, pdf_to_png_converter_1.pdfToPng)(
|
|
34
|
+
(0, pdf_to_png_converter_1.pdfToPng)(actualPdfFilePathOrBuffer, pdfToPngConvertOpts),
|
|
35
|
+
(0, pdf_to_png_converter_1.pdfToPng)(expectedPdfFilePathOrBuffer, pdfToPngConvertOpts),
|
|
25
36
|
]);
|
|
26
37
|
if (actualPdfPngPages.length !== expectedPdfPngPages.length) {
|
|
27
38
|
return false;
|
|
28
39
|
}
|
|
29
40
|
let overallCompareResult = true;
|
|
30
|
-
const diffsOutputFolder = opts?.diffsOutputFolder ?? (0, path_1.resolve)(`./test-results/comparePdf/${(0, path_1.parse)(actualPdfFilePathResolved).base}_vs_${(0, path_1.parse)(expectedPdfFilePath).base}`);
|
|
31
41
|
actualPdfPngPages.forEach((actualPdfPngPage, index) => {
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
const compareResult = (0, png_visual_compare_1.default)(actualPdfPngPage.content, expectedPdfPngPages.find((expectedPdfPngPage) => expectedPdfPngPage.name === actualPdfPngPage.name).content, comparePngOpts);
|
|
38
|
-
if (compareResult > 0) {
|
|
42
|
+
const comparePngOpts = { ...opts?.pdfToPngConvertOptions, ...excludedAreas[index] };
|
|
43
|
+
comparePngOpts.diffFilePath = (0, path_1.resolve)(diffsOutputFolder, `diff_${actualPdfPngPage.name}`);
|
|
44
|
+
const expectedPdfPngPage = expectedPdfPngPages.find((expectedPdfPngPage) => expectedPdfPngPage.name === actualPdfPngPage.name);
|
|
45
|
+
const compareResult = (0, png_visual_compare_1.default)(actualPdfPngPage.content, expectedPdfPngPage.content, comparePngOpts);
|
|
46
|
+
if (compareResult > compareThreshold) {
|
|
39
47
|
overallCompareResult = false;
|
|
40
48
|
}
|
|
41
49
|
});
|
package/out/compare.pdf.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compare.pdf.js","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":";;;;;AAAA,2BAAgC;AAChC,+
|
|
1
|
+
{"version":3,"file":"compare.pdf.js","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":";;;;;AAAA,2BAAgC;AAChC,+BAA+B;AAC/B,+DAA+D;AAC/D,4EAAmE;AAGpD,KAAK,UAAU,UAAU,CACtC,yBAAmD,EACnD,2BAAqD,EACrD,IAAwB;IAExB,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC,yBAAyB,CAAC,IA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E,EAAE;QACpD,MAAM,cAAc,GAAsB,EAAE,GAAG,IAAI,EAAE,sBAAsB,EAAE,GAAG,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;QACvG,cAAc,CAAC,YAAY,GAAG,IAAA,cAAO,EAAC,iBAAiB,EAAE,QAAQ,gBAAgB,CAAC,IAAI,EAAE,CAAC,CAAC;QAE1F,MAAM,kBAAkB,GAAG,mBAAmB,CAAC,IAAI,CACjD,CAAC,kBAAkB,EAAE,EAAE,CAAC,kBAAkB,CAAC,IAAI,KAAK,gBAAgB,CAAC,IAAI,CACzD,CAAC;QACnB,MAAM,aAAa,GAAW,IAAA,4BAAU,EAAC,gBAAgB,CAAC,OAAO,EAAE,kBAAkB,CAAC,OAAO,EAAE,cAAc,CAAC,CAAC;QAE/G,IAAI,aAAa,GAAG,gBAAgB,EAAE;YACpC,oBAAoB,GAAG,KAAK,CAAC;SAC9B;IACH,CAAC,CAAC,CAAC;IAEH,OAAO,oBAAoB,CAAC;AAC9B,CAAC;AAvDD,6BAuDC"}
|
|
@@ -2,5 +2,6 @@ import { ExcludedPageArea, PdfToPngOptions } from '.';
|
|
|
2
2
|
export declare type ComparePdfOptions = {
|
|
3
3
|
diffsOutputFolder?: string;
|
|
4
4
|
pdfToPngConvertOptions?: PdfToPngOptions;
|
|
5
|
-
excludedAreas
|
|
5
|
+
excludedAreas?: ExcludedPageArea[];
|
|
6
|
+
compareThreshold?: number;
|
|
6
7
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf-visual-compare",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "Visual regression testing library for PDFs in Js/Ts",
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pdf",
|
|
7
7
|
"pdf regression",
|
|
@@ -39,18 +39,18 @@
|
|
|
39
39
|
"tsc": "tsc --pretty"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"pdf-to-png-converter": "0.
|
|
42
|
+
"pdf-to-png-converter": "2.0.0",
|
|
43
43
|
"png-visual-compare": "0.4.12"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@types/jest": "
|
|
47
|
-
"@types/node": "17.0.
|
|
48
|
-
"@typescript-eslint/eslint-plugin": "5.
|
|
49
|
-
"@typescript-eslint/parser": "5.
|
|
50
|
-
"eslint": "8.
|
|
51
|
-
"jest": "28.1.
|
|
52
|
-
"ts-jest": "28.0.
|
|
53
|
-
"ts-node": "10.
|
|
54
|
-
"typescript": "4.
|
|
46
|
+
"@types/jest": "28.1.1",
|
|
47
|
+
"@types/node": "17.0.41",
|
|
48
|
+
"@typescript-eslint/eslint-plugin": "5.27.1",
|
|
49
|
+
"@typescript-eslint/parser": "5.27.1",
|
|
50
|
+
"eslint": "8.17.0",
|
|
51
|
+
"jest": "28.1.1",
|
|
52
|
+
"ts-jest": "28.0.4",
|
|
53
|
+
"ts-node": "10.8.1",
|
|
54
|
+
"typescript": "4.7.3"
|
|
55
55
|
}
|
|
56
56
|
}
|