Skip to content

Commit

Permalink
Added a new method to read a specific page number
Browse files Browse the repository at this point in the history
  • Loading branch information
k-yle committed Aug 7, 2024
1 parent 39e856c commit 8604cc9
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 70 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

108 changes: 54 additions & 54 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
35 changes: 21 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export async function pdf(
): Promise<{
length: number;
metadata: PdfMetadata;
getPage(pageNumber: number): Promise<Buffer>;
[Symbol.asyncIterator](): AsyncIterator<Buffer, void, void>;
}> {
const data = await parseInput(input);
Expand All @@ -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 };
},
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tests/jsdom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down

0 comments on commit 8604cc9

Please sign in to comment.