惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
aimingoo的专栏
aimingoo的专栏
S
SegmentFault 最新的问题
博客园_首页
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
博客园 - 【当耐特】
月光博客
月光博客
C
Check Point Blog
T
The Blog of Author Tim Ferriss
罗磊的独立博客
博客园 - Franky
MongoDB | Blog
MongoDB | Blog
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
B
Blog
阮一峰的网络日志
阮一峰的网络日志
腾讯CDC
美团技术团队
N
Netflix TechBlog - Medium
Stack Overflow Blog
Stack Overflow Blog
Y
Y Combinator Blog
L
LangChain Blog
The Cloudflare Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
PDF to Structured JSON Without ML Training: A 2026 Develo...
DevToolsmith · 2026-04-29 · via DEV Community

DevToolsmith

PDF to Structured JSON Without ML Training

Every team that ships a PDF processing feature reaches the same wall: OCR returns a string of words, but the user wants { "invoice_number": "INV-1234", "total": 4582.00, "line_items": [...] }. This is the gap LLMs solved in 2024-2025, and the patterns that actually work in production in 2026.

The four eras of PDF extraction

  1. Text PDFs (1995-2010)pdftotext gives you the text. Parsing structure is custom regex.
  2. Scanned PDFs (2010-2018) — Tesseract OCR. Layout is gone. You parse word streams.
  3. Layout-aware OCR (2018-2023) — Google Document AI, AWS Textract, Azure Form Recognizer. Tables sort of work. Custom models cost $$$.
  4. LLM extraction (2023-2026) — GPT-4V, Claude, Gemini handle complex layouts directly. Schema in, JSON out.

If you're starting fresh in 2026, you start at era 4. Era 3 is still useful for high-volume regulated workflows.

The "send the page to an LLM" pattern

The pattern is straightforward:

// Pseudocode — works with OpenAI, Anthropic, Google, Mistral
const pageImage = await pdfPageToImage(pdfBuffer, pageIndex);
const response = await llm.chat.completions.create({
  model: "claude-3-5-sonnet-20241022",
  messages: [
    { role: "user", content: [
      { type: "image", source: { type: "base64", data: pageImage }},
      { type: "text", text: extractionPrompt }
    ]}
  ],
  response_format: { type: "json_schema", schema: invoiceSchema }
});

Enter fullscreen mode Exit fullscreen mode

Where invoiceSchema is your strongly-typed JSON Schema:

const invoiceSchema = {
  type: "object",
  required: ["invoice_number", "issue_date", "total_amount", "line_items"],
  properties: {
    invoice_number: { type: "string" },
    issue_date: { type: "string", format: "date" },
    vendor: { type: "object", properties: { name: { type: "string" }, vat_id: { type: "string" }}},
    customer: { type: "object", properties: { name: { type: "string" }}},
    total_amount: { type: "number" },
    currency: { type: "string", enum: ["EUR","USD","GBP","CHF"]},
    line_items: {
      type: "array",
      items: {
        type: "object",
        properties: {
          description: { type: "string" },
          quantity: { type: "number" },
          unit_price: { type: "number" },
          total: { type: "number" }
        }
      }
    }
  }
};

Enter fullscreen mode Exit fullscreen mode

Three lessons from running this on 50,000+ documents:

1. Page-by-page beats whole-document

LLMs degrade on long contexts. Split PDFs into pages, extract per page, then merge by document. You can parallelize.

2. Image vs text mode

For text PDFs (text layer present): you can pass the extracted text. Cheaper, but loses layout.
For scanned PDFs: image mode is mandatory. ~3-5x cost.
For mixed PDFs (forms with stamps, signatures): always image mode.

3. JSON schema enforcement is everything

Without schema enforcement, you get hallucinated fields, missing fields, wrong types. With it, you get deterministic outputs you can validate. OpenAI's response_format, Anthropic's tool use forcing, and Mistral's structured output all do this.

What goes wrong (and how to handle it)

Hallucinations

LLMs invent invoice numbers when they can't read them. Always include in the prompt: "If a field cannot be confidently determined from the document, set it to null. Do not guess." Then validate downstream — if invoice_number is null, flag for human review.

Confidence scores

Add a _confidence field per extracted entity:

{
  "invoice_number": "INV-1234",
  "_confidence": { "invoice_number": 0.97, "total_amount": 0.99 }
}

Enter fullscreen mode Exit fullscreen mode

Use these to route low-confidence docs to human review. Don't auto-process below 0.85.

Multi-page documents

Concatenate JSON per page, then a final LLM call to merge:

const merged = await llm.chat({ 
  messages: [{ role: "user", content: 
    `Merge these per-page extractions into a single invoice JSON. Reconcile line items across pages. Pages: ${JSON.stringify(perPageResults)}` 
  }]
});

Enter fullscreen mode Exit fullscreen mode

Cost optimization

Image-mode extraction is ~$0.005-0.02 per page. For 100K pages/month that's $500-2000. Optimizations:

  • Pre-filter empty pages (blank, separator, cover) — saves 15-25%
  • Use cheaper models for triage (Haiku, Flash, Mistral Small) — route only complex pages to Sonnet/Opus
  • Cache by document hash — same PDF processed twice = no extra cost
  • Batch with prompt caching (Anthropic, OpenAI) — 50% off on repeated system prompts

Compliance

For regulated workflows (invoices, contracts, healthcare):

  • Don't use models that train on inputs (default for OpenAI/Anthropic enterprise tiers, opt-out on others)
  • EU data residency: Mistral, Anthropic Frankfurt region, Azure OpenAI EU
  • Audit trail: log model + version + prompt hash + input hash + output

When NOT to use LLMs

  • Standardized forms with strict layout (W2, 1099) — Document AI templates are cheaper and more reliable
  • Tables only (price lists, schedules) — Camelot/Tabula are deterministic
  • High-volume, low-margin (>1M pages/month, <$0.001/page budget) — train a custom model

What we built

We needed this for our portfolio's contract review workflow. We wrapped the pattern above into an API: send a PDF, get JSON back, with schema enforcement, confidence scoring, and EU data residency. Free tier 100 pages/month no card at parseflow.dev. Same pattern, packaged.

If you build it yourself, the patterns above are the difference between a 60% accurate prototype and a 95%+ accurate production system. The cost is mostly API tokens — engineering time is one weekend.


Antonio Altomonte builds developer APIs at DevToolsmith. PDF to JSON API: parseflow.dev.