pdf-raster

Quickstart

Convert PDFs into page images from a file path or an in-memory buffer.

convert() is the only public entrypoint you need to learn first.

Convert all pages from a file path

import { convert } from "@omsimos/pdf-raster";

const pages = await convert("./report.pdf");

for (const page of pages) {
  console.log({
    pageIndex: page.pageIndex,
    outputFormat: page.mimeType,
    width: page.width,
    height: page.height,
    dpi: page.dpi,
  });
}

Default behavior

If pages is omitted, the full document is rendered. If outputFormat is omitted, the output defaults to PNG.

What one page looks like

Each item in pages is a complete rendered image result:

const [page] = await convert("./report.pdf");

console.log({
  pageIndex: page.pageIndex,
  mimeType: page.mimeType,
  width: page.width,
  height: page.height,
  dpi: page.dpi,
  bytes: page.data.byteLength,
});

// {
//   pageIndex: 0,
//   mimeType: "image/png",
//   width: 833,
//   height: 417,
//   dpi: 300,
//   bytes: 14032
// }

The actual image bytes are in page.data, so you can write them to disk, return them from an API, or pass them into another service directly.

Convert selected pages

Page indices are zero-based:

import { convert } from "@omsimos/pdf-raster";

const pages = await convert("./report.pdf", {
  pages: [0, 2],
  dpi: 300,
});

Choose an output format

PNG is the default, but you can request another encoded format when needed:

import { convert } from "@omsimos/pdf-raster";

const pages = await convert("./report.pdf", {
  outputFormat: "webp",
});

console.log(pages[0].mimeType);
// "image/webp"

Convert from an in-memory buffer

import { convert } from "@omsimos/pdf-raster";

const pages = await convert("./invoice.pdf", {
  dpi: 300,
});

Understand the result

Every page includes:

  • pageIndex
  • data as an encoded image Buffer
  • mimeType
  • width
  • height
  • dpi

These values are enough to:

  • save the image
  • display it in a UI
  • upload it to another API
  • feed it into downstream processing

Common first mistakes

On this page