Skip to content

Commit

Permalink
Merge pull request #27 from dichovsky/version3
Browse files Browse the repository at this point in the history
Version3
  • Loading branch information
dichovsky committed Apr 9, 2023
2 parents 056f0b6 + 56c97cd commit 0751379
Show file tree
Hide file tree
Showing 33 changed files with 1,057 additions and 979 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Expand Up @@ -8,7 +8,7 @@ jobs:
runs-on: macos-11
strategy:
matrix:
node: [ 16, 18 ]
node: [ 18, 19 ]
steps:
- uses: actions/checkout@v2
- name: Test on macos-11 Use Node.js ${{ matrix.node }}
Expand All @@ -23,7 +23,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ 16, 18 ]
node: [ 18, 19 ]
steps:
- uses: actions/checkout@v2
- name: Test on Ubuntu Use Node.js ${{ matrix.node }}
Expand All @@ -38,7 +38,7 @@ jobs:
runs-on: windows-latest
strategy:
matrix:
node: [ 16, 18 ]
node: [ 18, 19 ]
steps:
- uses: actions/checkout@v2
- name: Test on Windows Use Node.js ${{ matrix.node }}
Expand Down
12 changes: 6 additions & 6 deletions .prettierrc
Expand Up @@ -7,11 +7,11 @@
"useTabs": false,
"arrowParens": "always",
"overrides": [
{
"files": ["*.json", ".*.json"],
"options": {
"tabWidth": 4
{
"files": ["*.json", ".*.json"],
"options": {
"tabWidth": 4
}
}
}
]
}
}
8 changes: 6 additions & 2 deletions README.md
Expand Up @@ -30,12 +30,13 @@ test(`Convert PDF To PNG`, async () => {
{
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.
useSystemFonts: false, // When `true`, fonts that aren't embedded in the PDF document will fallback to a system font. Default value is false.
enableXfa: false, // Render Xfa forms if any. Default value is false.
viewportScale: 2.0, // The desired scale of PNG viewport. Default value is 1.0.
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.
outputFileMask: 'buffer', // Output filename mask. Default value is 'buffer'.
pdfFilePassword: 'pa$$word', // Password for encrypted PDF.
pagesToProcess: [1, 3, 11], // Subset of pages to convert (first page = 1), other pages will be skipped if specified.
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.
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.
verbosityLevel: 0 // Verbosity level. ERRORS: 0, WARNINGS: 1, INFOS: 5. Default value is 0.
});
...
Expand All @@ -46,9 +47,12 @@ test(`Convert PDF To PNG`, async () => {

```javascript
{
name: string; // PNG page name in a format `{pdfFileName}_page_{pdfPageNumber}.png`,
pageNumber: number; // Page number in PDF file
name: string; // PNG page name in a format `{pdfFileName}_page_{pdfPageNumber}.png`
content: Buffer; // PNG page Buffer content
path: string; // Path to the rendered PNG page file (empty string and if outputFilesFolder is not provided)
width: number; // PNG page width
height: number; // PNG page height
}
```

Expand Down
14 changes: 6 additions & 8 deletions __tests__/buffer.pdf.to.buffer.ts
@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from 'fs';
import { parse, resolve } from 'path';
import comparePng from 'png-visual-compare';
import { pdfToPng, PngPageOutput } from '../src';
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');
Expand All @@ -15,8 +15,7 @@ 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 expectedFileContent: Buffer = readFileSync(expectedFilePath);
const compareResult: number = comparePng(pngPage.content, expectedFileContent);
const compareResult: number = comparePNG(pngPage.content, expectedFilePath);

expect(existsSync(pngPage.path)).toBe(false);
expect(compareResult).toBe(0);
Expand All @@ -33,8 +32,7 @@ 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 expectedFileContent: Buffer = readFileSync(expectedFilePath);
const compareResult: number = comparePng(pngPage.content, expectedFileContent);
const compareResult: number = comparePNG(pngPage.content, expectedFilePath);

expect(pngPage.name).toBe(`${PDF_TO_PNG_OPTIONS_DEFAULTS.outputFileMask as string}_page_${index + 1}.png`);
expect(existsSync(pngPage.path)).toBe(false);
Expand Down
9 changes: 4 additions & 5 deletions __tests__/pdf.pages.to.file.ts
@@ -1,7 +1,7 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import comparePng from 'png-visual-compare';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { pdfToPng, PngPageOutput } from '../src';
import { comparePNG } from '../src/compare.png';

test(`should convert specific PDF pages To PNG files`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
Expand All @@ -15,9 +15,8 @@ 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 expectedFileContent: Buffer = readFileSync(expectedFilePath);
const actualFileContent: Buffer = readFileSync(pngPage.path);
const compareResult: number = comparePng(actualFileContent, expectedFileContent);
const compareResult: number = comparePNG(actualFileContent, expectedFilePath);

expect(compareResult).toBe(0);
});
Expand Down
11 changes: 5 additions & 6 deletions __tests__/pdf.to.buffer.ts
@@ -1,7 +1,7 @@
import { existsSync, readFileSync } from 'fs';
import { resolve } from 'path';
import comparePng from 'png-visual-compare';
import { pdfToPng, PngPageOutput } from '../src';
import { existsSync } 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 buffer (without saving to file)`, async () => {
const pdfFilePath: string = resolve('test-data/large_pdf.pdf');
Expand All @@ -11,8 +11,7 @@ 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 expectedFileContent: Buffer = readFileSync(expectedFilePath);
const compareResult: number = comparePng(pngPage.content, expectedFileContent);
const compareResult: number = comparePNG(pngPage.content, expectedFilePath);

expect(existsSync(pngPage.path)).toBe(false);
expect(compareResult).toBe(0);
Expand Down
9 changes: 4 additions & 5 deletions __tests__/pdf.to.file.ts
@@ -1,7 +1,7 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import comparePng from 'png-visual-compare';
import { readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import { pdfToPng, PngPageOutput } 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');
Expand All @@ -11,9 +11,8 @@ 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 expectedFileContent: Buffer = readFileSync(expectedFilePath);
const actualFileContent: Buffer = readFileSync(pngPage.path);
const compareResult: number = comparePng(actualFileContent, expectedFileContent);
const compareResult: number = comparePNG(actualFileContent, expectedFilePath);

expect(compareResult).toBe(0);
});
Expand Down
2 changes: 1 addition & 1 deletion __tests__/pdf.to.png.exceptions.ts
@@ -1,4 +1,4 @@
import { resolve } from 'path';
import { resolve } from 'node:path';
import { pdfToPng } from '../src';

test(`should throw "PDF file not found" exception`, async () => {
Expand Down
35 changes: 34 additions & 1 deletion __tests__/props.to.pdf.doc.init.params.ts
Expand Up @@ -8,9 +8,11 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
password: undefined,
},
},
Expand All @@ -20,9 +22,11 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
password: undefined,
},
},
Expand All @@ -34,9 +38,11 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
password: undefined,
},
},
Expand All @@ -48,9 +54,11 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: false,
useSystemFonts: false,
enableXfa: false,
password: undefined,
},
},
Expand All @@ -62,9 +70,11 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: true,
useSystemFonts: true,
enableXfa: false,
password: undefined,
},
},
Expand All @@ -76,9 +86,11 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
password: '12345',
},
},
Expand All @@ -90,9 +102,27 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 1,
disableFontFace: true,
useSystemFonts: false,
enableXfa: false,
password: undefined,
},
},
{
id: 'only enableXfa is specified',
props: {
enableXfa: true,
},
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 0,
disableFontFace: true,
useSystemFonts: false,
enableXfa: true,
password: undefined,
},
},
Expand All @@ -102,6 +132,7 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
viewportScale: 3.0,
disableFontFace: false,
useSystemFonts: true,
enableXfa: true,
pdfFilePassword: 'pdfFilePassword',
outputFolder: 'outputFolder',
outputFileMask: 'outputFileMask',
Expand All @@ -112,17 +143,19 @@ const testDataArray: { id: string; props?: PdfToPngOptions; expectedPdfDocInitPa
expectedPdfDocInitParams: {
cMapUrl: '../node_modules/pdfjs-dist/cmaps/',
cMapPacked: true,
standardFontDataUrl: '../node_modules/pdfjs-dist/standard_fonts/',
verbosity: 2,
disableFontFace: false,
useSystemFonts: true,
enableXfa: true,
password: 'pdfFilePassword',
},
},
];

for (const testData of testDataArray) {
test(`should convert props to PdfDocInitParams when ${testData.id}`, async () => {
const actualPdfDocInitParams = propsToPdfDocInitParams(testData.props);
const actualPdfDocInitParams: DocumentInitParameters = propsToPdfDocInitParams(testData.props);

expect(actualPdfDocInitParams).toStrictEqual(testData.expectedPdfDocInitParams);
});
Expand Down
8 changes: 3 additions & 5 deletions __tests__/protected.pdf.ts
@@ -1,7 +1,6 @@
import { readFileSync } from 'fs';
import { resolve } from 'path';
import comparePng from 'png-visual-compare';
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');
Expand All @@ -12,8 +11,7 @@ test(`should convert protected PDF To PNG`, async () => {

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

expect(compareResult).toBe(0);
});
Expand Down

0 comments on commit 0751379

Please sign in to comment.