PDFLoader parses a PDF file and returns a list of Document objects that downstream LangChain.js workflows can split, summarize, or retrieve. It is commonly selected by file extension alongside DocxLoader, giving an application one loading path for multiple document types. section 7.1.3
A practical PDF-processing pipeline uses PDFLoader in these stages:
- Select PDFLoader after inspecting the file extension. section 7.1.3
- Instantiate it with the file path and await load() to obtain Documents. section 7.1.3
- Split the returned documents into overlapping chunks before summarization or retrieval. section 7.1.3
Listing 7.1 Loading different document types
import { PDFLoader } from "@langchain/community/document_loaders/fs/pdf"; #1
import { DocxLoader } from "@langchain/community/document_loaders/fs/docx";
import path from "path"; #2
const loaders = {
pdf: PDFLoader,
docx: DocxLoader,
};
const loadDocumentFromFile = async (filePath) => { #3
const fileExtension = path.extname(filePath)
.substring(1).toLowerCase();
const LoaderClass = loaders[fileExtension];
if (!LoaderClass) {
console.error(`No loader found for file extension: ${fileExtension}`);
return null;
}
const loader = new LoaderClass(filePath);
return await loader.load();
};
loadDocumentFromFile("./data/aesops-fables.pdf") #4
.then((documents) => {
console.log(`Loaded ${documents.length} documents.`);
})
.catch((error) => {
console.error("Error loading documents:", error);
});
Use the loader-selection example as a starting point for a JavaScript document-ingestion utility, including unsupported-extension handling and load errors. section 7.1.3
PDFLoader is a reasonable middle ground for LLM-oriented PDF parsing, but extraction is imperfect. Treat loaded content as input for further preprocessing rather than assuming reliable interpretation of every PDF. section 7.1.3
