@nova-slides/pdf-to-img
Version:
ššø Converts PDFs to images in nodejs
99 lines (69 loc) ⢠2.64 kB
Markdown
This version is based on [pdf-to-img v3](https://github.com/k-yle/pdf-to-img) and adds the following options
- `maxWidth` Maximum width of the output image
- `maxHeight` Maximum height of the output image
- `maintainAspectRatio` If true, maintain aspect ratio when maxWidth or maxHeight are specified. Defaults to true.
[](https://github.com/k-yle/pdf-to-img/actions)
[](https://coveralls.io/github/k-yle/pdf-to-img?branch=main)
[](https://badge.fury.io/js/pdf-to-img)
[](https://www.npmjs.com/package/pdf-to-img)

ššø Converts PDFs to images in nodejs.
Useful for unit tests of PDFs
Supports nodejs v16.17+, and comes with a CLI.
```sh
npm install -S pdf-to-img
```
NodeJS:
```js
const { promises: fs } = require("node:fs");
const { pdf } = require("pdf-to-img");
async function main() {
let counter = 1;
const document = await pdf("example.pdf", { scale: 3 });
for await (const image of document) {
await fs.writeFile(`page${counter}.png`, image);
counter++;
}
}
main();
```
Using jest and [jest-image-snapshot](https://npm.im/jest-image-snapshot):
```js
const { pdf } = require("pdf-to-img");
it("generates a PDF", async () => {
for await (const page of await pdf("example.pdf")) {
expect(page).toMatchImageSnapshot();
}
});
// or if you want access to more details:
it("generates a PDF with 2 pages", async () => {
const doc = await pdf("example.pdf");
expect(doc.length).toBe(2);
expect(doc.metadata).toEqual({ ... });
for await (const page of doc) {
expect(page).toMatchImageSnapshot();
}
});
```
The `pdf` function accepts either a path to the file on disk, or a data URL (e.g. `data:application/pdf;base64,...`)
### Options
You can supply a second argument which is an object of options:
```js
const doc = await pdf("example.pdf", {
password: "...", // if the PDF is encrypted
scale: 2.0, // use this for PDFs with high resolution images if the generated image is low quality
});
```
```sh
npm i -g pdf-to-img@latest
pdf2img inputFile.pdf
```