Skip to content

๐Ÿ“ƒ๐Ÿ“ธ Converts PDFs to images in nodejs with no native dependencies

License

Notifications You must be signed in to change notification settings

dpes8693/pdf-to-img

ย 
ย 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

pdf-to-img

Build Status Coverage Status npm version npm npm bundle size

๐Ÿ“ƒ๐Ÿ“ธ Converts PDFs to images in nodejs with no native dependencies.

Useful for unit tests of PDFs

Supports nodejs v16 to v20.

Install

npm install -S pdf-to-img

Example

NodeJS:

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:

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:

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
});

About

๐Ÿ“ƒ๐Ÿ“ธ Converts PDFs to images in nodejs with no native dependencies

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 98.6%
  • JavaScript 1.4%