LangChain Tutorial

How to Handle OCR for PDFs in LangChain

Learn the difference between searchable PDFs and scanned PDFs, and discover the best OCR techniques to extract text accurately before sending it to your LLM.


Introduction

One of the most common problems developers face while building AI-powered document readers is that some PDFs return empty text even though they clearly contain readable content.

The reason is simple:

Not all PDFs contain actual text. Many PDFs are simply scanned images stored inside a PDF container. Standard PDF parsers only read embedded text layers—they cannot recognize text inside images.

To process scanned documents, invoices, contracts, handwritten forms, or paper documents, you need an OCR (Optical Character Recognition) engine.

Why Standard PDF Parsers Fail

Libraries such as PyPDFLoader (default configuration) or pypdf extract only the digital text layer from a PDF.

PDF Type Standard Parser OCR Required?
Digital/Searchable PDF ✅ Yes ❌ No
Scanned PDF ❌ No ✅ Yes
Paper Documents Converted to PDF ❌ No ✅ Yes
Image-based Reports ❌ No ✅ Yes

Option 1 — PyPDFLoader + RapidOCR (Recommended)

If your PDF contains embedded images along with searchable text, RapidOCR is a lightweight and fast solution.

Install Required Packages

pip install rapidocr-onnxruntime pypdf

Enable OCR inside PyPDFLoader

from langchain_community.document_loaders import PyPDFLoader
from langchain_community.document_loaders.parsers.pdf import RapidOCRBlobParser

loader = PyPDFLoader(
    file_path=doc_path,
    extract_images=True,
    images_parser=RapidOCRBlobParser()
)

docs = loader.load()
Benefits
  • Very easy to configure
  • Fast OCR processing
  • Works directly with LangChain
  • No manual page conversion required

Option 2 — pdf2image + PyTesseract

For completely scanned documents, the most accurate approach is converting every PDF page into an image and then running Tesseract OCR on each page.

Install Packages

pip install pytesseract pdf2image pillow
System Requirements
  • Ubuntu: sudo apt install tesseract-ocr poppler-utils
  • MacOS: brew install tesseract poppler
  • Windows: Install Tesseract OCR and add it to PATH

Python Example

from pdf2image import convert_from_path
import pytesseract

images = convert_from_path(pdf_path)

text_pages = []

for i, image in enumerate(images):
    text = pytesseract.image_to_string(image)
    text_pages.append(text)

full_text = "\n".join(text_pages)
Advantages
  • Excellent OCR accuracy
  • Ideal for scanned books
  • Works with old paper documents
  • Best for invoices and handwritten scans

Sending OCR Text to GPT using LangChain

After OCR extraction, simply pass the extracted text into your LLM.

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

prompt = ChatPromptTemplate.from_template(
    "Summarize the following document:\n\n{text}"
)

chain = prompt | llm | StrOutputParser()

summary = chain.invoke({
    "text": full_text[:4000]
})

The extracted OCR text is now summarized by GPT into concise executive insights.

Comparison of OCR Techniques

Method Best For Performance Extra Setup
Standard PyPDFLoader Digital PDFs ⭐⭐⭐⭐⭐ None
RapidOCR Mixed PDFs with images ⭐⭐⭐⭐ Minimal
pdf2image + Tesseract 100% Scanned Documents ⭐⭐⭐⭐⭐ High

Recommended OCR Workflow

1️⃣ Load PDF

Read the document.

2️⃣ OCR

Extract text from scanned pages.

3️⃣ LangChain

Create document objects.

4️⃣ GPT

Generate summaries, Q&A and insights.

Best Practices

  • ✔ Use standard PyPDFLoader for searchable PDFs.
  • ✔ Enable RapidOCR when PDFs contain embedded images.
  • ✔ Use Tesseract for completely scanned documents.
  • ✔ Clean extracted OCR text before sending it to an LLM.
  • ✔ Limit token size when passing large documents to GPT.

Final Thoughts

Choosing the correct OCR strategy depends entirely on the type of PDF you're processing. Searchable PDFs work perfectly with standard parsers, while scanned documents require OCR before any AI model can understand the content.

If you're building document AI applications with LangChain, combining RapidOCR for mixed PDFs and Tesseract OCR for scanned documents gives you a powerful, production-ready document processing pipeline capable of handling almost every PDF format.

Pro Tip: Build your application to automatically detect whether a PDF contains searchable text. If not, switch to an OCR pipeline. This hybrid approach provides the best performance and accuracy while minimizing unnecessary OCR processing.