liveBook · Manning

PyPDFLoader

As explained in MongoDB 8.0 in Action, Third Edition

PyPDFLoader is a LangChain document loader that retrieves and extracts text from PDF files.

PyPDFLoader loads a PDF document for downstream processing in a retrieval-augmented generation pipeline. The loaded pages are split into chunks, embedded, and inserted into MongoDB Atlas for vector search. section 14.5.2

A practical ingestion flow uses PyPDFLoader alongside:

  • RecursiveCharacterTextSplitter, which divides the loaded document into 500-character chunks with no overlap. section 14.5.2
  • OpenAIEmbeddings, which generates vectors for each chunk. section 14.5.2
  • MongoDBAtlasVectorSearch, which inserts the processed chunks and makes them searchable. section 14.5.2

Load a PDF for ingestion

Instantiate PyPDFLoader with a PDF URL, then call loader.load() to obtain document data: loader = PyPDFLoader("https://mng.bz/PwxP"); data = loader.load(). section 14.5.2

import os
from langchain_community.document_loaders import PyPDFLoader
from langchain_openai import OpenAIEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from pymongo import MongoClient

MONGO_URI = os.environ["MONGO_URI"]

# Note that if you change this, you also
➥need to change it in `rag_mongo/chain.py`
DB_NAME = "langchain" #1
COLLECTION_NAME = "mongodb" #2
ATLAS_VECTOR_SEARCH_INDEX_NAME = "default" #3
EMBEDDING_FIELD_NAME = "embedding"
client = MongoClient(MONGO_URI)
db = client[DB_NAME]
MONGODB_COLLECTION = db[COLLECTION_NAME]

if __name__ == "__main__":
    # Load docs
    loader = PyPDFLoader("https://mng.bz/PwxP") #4
    data = loader.load()

    # Split docs
    text_splitter = RecursiveCharacterTextSplitter
➥(chunk_size=500, chunk_overlap=0)
    docs = text_splitter.split_documents(data)

    # Insert the documents in MongoDB Atlas Vector Search
    _ = MongoDBAtlasVectorSearch.from_documents(
        documents=docs,
        embedding=OpenAIEmbeddings(disallowed_special=()),
        collection=MONGODB_COLLECTION,
        index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
    )

View the surrounding ingestion script showing PDF loading, chunking, embedding, and MongoDB connection setup. section 14.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains initialization and load(), then connects the result to chunking, embeddings, and vector insertion.
Examples
Uses a MongoDB documentation PDF URL and a 500-character, zero-overlap chunking configuration.
What it assumes
Assumes Python access to LangChain components, a PDF URL, OpenAI credentials, and a configured MongoDB connection.
What you can do after
Build it from scratch as a PDF-to-vector ingestion step for searchable MongoDB Atlas data.
How it connects
Connects PDF documents with text splitting, vector embeddings, MongoDB Atlas, and vector search.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.