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.
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:
To process scanned documents, invoices, contracts, handwritten forms, or paper documents, you need an OCR (Optical Character Recognition) engine.
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 |
If your PDF contains embedded images along with searchable text, RapidOCR is a lightweight and fast solution.
pip install rapidocr-onnxruntime pypdf
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()
For completely scanned documents, the most accurate approach is converting every PDF page into an image and then running Tesseract OCR on each page.
pip install pytesseract pdf2image pillow
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)
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.
| Method | Best For | Performance | Extra Setup |
|---|---|---|---|
| Standard PyPDFLoader | Digital PDFs | ⭐⭐⭐⭐⭐ | None |
| RapidOCR | Mixed PDFs with images | ⭐⭐⭐⭐ | Minimal |
| pdf2image + Tesseract | 100% Scanned Documents | ⭐⭐⭐⭐⭐ | High |
Read the document.
Extract text from scanned pages.
Create document objects.
Generate summaries, Q&A and insights.
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.