LangChain 

Extracting Structured Tables from Scanned PDF Images using LangChain

Learn the best techniques to accurately extract tables from scanned invoices, receipts, financial reports, and image-based PDFs using GPT-4o Vision, IBM Docling, and Unstructured PDF Parser.


Introduction

Extracting tables from scanned PDF documents is significantly more challenging than extracting plain text from searchable PDFs.

Libraries such as pdfplumber, PyPDF, and Camelot depend on embedded text layers. Unfortunately, scanned PDFs contain only images, making these tools ineffective.

The Problem
OCR engines such as Tesseract or RapidOCR can recognize words, but they often lose the visual relationship between rows and columns, resulting in scrambled table data.

Modern AI-powered document processing solves this problem by combining OCR, layout detection, computer vision, and Large Language Models (LLMs).

Why Traditional OCR Fails on Tables

Tool Reads Images? Preserves Table Layout? Recommended?
PyPDF No
pdfplumber No
Camelot Limited Only Digital PDFs
Tesseract OCR Average
GPT-4o Vision Excellent

Strategy 1 — GPT-4o Vision + Pydantic (Recommended)

This is currently the most accurate approach for extracting structured tables from scanned documents.

Instead of running OCR first, each PDF page is converted into an image and sent directly to GPT-4o Vision.

Advantages
  • Excellent table recognition
  • Maintains row-column relationships
  • No OCR preprocessing required
  • Returns structured JSON using Pydantic

Workflow

1️⃣ PDF

Convert pages into images.

2️⃣ GPT-4o Vision

Analyze page visually.

3️⃣ Pydantic

Validate extracted schema.

4️⃣ DataFrame

Use structured data.

Python Example

import base64
from io import BytesIO
from typing import List

from pydantic import BaseModel, Field
from pdf2image import convert_from_path

from langchain_openai import ChatOpenAI
from langchain_core.messages import HumanMessage

class TableRow(BaseModel):
    item_description: str
    quantity: int
    unit_price: float
    total_amount: float

class ExtractedTable(BaseModel):
    table_name: str
    rows: List[TableRow]

images = convert_from_path("scanned_invoice.pdf")

buffer = BytesIO()
images[0].save(buffer, format="JPEG")

img_base64 = base64.b64encode(buffer.getvalue()).decode("utf-8")

llm = ChatOpenAI(model="gpt-4o", temperature=0)

structured_llm = llm.with_structured_output(ExtractedTable)

message = HumanMessage(
    content=[
        {
            "type": "text",
            "text": "Extract all table data accurately."
        },
        {
            "type": "image_url",
            "image_url": {
                "url": f"data:image/jpeg;base64,{img_base64}"
            }
        }
    ]
)

result = structured_llm.invoke([message])

Strategy 2 — IBM Docling

IBM Research developed Docling to parse complex business documents while preserving layouts, reading order, tables, and document structure.

Best For
Companies that require an open-source local solution without relying on external APIs.

Installation

pip install docling langchain-docling

Python Example

from langchain_docling import DoclingLoader
from docling.document_converter import DocumentConverter

loader = DoclingLoader(
    file_path="scanned_document.pdf",
    converter=DocumentConverter()
)

docs = loader.load()

for doc in docs:
    print(doc.page_content)
    print("=" * 50)

Strategy 3 — Unstructured PDF Parser (HI_RES)

The Unstructured library combines computer vision, OCR, and layout detection models such as YOLOX and Detectron2 to reconstruct scanned tables into HTML or Markdown.

Installation

pip install "unstructured[pdf]" poppler-utils tesseract-ocr

Python Example

from langchain_community.document_loaders import UnstructuredPDFLoader

loader = UnstructuredPDFLoader(
    "scanned_table.pdf",
    strategy="hi_res",
    infer_table_structure=True,
    ocr_languages="eng"
)

docs = loader.load()

for doc in docs:
    if "Table" in str(doc.metadata):
        print(doc.page_content)

Strategy Comparison

Tool / Strategy Cost Accuracy Setup Difficulty Best Use Case
GPT-4o Vision + Pydantic API Usage 95%+ Easy Highest accuracy
IBM Docling Free 85–90% Medium Local processing
Unstructured (HI_RES) Free 80–90% Medium Complex layouts

Which Strategy Should You Choose?

GPT-4o Vision

Choose this when maximum accuracy is your priority and API usage is acceptable.

IBM Docling

Ideal for enterprises requiring local processing without sending documents to cloud services.

Unstructured

Great for large batches of scanned documents with complex layouts and multiple tables.

Final Recommendation

If you're building a production-grade AI document processing application, GPT-4o Vision with Structured Output offers the highest accuracy for extracting structured tables from scanned PDFs while requiring minimal preprocessing.

For organizations needing an entirely local and open-source solution, IBM Docling is an excellent choice. If your workflow involves large-scale scanned documents with complex layouts, Unstructured PDF Parser provides powerful layout-aware OCR capabilities.

Pro Tip: Use GPT-4o Vision whenever possible for invoices, purchase orders, receipts, and financial statements. Its multimodal understanding preserves table structure far better than traditional OCR engines.