pdf-raster

OCR and vision models

Pass rendered pages to Tesseract.js or to a multimodal model with the AI SDK.

OCR engines and vision models read images, not PDFs. Render the pages with pdf-raster first, then pass page.data to the next tool.

Resolution and format

Render at 300 DPI, the default, so small text stays legible. PNG and WebP are lossless. JPEG adds compression artifacts around text, so avoid it for OCR unless the tool requires it.

Tesseract.js

import { convert } from "pdf-raster";
import { createWorker } from "tesseract.js";

const worker = await createWorker("eng");
const pages = await convert("./scan.pdf");

for (const page of pages) {
  const { data } = await worker.recognize(page.data);
  console.log(`Page ${page.pageIndex + 1}:`, data.text);
}

await worker.terminate();

AI SDK

import { convert } from "pdf-raster";
import { generateText } from "ai";
import { openai } from "@ai-sdk/openai";

const [page] = await convert("./invoice.pdf", {
  pages: [0],
  outputFormat: "webp",
});

const { text } = await generateText({
  model: openai("gpt-6-astra"),
  messages: [
    {
      role: "user",
      content: [
        { type: "text", text: "List the line items on this invoice." },
        { type: "file", data: page.data, mediaType: page.mimeType },
      ],
    },
  ],
});

Other image-capable SDKs take the same two values: the bytes in page.data and the type in page.mimeType. Some APIs want base64 instead of bytes, which page.data.toString("base64") gives you.

Sending many pages to a model

Most vision APIs limit image size and count per request, and they bill by image size. Check your provider's limits, then lower dpi or crop to the region you need:

const [header] = await convert("./invoice.pdf", {
  pages: [0],
  dpi: 200,
  crop: { x: 0, y: 0, width: 1700, height: 500 }, // the top of the page
});

pdf-raster only renders. Retries, prompts and parsing the model's answer belong in your application code.

On this page