Skip to content

Commit

Permalink
Remove deprecated canvas api
Browse files Browse the repository at this point in the history
  • Loading branch information
dichovsky committed Oct 13, 2024
1 parent 7310120 commit 784910f
Show file tree
Hide file tree
Showing 23 changed files with 820 additions and 981 deletions.
19 changes: 0 additions & 19 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,6 @@ on:
- 'release/*'

jobs:
macos:
runs-on: [self-hosted, macOS]
strategy:
matrix:
node: [ 20 ]
steps:
- name : Checkout
uses: actions/checkout@v2
- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: 'npm'
- name: Install dependencies
run: npm ci
- name: Build and lint
run: npm run build:test && npm run lint
- name: Test
run: npm test
ubuntu:
runs-on: ubuntu-latest
strategy:
Expand Down
44 changes: 0 additions & 44 deletions __tests__/canvas.test.ts

This file was deleted.

11 changes: 7 additions & 4 deletions __tests__/pdf.layers.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import { expect, test } from 'vitest'
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

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

pngPages.forEach((pngPage: PngPageOutput) => {
const expectedFilePath: string = resolve('./test-data/layers/expected', pngPage.name);
const actualFileContent: Buffer = readFileSync(pngPage.path);
const compareResult: number = comparePNG(actualFileContent, expectedFilePath);
const compareResult: number = comparePNG({
actualFile: actualFileContent,
expectedFile: expectedFilePath,
createExpectedFileIfMissing: true,
});

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

8 changes: 6 additions & 2 deletions __tests__/pdf.to.buffer.file-mask.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { existsSync, readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

Expand All @@ -15,7 +15,11 @@ test(`should convert PDF To PNG without saving to file, output file mask is defi

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

expect(existsSync(pngPage.path)).to.equal(false);
expect(compareResult).to.equal(0);
Expand Down
8 changes: 6 additions & 2 deletions __tests__/pdf.to.buffer.no-file-mask.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { existsSync, readFileSync } from 'node:fs';
import { parse, resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';
import { PDF_TO_PNG_OPTIONS_DEFAULTS } from '../src/const';
Expand All @@ -18,7 +18,11 @@ test(`should convert PDF To PNG without saving to file, output file mask is not
'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);
const compareResult: number = comparePNG({
actualFile: pngPage.content,
expectedFile: expectedFilePath,
createExpectedFileIfMissing: false,
});

expect(pngPage.name).to.equal(`${PDF_TO_PNG_OPTIONS_DEFAULTS.outputFileMask as string}_page_${index + 1}.png`);
expect(existsSync(pngPage.path)).to.equal(false);
Expand Down
8 changes: 6 additions & 2 deletions __tests__/pdf.to.buffer.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { existsSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

Expand All @@ -13,7 +13,11 @@ test(`should convert PDF To PNG buffer (without saving to file)`, async () => {

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

expect(existsSync(pngPage.path)).to.equal(false);
expect(compareResult).to.equal(0);
Expand Down
10 changes: 10 additions & 0 deletions __tests__/pdf.to.file.big.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';

test(`should convert BIG sample`, async () => {
const pdfFilePath: string = resolve('./test-data/10-page-sample.pdf');
const pngPages: PngPageOutput[] = await pdfToPng(pdfFilePath);

expect(pngPages.length).to.eq(10);
});
26 changes: 26 additions & 0 deletions __tests__/pdf.to.file.sample.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

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',
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({
actualFile: actualFileContent,
expectedFile: expectedFilePath,
createExpectedFileIfMissing: true,
});

expect(compareResult).to.equal(0);
});
});
8 changes: 6 additions & 2 deletions __tests__/pdf.to.file.specific-pages.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

Expand All @@ -17,7 +17,11 @@ test(`should convert specific PDF pages To PNG files`, async () => {
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);
const compareResult: number = comparePNG({
actualFile: actualFileContent,
expectedFile: expectedFilePath,
createExpectedFileIfMissing: true,
});

expect(compareResult).to.equal(0);
});
Expand Down
25 changes: 6 additions & 19 deletions __tests__/pdf.to.file.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { PngPageOutput, pdfToPng } from '../src';
import { comparePNG } from '../src/compare.png';

Expand All @@ -13,24 +13,11 @@ test(`should convert PDF To PNG files`, async () => {
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',
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);
const compareResult: number = comparePNG({
actualFile: actualFileContent,
expectedFile: expectedFilePath,
createExpectedFileIfMissing: true,
});

expect(compareResult).to.equal(0);
});
Expand Down
4 changes: 2 additions & 2 deletions __tests__/pdf.to.png.exceptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { pdfToPng } from '../src';

test(`should throw error when page index = 0 is requested`, async () => {
Expand All @@ -21,7 +21,7 @@ test(`should throw error when page index < 1 is requested`, async () => {
test(`should throw error when page index > then file contains and strictPagesToProcess is enabled`, async () => {
const pdfFilePath: string = resolve('./test-data/large_pdf.pdf');

await pdfToPng(pdfFilePath, { pagesToProcess: [1, 2, 1000], strictPagesToProcess: true }).catch((error: Error) => {
await pdfToPng(pdfFilePath, { pagesToProcess: [1, 2, 1000], strictPagesToProcess: true }).catch((error: Error) => {
expect(error.message).to.equal('Invalid pages requested, page number must be <= total pages');
});
});
18 changes: 9 additions & 9 deletions __tests__/props.to.pdf.doc.init.params.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { readdirSync } from 'node:fs';
import { DocumentInitParameters } from 'pdfjs-dist/types/src/display/api';
import { expect, test } from 'vitest';
import { DOCUMENT_INIT_PARAMS_DEFAULTS, STANDARD_CMAPS, STANDARD_FONTS } from '../src/const';
import { propsToPdfDocInitParams } from '../src/props.to.pdf.doc.init.params';
import { PdfToPngOptions } from '../src/types/pdf.to.png.options';
Expand All @@ -18,7 +18,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
enableXfa: true,
password: undefined,
},
},
Expand All @@ -32,7 +32,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
enableXfa: true,
password: undefined,
},
},
Expand All @@ -48,7 +48,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
enableXfa: true,
password: undefined,
},
},
Expand All @@ -64,7 +64,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 0,
disableFontFace: false,
useSystemFonts: false,
enableXfa: false,
enableXfa: true,
password: undefined,
},
},
Expand All @@ -80,7 +80,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 0,
disableFontFace: true,
useSystemFonts: true,
enableXfa: false,
enableXfa: true,
password: undefined,
},
},
Expand All @@ -96,7 +96,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
enableXfa: true,
password: '12345',
},
},
Expand All @@ -112,7 +112,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
verbosity: 1,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
enableXfa: true,
password: undefined,
},
},
Expand Down Expand Up @@ -164,7 +164,7 @@ for (const testData of testDataArray) {
const actualPdfDocInitParams: DocumentInitParameters = propsToPdfDocInitParams(testData.props);

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

const standardFonts = readdirSync(actualPdfDocInitParams.standardFontDataUrl as string, { recursive: true });
standardFonts.sort();
STANDARD_FONTS.sort();
Expand Down
8 changes: 6 additions & 2 deletions __tests__/protected.pdf.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, test } from 'vitest'
import { resolve } from 'node:path';
import { expect, test } from 'vitest';
import { pdfToPng, PngPageOutput } from '../src';
import { comparePNG } from '../src/compare.png';

Expand All @@ -12,7 +12,11 @@ test(`should convert protected PDF To PNG`, async () => {

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

expect(compareResult).to.equal(0);
});
Expand Down
Loading

0 comments on commit 784910f

Please sign in to comment.