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 CHANGED
@@ -1,4 +1,45 @@
1
1
  # pdf-visual-compare
2
- Visual regression testing library for PDFs in Js/Ts
2
+
3
+ Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.
3
4
 
4
5
  [![Tests on push](https://github.com/dichovsky/pdf-visual-compare/actions/workflows/test.yml/badge.svg?branch=main)](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
+ [!["Buy Me A Coffee"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://buymeacoffee.com/dichovsky)
@@ -1,2 +1,2 @@
1
1
  import { ComparePdfOptions } from './types';
2
- export default function comparePdf(actualPdfFilePath: string, expectedPdfFilePath: string, opts?: ComparePdfOptions): Promise<boolean>;
2
+ export default function comparePdf(actualPdfFilePathOrBuffer: string | ArrayBufferLike, expectedPdfFilePathOrBuffer: string | ArrayBufferLike, opts?: ComparePdfOptions): Promise<boolean>;
@@ -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(actualPdfFilePath, expectedPdfFilePath, opts) {
11
- const actualPdfFilePathResolved = (0, path_1.resolve)(actualPdfFilePath);
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
- const expectedPdfFilePathResolved = (0, path_1.resolve)(expectedPdfFilePath);
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
- pdfToPngConvertOpts.viewportScale = pdfToPngConvertOpts.viewportScale ?? 2.0;
21
- pdfToPngConvertOpts.outputFileMask = pdfToPngConvertOpts.outputFileMask ?? 'comparePdf';
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)(actualPdfFilePathResolved, pdfToPngConvertOpts),
24
- (0, pdf_to_png_converter_1.pdfToPng)((0, path_1.resolve)(expectedPdfFilePath), pdfToPngConvertOpts),
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
- let comparePngOpts = {};
33
- if (opts?.excludedAreas && opts.excludedAreas[index].excludedAreas) {
34
- comparePngOpts = { ...opts.excludedAreas[index] };
35
- }
36
- comparePngOpts.diffFilePath = comparePngOpts.diffFilePath ?? (0, path_1.resolve)(diffsOutputFolder, `diff_${actualPdfPngPage.name}`);
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
  });
@@ -1 +1 @@
1
- {"version":3,"file":"compare.pdf.js","sourceRoot":"","sources":["../src/compare.pdf.ts"],"names":[],"mappings":";;;;;AAAA,2BAAgC;AAChC,+BAAsC;AACtC,+DAA+D;AAC/D,4EAAmE;AAGpD,KAAK,UAAU,UAAU,CAAC,iBAAyB,EAAE,mBAA2B,EAAE,IAAwB;IACrH,MAAM,yBAAyB,GAAW,IAAA,cAAO,EAAC,iBAAiB,CAAC,CAAC;IACrE,IAAI,CAAC,IAAA,eAAU,EAAC,yBAAyB,CAAC,EAAE;QACxC,MAAM,KAAK,CAAC,4BAA4B,CAAC,CAAC;KAC7C;IAED,MAAM,2BAA2B,GAAW,IAAA,cAAO,EAAC,mBAAmB,CAAC,CAAC;IACzE,IAAI,CAAC,IAAA,eAAU,EAAC,2BAA2B,CAAC,EAAE;QAC1C,MAAM,KAAK,CAAC,8BAA8B,CAAC,CAAC;KAC/C;IAED,MAAM,mBAAmB,GAAoB,IAAI,EAAE,sBAAsB,IAAI,EAAE,CAAC;IAChF,mBAAmB,CAAC,aAAa,GAAG,mBAAmB,CAAC,aAAa,IAAI,GAAG,CAAC;IAC7E,mBAAmB,C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,KAAK,CAAC,0CAA0C,CAAC,CAAC;KACzD;IAED,MAAM,CAAC,iBAAiB,EAAE,mBAAmB,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACjE,IAAA,+BAAQ,EAAC,yBAAyB,EAAE,mBAAmB,CAAC;QACxD,IAAA,+BAAQ,EAAC,2BAA2B,EAAE,mBAAmB,CAAC;KAC3D,CAAC,CAAC;IAEH,IAAI,iBAAiB,CAAC,MAAM,KAAK,mBAAmB,CAAC,MAAM,EAAE;QAC3D,OAAO,KAAK,CAAC;KACd;IAED,IAAI,oBAAoB,GAAG,IAAI,CAAC;IAChC,iBAAiB,CAAC,OAAO,CAAC,CAAC,gBAAgB,EAAE,KAAK,EAAE,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: ExcludedPageArea[];
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": "0.0.10",
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.2.5",
42
+ "pdf-to-png-converter": "2.0.0",
43
43
  "png-visual-compare": "0.4.12"
44
44
  },
45
45
  "devDependencies": {
46
- "@types/jest": "27.5.0",
47
- "@types/node": "17.0.31",
48
- "@typescript-eslint/eslint-plugin": "5.22.0",
49
- "@typescript-eslint/parser": "5.22.0",
50
- "eslint": "8.15.0",
51
- "jest": "28.1.0",
52
- "ts-jest": "28.0.1",
53
- "ts-node": "10.7.0",
54
- "typescript": "4.6.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
  }