Node.js and Bun
Convert PDFs from disk or from memory in scripts, workers and servers.
These examples run the same way in Node.js 24+ and Bun.
Save every page to disk
import { mkdir, writeFile } from "node:fs/promises";
import { convert } from "pdf-raster";
await mkdir("./out", { recursive: true });
const pages = await convert("./report.pdf", { outputFormat: "webp" });
for (const page of pages) {
await writeFile(`./out/page-${page.pageIndex + 1}.webp`, page.data);
}Make a thumbnail of the first page
A low DPI renders faster and gives a smaller file. At 72 DPI a US letter page is 612 × 792 pixels.
const [cover] = await convert("./report.pdf", {
pages: [0],
dpi: 72,
outputFormat: "jpeg",
});Convert bytes from memory
Pass a Buffer, Uint8Array or ArrayBuffer when the PDF comes from an
upload, a queue message or a storage SDK.
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
import { convert } from "pdf-raster";
const s3 = new S3Client({});
const object = await s3.send(
new GetObjectCommand({ Bucket: "documents", Key: "invoice.pdf" }),
);
const bytes = await object.Body!.transformToByteArray();
const pages = await convert(bytes, { dpi: 300 });Process a long PDF in batches
convert() holds every result in memory until it resolves. For documents
with hundreds of pages, request a batch at a time and write each batch out
before the next one.
import { writeFile } from "node:fs/promises";
import { convert } from "pdf-raster";
const pageCount = 400; // from your own metadata
const batchSize = 20;
for (let start = 0; start < pageCount; start += batchSize) {
const indices = Array.from(
{ length: Math.min(batchSize, pageCount - start) },
(_, i) => start + i,
);
const pages = await convert("./archive.pdf", { pages: indices });
for (const page of pages) {
await writeFile(`./out/${page.pageIndex}.png`, page.data);
}
}pdf-raster has no page-count API, so get the count from your own records or from another PDF library.
Convert many files at once
Concurrent calls are safe. Because PDFium renders one page at a time across the process, running calls in parallel adds little speed. It helps most with JPEG, where encoding overlaps with the next call's rendering. See the throughput table.
const files = ["a.pdf", "b.pdf", "c.pdf", "d.pdf"];
const results = await Promise.all(
files.map((file) => convert(file, { outputFormat: "jpeg" })),
);