diff --git a/CHANGELOG.md b/CHANGELOG.md index 2cfd5e0..362f423 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## 4.1.0 (2024-08-07) + +- Added a new method to read a specific page number + ## 4.0.0 (2024-06-08) - 💥 BREAKING CHANGE: Drop support for node v16. The minimum version is now v18 diff --git a/README.md b/README.md index ddc01bb..e642468 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,10 @@ async function main() { await fs.writeFile(`page${counter}.png`, image); counter++; } + + + // you can also read a specific page number: + const page12buffer = await document.getPage(12) } main(); ``` diff --git a/package-lock.json b/package-lock.json index 375b86c..78655ef 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pdf-to-img", - "version": "4.0.0", + "version": "4.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "pdf-to-img", - "version": "4.0.0", + "version": "4.1.0", "license": "MIT", "dependencies": { "canvas": "2.11.2", diff --git a/package.json b/package.json index 7c894af..7b34956 100644 --- a/package.json +++ b/package.json @@ -1,54 +1,54 @@ -{ - "name": "pdf-to-img", - "version": "4.0.0", - "author": "Kyle Hensel", - "description": "📃📸 Converts PDFs to images in nodejs", - "exports": "./dist/index.js", - "types": "dist/index.d.ts", - "type": "module", - "license": "MIT", - "files": [ - "dist" - ], - "keywords": [ - "pdf", - "pdfjs", - "pdf-to-img", - "pdf-to-png", - "vitest", - "jest" - ], - "repository": "https://github.com/k-yle/pdf-to-img", - "scripts": { - "build": "tsc && rm -rf dist && mv temp/src dist && rm -rf temp", - "lint": "eslint --ignore-path .gitignore .", - "test": "vitest --pool=forks", - "trypublish": "npm publish --provenance || true" - }, - "bin": { - "pdf2img": "./bin/cli.mjs" - }, - "engines": { - "node": ">=18" - }, - "engineStrict": true, - "dependencies": { - "canvas": "2.11.2", - "pdfjs-dist": "4.2.67" - }, - "devDependencies": { - "@rushstack/eslint-patch": "^1.5.1", - "@types/jest-image-snapshot": "^6.2.1", - "@types/node": "^20.8.6", - "@types/pdfjs-dist": "2.10.377", - "@vitest/coverage-v8": "^1.6.0", - "eslint": "^8.50.0", - "eslint-config-kyle": "^14.12.3", - "jest-image-snapshot": "^6.2.0", - "typescript": "^5.2.2", - "vitest": "^1.6.0" - }, - "prettier": { - "trailingComma": "es5" - } -} +{ + "name": "pdf-to-img", + "version": "4.1.0", + "author": "Kyle Hensel", + "description": "📃📸 Converts PDFs to images in nodejs", + "exports": "./dist/index.js", + "types": "dist/index.d.ts", + "type": "module", + "license": "MIT", + "files": [ + "dist" + ], + "keywords": [ + "pdf", + "pdfjs", + "pdf-to-img", + "pdf-to-png", + "vitest", + "jest" + ], + "repository": "https://github.com/k-yle/pdf-to-img", + "scripts": { + "build": "tsc && rm -rf dist && mv temp/src dist && rm -rf temp", + "lint": "eslint --ignore-path .gitignore .", + "test": "vitest --pool=forks", + "trypublish": "npm publish --provenance || true" + }, + "bin": { + "pdf2img": "./bin/cli.mjs" + }, + "engines": { + "node": ">=18" + }, + "engineStrict": true, + "dependencies": { + "canvas": "2.11.2", + "pdfjs-dist": "4.2.67" + }, + "devDependencies": { + "@rushstack/eslint-patch": "^1.5.1", + "@types/jest-image-snapshot": "^6.2.1", + "@types/node": "^20.8.6", + "@types/pdfjs-dist": "2.10.377", + "@vitest/coverage-v8": "^1.6.0", + "eslint": "^8.50.0", + "eslint-config-kyle": "^14.12.3", + "jest-image-snapshot": "^6.2.0", + "typescript": "^5.2.2", + "vitest": "^1.6.0" + }, + "prettier": { + "trailingComma": "es5" + } +} diff --git a/src/index.ts b/src/index.ts index e813338..7a29739 100644 --- a/src/index.ts +++ b/src/index.ts @@ -73,6 +73,7 @@ export async function pdf( ): Promise<{ length: number; metadata: PdfMetadata; + getPage(pageNumber: number): Promise; [Symbol.asyncIterator](): AsyncIterator; }> { const data = await parseInput(input); @@ -91,30 +92,36 @@ export async function pdf( const metadata = await pdfDocument.getMetadata(); + async function getPage(pageNumber: number) { + const page = await pdfDocument.getPage(pageNumber); + + const viewport = page.getViewport({ scale: options.scale ?? 1 }); + + const { canvas, context } = canvasFactory.create( + viewport.width, + viewport.height + ); + + await page.render({ + canvasContext: context, + viewport, + }).promise; + + return canvas.toBuffer(); + } + return { length: pdfDocument.numPages, metadata: sanitize(metadata.info), + getPage, [Symbol.asyncIterator]() { return { pg: 0, async next(this: { pg: number }) { if (this.pg < pdfDocument.numPages) { this.pg += 1; - const page = await pdfDocument.getPage(this.pg); - - const viewport = page.getViewport({ scale: options.scale ?? 1 }); - - const { canvas, context } = canvasFactory.create( - viewport.width, - viewport.height - ); - - await page.render({ - canvasContext: context, - viewport, - }).promise; - return { done: false, value: canvas.toBuffer() }; + return { done: false, value: await getPage(this.pg) }; } return { done: true, value: undefined }; }, diff --git a/tests/__image_snapshots__/jsdom-test-ts-multipage-pdf-can-read-a-specific-page-number-1-snap.png b/tests/__image_snapshots__/jsdom-test-ts-multipage-pdf-can-read-a-specific-page-number-1-snap.png new file mode 100644 index 0000000..d2966e0 Binary files /dev/null and b/tests/__image_snapshots__/jsdom-test-ts-multipage-pdf-can-read-a-specific-page-number-1-snap.png differ diff --git a/tests/jsdom.test.ts b/tests/jsdom.test.ts index df5f714..3654652 100644 --- a/tests/jsdom.test.ts +++ b/tests/jsdom.test.ts @@ -35,6 +35,11 @@ describe("multipage.pdf", () => { expect(page).toMatchImageSnapshot(); } }); + + it("can read a specific page number", async () => { + const document = await pdf("./tests/multipage.pdf"); + expect(await document.getPage(2)).toMatchImageSnapshot(); + }); }); describe("test-pattern.pdf", () => {