Skip to content

Commit

Permalink
update dfjs-dist to version ^4.0.269
Browse files Browse the repository at this point in the history
  • Loading branch information
dichovsky committed Nov 28, 2023
1 parent 3a3376a commit 5d8dad2
Show file tree
Hide file tree
Showing 20 changed files with 1,509 additions and 4,263 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);
});
});
27 changes: 24 additions & 3 deletions __tests__/pdf.to.file.ts
@@ -1,19 +1,40 @@
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);
});
});
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);
});
4 changes: 3 additions & 1 deletion __tests__/props.to.pdf.doc.init.params.ts
@@ -1,3 +1,5 @@
import { expect } from 'chai';
import { test } from 'mocha';
import { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
import { propsToPdfDocInitParams } from '../src/props.to.pdf.doc.init.params';
import { PdfToPngOptions } from '../src/types/pdf.to.png.options';
Expand Down Expand Up @@ -157,6 +159,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);
});
}
8 changes: 5 additions & 3 deletions __tests__/protected.pdf.ts
@@ -1,18 +1,20 @@
import { expect } from 'chai';
import { test } from 'mocha';
import { resolve } from 'node:path';
import { pdfToPng, PngPageOutput } from '../src';
import { comparePNG } from '../src/compare.png';

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

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

expect(compareResult).toBe(0);
expect(compareResult).to.equal(0);
});
});
3 changes: 2 additions & 1 deletion dockerfile
@@ -1,7 +1,8 @@
FROM node:20
RUN apt-get update && apt-get install -y build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
WORKDIR /usr/pkg/
COPY . .
RUN apt-get update && apt-get install -y build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev
RUN npm ci
RUN npm run build

CMD npm run docker:test
12 changes: 0 additions & 12 deletions jest.config.ts

This file was deleted.

0 comments on commit 5d8dad2

Please sign in to comment.