The Agony of Traditional OCR Pipelines
For twenty years, extracting structured data from PDF documents was an engineering nightmare:
- Scanned invoices fed into Tesseract or AWS Textract emerged with scrambled reading orders.
- Multi-column tables had their cell contents interleaved into nonsensical paragraphs.
- Subtle visual cues—such as a red strikethrough over a cancelled price, or a signature checkbox—were completely lost during text extraction.
By treating PDF pages as high-resolution images and prompting multimodal vision models directly, developers can bypass OCR completely.
1. Visual Token Economics & Tiling Strategies
When sending PDF page renders (PNG/JPEG) to a model like Claude 3.5 Sonnet or GPT-4o, image resolution directly impacts extraction quality:
[Standard A4 Page: 300 DPI -> 2480 x 3508 pixels]
↓ Downsampled & Tiled
[6 to 8 Vision Patches of 512x512 = ~1,600 Vision Tokens per Page]
At ~1,600 tokens per page, processing a 5-page legal contract costs less than $0.02. In return, the model perceives spatial layout, typography hierarchies, borders, logos, and signatures exactly as a human auditor would.
2. Spatial Anchor Prompting for Tabular Extraction
When prompting for complex nested tables, instruct the model to use Visual Spatial Anchoring:
<document_instruction>
You are an expert forensic document auditor inspecting the attached 3-page commercial freight manifest.
Extraction Rules:
1. Ground every extracted line item by its relative vertical position on the page.
2. For each table row, extract:
- Line Item Number
- Commodity Description (verbatim)
- Harmonized Tariff Schedule (HTS) Code
- Weight (Gross and Net in kg)
- Declared Customs Value (USD)
3. Visual Discrepancy Rules:
- If a cell has a handwritten pencil correction over typed text, transcribe the handwritten value and flag "corrected_manually": true.
- If a row is stamped with "VOID", omit it from active items and append it to "voided_items".
</document_instruction>
3. Implementing Multi-Page Processing with TypeScript
// doc-ai/extract-manifest.ts
import { generateObject } from 'ai';
import { openai } from '@ai-sdk/openai';
import { z } from 'zod';
const ManifestSchema = z.object({
carrierName: z.string(),
bolNumber: z.string().describe("Bill of Lading number"),
totalWeightKg: z.number(),
items: z.array(z.object({
htsCode: z.string(),
description: z.string(),
customsValueUsd: z.number(),
hasHandwrittenCorrections: z.boolean(),
})),
signatureDetected: z.boolean(),
});
export async function parsePdfPageImages(pageImageBase64Array: string[]) {
const imageContents = pageImageBase64Array.map((b64) => ({
type: 'image' as const,
image: b64,
}));
const result = await generateObject({
model: openai('gpt-4o'),
schema: ManifestSchema,
messages: [
{
role: 'system',
content: 'Extract shipping manifest records. Verify column alignment carefully.',
},
{
role: 'user',
content: [
{ type: 'text', text: 'Extract structured data from these scanned manifest pages:' },
...imageContents,
],
},
],
});
return result.object;
}
4. Key Takeaways
- Ditch Classical OCR for Complex Layouts: Multimodal models understand visual spacing, checkboxes, and logos natively.
- Render at 150–200 DPI: 200 DPI provides the ideal balance between crisp character clarity and token cost.
- Instruct on Visual Overlays: Explicitly tell the model how to interpret handwritten corrections, ink stamps, and crossed-out text.