Skip to content

Commit

Permalink
Merge pull request #42 from dichovsky/updatePdfJsToVersion4
Browse files Browse the repository at this point in the history
update dfjs-dist to version ^4.0.269
  • Loading branch information
dichovsky committed Mar 14, 2024
2 parents b1dbd67 + c1d6614 commit 6591d61
Show file tree
Hide file tree
Showing 24 changed files with 1,608 additions and 4,239 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Expand Up @@ -10,5 +10,6 @@
"rules": {
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-explicit-any": 0
}
},
"ignorePatterns": ["*.js", "*.d.ts"]
}
6 changes: 6 additions & 0 deletions .mocharc.js
@@ -0,0 +1,6 @@
module.exports = {
reporter: 'spec',
timeout: '60000',
spec: ['out/__tests__/**/*.js'],
parallel: true
};
41 changes: 0 additions & 41 deletions __tests__/buffer.pdf.to.buffer.ts

This file was deleted.

23 changes: 0 additions & 23 deletions __tests__/pdf.pages.to.file.ts

This file was deleted.

50 changes: 44 additions & 6 deletions __tests__/pdf.to.buffer.ts
@@ -1,19 +1,57 @@
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect } from 'chai';
import { test } from 'mocha';
import { existsSync, readFileSync } from 'node:fs';
import { parse, resolve } from 'node:path';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';
import { PDF_TO_PNG_OPTIONS_DEFAULTS } from '../src/const';

const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');
const pdfBuffer: Buffer = readFileSync(pdfFilePath);

test(`should convert PDF To PNG buffer (without saving to file)`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath, {
viewportScale: 2.0,
});

pngPages.forEach((pngPage: PngPageOutput) => {
const expectedFilePath: string = resolve('test-data/pdf.to.buffer/expected', pngPage.name);
const expectedFilePath: string = resolve('./test-data/pdf.to.buffer/expected', pngPage.name);
const compareResult: number = comparePNG(pngPage.content, expectedFilePath);

expect(existsSync(pngPage.path)).to.equal(false);
expect(compareResult).to.equal(0);
});
});

test(`should convert PDF To PNG without saving to file, output file mask is defined`, async () => {
const pngPages: PngPageOutput[] = await pdfToPng(pdfBuffer, {
viewportScale: 2.0,
outputFileMask: 'large_pdf',
});

pngPages.forEach((pngPage: PngPageOutput) => {
const expectedFilePath: string = resolve('./test-data/pdf.to.buffer/expected', pngPage.name);
const compareResult: number = comparePNG(pngPage.content, expectedFilePath);

expect(existsSync(pngPage.path)).to.equal(false);
expect(compareResult).to.equal(0);
});
});

test(`should convert PDF To PNG without saving to file, output file mask is not defined`, async () => {
const pngPages: PngPageOutput[] = await pdfToPng(pdfBuffer, {
viewportScale: 2.0,
});

pngPages.forEach((pngPage: PngPageOutput, index: number) => {
const expectedFilePath: string = resolve(
'test-data/pdf.to.buffer/expected',
pngPage.name.replace(PDF_TO_PNG_OPTIONS_DEFAULTS.outputFileMask as string, parse(pdfFilePath).name),
);
const compareResult: number = comparePNG(pngPage.content, expectedFilePath);

expect(existsSync(pngPage.path)).toBe(false);
expect(compareResult).toBe(0);
expect(pngPage.name).to.equal(`${PDF_TO_PNG_OPTIONS_DEFAULTS.outputFileMask as string}_page_${index + 1}.png`);
expect(existsSync(pngPage.path)).to.equal(false);
expect(compareResult).to.equal(0);
});
});
46 changes: 43 additions & 3 deletions __tests__/pdf.to.file.ts
@@ -1,19 +1,59 @@
import { expect } from 'chai';
import { test } from 'mocha';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

test(`should convert PDF To PNG files`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath, {
outputFolder: 'test-results/pdf.to.file/actual',
});

pngPages.forEach((pngPage: PngPageOutput) => {
const expectedFilePath: string = resolve('test-data/pdf.to.file/expected', pngPage.name);
const expectedFilePath: string = resolve('./test-data/pdf.to.file/expected', pngPage.name);
const actualFileContent: Buffer = readFileSync(pngPage.path);
const compareResult: number = comparePNG(actualFileContent, expectedFilePath);

expect(compareResult).toBe(0);
expect(compareResult).to.equal(0);
});
});

test(`should convert specific PDF pages To PNG files`, async () => {
const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath, {
outputFolder: 'test-results/pdf.pages.to.file/actual',
pagesToProcess: [-1, 0, 2, 5, 7, 99, 999999],
});

// Should skip page 99 since it's beyond PDF bounds
expect(pngPages.length).to.equal(3);

pngPages.forEach((pngPage: PngPageOutput) => {
const expectedFilePath: string = resolve('./test-data/pdf.to.file/expected', pngPage.name);
const actualFileContent: Buffer = readFileSync(pngPage.path);
const compareResult: number = comparePNG(actualFileContent, expectedFilePath);

expect(compareResult).to.equal(0);
});
});

test(`should convert simple sample`, async () => {
const pdfFilePath: string = resolve('./test-data/sample.pdf');
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath, {
outputFolder: 'test-results/sample/actual',
disableFontFace: true,
useSystemFonts: false,
viewportScale: 2.0,
});

expect(pngPages.length).to.equal(2);
pngPages.forEach((pngPage: PngPageOutput) => {
const expectedFilePath: string = resolve('./test-data/sample/expected', pngPage.name);
const actualFileContent: Buffer = readFileSync(pngPage.path);
const compareResult: number = comparePNG(actualFileContent, expectedFilePath);

expect(compareResult).to.equal(0);
});
});
29 changes: 17 additions & 12 deletions __tests__/pdf.to.png.exceptions.ts
@@ -1,26 +1,31 @@
import { test } from 'mocha';
import { resolve } from 'node:path';
import { pdfToPng } from '../src';

const chai = require('chai');
const expect = chai.expect;
chai.use(require('chai-as-promised'));

test(`should throw error when page index = 0 is requested`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');

await expect(async () => {
await pdfToPng(pdfFilePath, { pagesToProcess: [0, 1, 2], strictPagesToProcess: true });
}).rejects.toThrow('Invalid pages requested');
await expect(pdfToPng(pdfFilePath, { pagesToProcess: [0, 1, 2], strictPagesToProcess: true })).to.be.rejectedWith(
Error,
);
});

test(`should throw error when page index < 1 is requested`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');

await expect(async () => {
await pdfToPng(pdfFilePath, { pagesToProcess: [1, 2, -1], strictPagesToProcess: true });
}).rejects.toThrow('Invalid pages requested');
await expect(pdfToPng(pdfFilePath, { pagesToProcess: [1, 2, -1], strictPagesToProcess: true })).to.be.rejectedWith(
Error,
);
});

test(`should throw error when page index > then file contains and strictPagesToProcess is enabled`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');

await expect(async () => {
await pdfToPng(pdfFilePath, { pagesToProcess: [1, 2, 1000], strictPagesToProcess: true });
}).rejects.toThrow('Invalid pages requested');
await expect(
pdfToPng(pdfFilePath, { pagesToProcess: [1, 2, 1000], strictPagesToProcess: true }),
).to.be.rejectedWith(Error);
});
44 changes: 25 additions & 19 deletions __tests__/props.to.pdf.doc.init.params.ts
@@ -1,14 +1,20 @@
import { expect } from 'chai';
import { test } from 'mocha';
import { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
import { DOCUMENT_INIT_PARAMS_DEFAULTS } from '../src/const';
import { propsToPdfDocInitParams } from '../src/props.to.pdf.doc.init.params';
import { PdfToPngOptions } from '../src/types/pdf.to.png.options';

const cMapUrl: string = DOCUMENT_INIT_PARAMS_DEFAULTS.cMapUrl as string;
const standardFontDataUrl: string = DOCUMENT_INIT_PARAMS_DEFAULTS.standardFontDataUrl as string;

const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitParams: DocumentInitParameters }[] = [
{
id: 'props undefined',
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
Expand All @@ -20,9 +26,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
id: 'props are empty',
props: {},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
Expand All @@ -36,9 +42,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
viewportScale: 15,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
Expand All @@ -52,9 +58,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
disableFontFace: false,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: false,
useSystemFonts: false,
Expand All @@ -68,9 +74,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
useSystemFonts: true,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: true,
useSystemFonts: true,
Expand All @@ -84,9 +90,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
pdfFilePassword: '12345',
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
Expand All @@ -100,9 +106,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosityLevel: 1,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 1,
disableFontFace: true,
useSystemFonts: false,
Expand All @@ -116,9 +122,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
enableXfa: true,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
Expand All @@ -141,9 +147,9 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosityLevel: 2,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapUrl,
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
standardFontDataUrl,
verbosity: 2,
disableFontFace: false,
useSystemFonts: true,
Expand All @@ -157,6 +163,6 @@ for (const testData of testDataArray) {
test(`should convert props to PdfDocInitParams when ${testData.id}`, async () => {
const actualPdfDocInitParams: DocumentInitParameters = propsToPdfDocInitParams(testData.props);

expect(actualPdfDocInitParams).toStrictEqual(testData.expectedPdfDocInitParams);
expect(actualPdfDocInitParams).to.deep.equal(testData.expectedPdfDocInitParams);
});
}

0 comments on commit 6591d61

Please sign in to comment.