← Blog

Claude API Data Extraction from PDF: A Practical Guide

2026-11-18 · 5 min read · SubToAPI Team

Extracting structured data from PDFs with the Claude API means sending the document (or its text/images) to Claude along with instructions on what fields to pull out, then parsing Claude's response — usually JSON — into your application. Claude handles messy, unstructured PDF layouts like invoices, contracts, and reports far better than regex or traditional OCR pipelines because it understands context, not just text position.

This matters because PDFs are notoriously inconsistent. An invoice from one vendor puts the total at the bottom right; another puts it in a table header. Traditional extraction tools need custom templates per document type. Claude reads the content the way a human would, which means one well-designed prompt can handle dozens of PDF variants without per-template engineering.

How PDF Extraction Works with Claude

There are two practical approaches, depending on how your PDF is structured and what Claude API access you're using.

1. Native PDF support

Claude's models support PDF input directly as a document content block. You base64-encode the PDF and send it in the request, and Claude processes both the text and visual layout (tables, charts, scanned pages) in context.

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "content-type: application/json" \
  -d '{
    "model": "claude-sonnet-4",
    "max_tokens": 1024,
    "messages": [
      {
        "role": "user",
        "content": [
          {
            "type": "document",
            "source": {
              "type": "base64",
              "media_type": "application/pdf",
              "data": "<BASE64_PDF_DATA>"
            }
          },
          {
            "type": "text",
            "text": "Extract invoice number, vendor name, total amount, and due date. Return valid JSON only."
          }
        ]
      }
    ]
  }'

This is the simplest path and works well for PDFs under typical size limits. If you're already using SubToAPI's wrapper around your Claude access, this exact request format works against /v1/messages — see the quickstart and messages docs for request limits and model options.

2. Pre-extracted text or OCR pipeline

For scanned PDFs with poor text layers, or very large documents, extract text first (with a library like pdf-parse, pdfplumber, or an OCR step), then send the plain text to Claude. This reduces payload size and lets you chunk long documents across multiple requests.

import fs from "fs";
import pdf from "pdf-parse";

const buffer = fs.readFileSync("invoice.pdf");
const { text } = await pdf(buffer);

const response = await fetch("https://api.subtoapi.app/v1/messages", {
  method: "POST",
  headers: {
    "Authorization": `Bearer ${process.env.SUBTOAPI_KEY}`,
    "content-type": "application/json"
  },
  body: JSON.stringify({
    model: "claude-sonnet-4",
    max_tokens: 1024,
    messages: [
      {
        role: "user",
        content: `Extract the following fields as JSON: invoice_number, vendor, total, due_date.\n\nDocument text:\n${text}`
      }
    ]
  })
});

const data = await response.json();
console.log(data.content[0].text);

Designing Prompts for Reliable Extraction

The quality of your extracted data depends almost entirely on prompt specificity. A few rules that consistently improve accuracy:

Example schema instruction:

Return a JSON object with exactly these keys:
{
  "invoice_number": string,
  "vendor": string,
  "total_amount": number,
  "due_date": "YYYY-MM-DD" or null
}
No markdown formatting, no commentary.

Using Tool Calls for Structured Output

If you need guaranteed schema compliance rather than hoping Claude follows formatting instructions, use tool/function calling. Define a tool with a strict JSON schema for the extracted fields, and Claude will return a tool call matching that schema instead of free-form text. This is the most reliable method for production pipelines processing many PDF types. See the tool use docs for the request structure, and streaming docs if you're extracting from very long documents and want partial results as they're generated.

Batching and Scaling Extraction

For high-volume use cases — processing hundreds of invoices, contracts, or forms daily — a few practical considerations:

If you're evaluating providers for this kind of workload, pricing and seat structure matter as volume grows — see pricing for Solo, Team, and Scale tiers, and sign up for a free trial if you want to test extraction against your own PDF samples before committing.

Common Pitfalls

questions

Can Claude extract data from scanned (image-based) PDFs? Yes. Claude's document support processes visual content, so scanned pages with tables, stamps, or handwriting are often readable without a separate OCR step, though accuracy depends on scan quality.

What's the best output format for extracted PDF data? JSON, defined explicitly in your prompt or enforced via tool calling. Tool calling gives the strongest schema guarantees for production pipelines.

How do I handle PDFs too large for a single request? Split by page ranges or sections, extract text with a library first, then send manageable chunks to Claude, reassembling results afterward.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →