UNPKG

pdf-visual-compare

Version:

Visual regression testing library for PDFs in Js/Ts without binary and OS dependencies.

68 lines 3.81 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; import { existsSync } from 'node:fs'; import { resolve } from 'node:path'; import { pdfToPng } from 'pdf-to-png-converter'; import comparePng from 'png-visual-compare'; import { DEFAULT_DIFFS_FOLDER } from './const.js'; export function comparePdf(actualPdf_1, expectedPdf_1) { return __awaiter(this, arguments, void 0, function* (actualPdf, expectedPdf, opts = {}) { var _a, _b, _c; validateInputFileType(actualPdf); validateInputFileType(expectedPdf); const pdfToPngConvertOpts = Object.assign({}, opts.pdfToPngConvertOptions); if (!pdfToPngConvertOpts.viewportScale) { pdfToPngConvertOpts.viewportScale = 2.0; } if (!pdfToPngConvertOpts.outputFileMaskFunc) { pdfToPngConvertOpts.outputFileMaskFunc = (pageNumber) => `comparePdf_${pageNumber}.png`; } const diffsOutputFolder = (_a = opts === null || opts === void 0 ? void 0 : opts.diffsOutputFolder) !== null && _a !== void 0 ? _a : DEFAULT_DIFFS_FOLDER; const compareThreshold = (_b = opts === null || opts === void 0 ? void 0 : opts.compareThreshold) !== null && _b !== void 0 ? _b : 0; const excludedAreas = (_c = opts === null || opts === void 0 ? void 0 : opts.excludedAreas) !== null && _c !== void 0 ? _c : []; if (compareThreshold < 0) { throw Error('Compare Threshold cannot be less than 0.'); } let [actualPdfPngPages, expectedPdfPngPages] = yield Promise.all([ pdfToPng(actualPdf, pdfToPngConvertOpts), pdfToPng(expectedPdf, pdfToPngConvertOpts), ]); if (actualPdfPngPages.length < expectedPdfPngPages.length) { [actualPdfPngPages, expectedPdfPngPages] = [expectedPdfPngPages, actualPdfPngPages]; } let documentCompareResult = true; actualPdfPngPages.forEach((pngPage, index) => { var _a; const comparePngOpts = Object.assign(Object.assign(Object.assign({}, opts === null || opts === void 0 ? void 0 : opts.pdfToPngConvertOptions), excludedAreas[index]), { throwErrorOnInvalidInputData: false }); comparePngOpts.diffFilePath = resolve(diffsOutputFolder, `diff_${pngPage.name}`); const pngPageOutputToCompareWith = expectedPdfPngPages.find((p) => p.name === pngPage.name); const pageCompareResult = comparePng(pngPage.content, (_a = pngPageOutputToCompareWith === null || pngPageOutputToCompareWith === void 0 ? void 0 : pngPageOutputToCompareWith.content) !== null && _a !== void 0 ? _a : '', comparePngOpts); if (pageCompareResult > compareThreshold) { documentCompareResult = false; } }); return documentCompareResult; }); } function validateInputFileType(inputFile) { if (Buffer.isBuffer(inputFile)) { return; } if (typeof inputFile === 'string') { if (existsSync(inputFile)) { return; } else { throw Error(`PDF file not found: ${inputFile}`); } } throw Error(`Unknown input file type.`); } //# sourceMappingURL=comparePdf.js.map