DS DevShelfHub Projects · AI tools
Tutorials / LangChain / Reference / Classes / PyPDFLoader
Document Loader langchain-community Beginner

PyPDFLoader: Reference Guide

By DevShelfHub

Extract text from PDF files.

What is PyPDFLoader?

PyPDFLoader reads PDF files and extracts text content page-by-page, producing one Document object per page. Each Document carries a page_content string and a metadata dict with source (the file path), page (zero-based page number), and total_pages. The class wraps the pypdf library and requires no additional system dependencies beyond the Python package.

The loader supports synchronous and asynchronous loading via .load() and .aload(). For large PDFs, .lazy_load() returns a generator instead of loading all pages into memory at once—valuable when processing files with hundreds of pages in a pipeline that streams to a vector store.

PyPDFLoader is intentionally minimal: no OCR, no layout analysis, no table extraction. If your PDFs contain scanned images, two-column layouts, or form fields, the extracted text will be garbled or empty. For those cases, DoclingLoader, UnstructuredPDFLoader, or AWS Textract give substantially better results. PyPDFLoader is the first choice for programmatically generated PDFs—invoices, reports, contracts—where the text layer is reliable and extraction is fast.

When to Use

You need to process standard PDFs. Use PyPDFLoader for simple, text-based PDFs.

Use Cases

  • Extract PDF content
  • Index PDFs for RAG
  • Document processing
  • Report analysis
  • Contract extraction
  • Document management

Key Features

  • Text extraction
  • Page metadata
  • Fast processing
  • No external dependencies
  • Simple API
  • Batch loading

When NOT to Use

For scanned/image PDFs—use AWS Textract or Docling.

Notes

No OCR: scanned PDFs return empty or garbled text

PyPDFLoader relies on the PDF's embedded text layer. Scanned documents contain only image pixels—no text layer. If loader.load()[0].page_content is an empty string or nonsense, the file is scanned. Switch to UnstructuredPDFLoader with strategy="hi_res" or DoclingLoader for OCR support.

Zero-based page numbers in metadata

metadata["page"] starts at 0, not 1. When surfacing page numbers to users, add 1: page_label = doc.metadata["page"] + 1. This also matters when filtering Documents for a specific page range before chunking and embedding.

Use lazy_load() for large files

loader.load() reads the entire PDF into memory. A 500-page report can allocate hundreds of megabytes. Use loader.lazy_load() in production pipelines to stream one page at a time and avoid OOM errors.

Password-protected PDFs

Pass password="..." to PyPDFLoader("file.pdf", password="secret") to handle encrypted files. Without it, PyPDFLoader raises PdfReadError on protected documents. Add try/except PdfReadError to handle gracefully in batch pipelines.

Import

python
from langchain_community.document_loaders import PyPDFLoader

Key Parameters

Parameter Type Default Purpose
file_path str None Path to PDF file

Usage Examples

Load PDF

python
loader = PyPDFLoader('document.pdf')
pages = loader.load()
for page in pages:
    print(f'Page {page.metadata["page"] + 1}: {page.page_content[:100]}')

Lazy Load Large PDF

python
loader = PyPDFLoader('large_report.pdf')
for page in loader.lazy_load():
    vector_store.add_documents([page])

Filter Pages by Range

python
loader = PyPDFLoader('report.pdf')
pages = loader.load()
selected = [p for p in pages if 4 <= p.metadata["page"] <= 9]
splitter = RecursiveCharacterTextSplitter(chunk_size=1000)
chunks = splitter.split_documents(selected)

Common Mistakes

❌ Try to OCR scanned PDFs with PyPDFLoader

✅ Use AWS Textract for scanned documents

Browse the full LangChain API reference index to explore more classes, methods, and decorators, or start with the LangChain introduction tutorial for end-to-end context on building with PyPDFLoader and the wider framework.

PyPDFLoader FAQ

What is PyPDFLoader in LangChain?

Extract text from PDF files. PyPDFLoader reads PDF files and extracts text content page-by-page, producing one Document object per page. Each Document carries a page_content string and a metadata dict with source (the file path), page (zero-based page number), and total_pages. The class wraps the pypdf library and requires no additional system dependencies beyond the Python package. The loader supports synchronous and asynchronous loading via .load() and .aload(). For large PDFs, .lazy_load() returns a …

Which package provides PyPDFLoader?

DevShelfHub documents PyPDFLoader from the langchain-community package. Pin your installed LangChain version and match imports to the snippet on this page.

When should I use PyPDFLoader?

You need to process standard PDFs. Use PyPDFLoader for simple, text-based PDFs.

When should I avoid using PyPDFLoader?

For scanned/image PDFs—use AWS Textract or Docling.

How do I import PyPDFLoader in Python?

from langchain_community.document_loaders import PyPDFLoader

Where can I explore more LangChain API reference pages?

Open the LangChain API reference index on DevShelfHub to browse classes, methods, and decorators, each with runnable examples, parameters, common mistakes, and cross-links.