PDF Processing Pipeline Documentationο
Overviewο
A document processing pipeline is a structured sequence of steps that transforms raw, unstructured documents into formats suitable for training or inference with Large Language Models (LLMs). Its importance lies in ensuring data quality, relevance, and usability, which directly impact LLM performance.
Docling: NextβGeneration Document Processingο
Overviewο
Docling is a powerful document parsing tool designed to extract structured, layout-aware content from various file types such as PDF, DOCX, PPTX, HTML, and more. It transforms these documents into a rich unified representation that retains important elements like layout, headings, tables, and sections. This makes the documents ideal for generative AI workflows, including Retrieval-Augmented Generation (RAG).
Docling is integrated with LangChain via the DoclingLoader, which allows seamless integration of complex documents into LLM pipelines.
Architectureο
The architecture consists of the following components:
In a nutshell, Doclingβs architecture is outlined in the diagram above.
For each document format, the document converter knows which format-specific backend to employ for parsing the document and which pipeline to use for orchestrating the execution, along with any relevant options.
π§ Key Featuresο
β Supports multiple document formats: PDF, DOCX, PPTX, HTML, and more.
β Preserves layout, tables, headings, and sections.
β Converts documents into formats optimized for LLMs.
β Works natively with LangChain.
β Supports two export modes for flexibility:
ExportType.DOC_CHUNKS: Splits documents into smaller chunks, each treated as a separate LangChain Document.
ExportType.MARKDOWN: Converts the entire document into a single markdown-formatted LangChain Document.
π Installationο
To install Docling, use the following command:
`bash
pip install -qU langchain-docling
`
This will install the latest version of Docling along with the necessary integrations for LangChain.
π¦ Usageο
To use Docling, you can import the DoclingLoader and specify the document path and export type. Hereβs a simple example:
1. Import Required Modules
from langchain_docling import DoclingLoader
from langchain_docling.schema import ExportType
ExportType.DOC_CHUNKS β Document is split into chunks
ExportType.MARKDOWN β Whole document as a single markdown output
export_type = ExportType.DOC_CHUNKS # or ExportType.MARKDOWN
3. Load Document
Specify the document path and export type:
doc_path = "path/to/your/document.pdf" # Specify your document path
loader = DoclingLoader(doc_path, export_type=export_type)
# Load the document
documents = loader.load()
This will load the document and return a list of LangChain Document objects, each containing the extracted content.
4. Use with LangChain
Once loaded, the documents can be passed into any LangChain component like a retriever, summarizer, or question-answering chain.
π Supported File Typesο
.docx
.pptx
.html
.txt
Other common document formats
π PDF Data Extraction doclingο
π§ Goalο
We will:
Extract all content from a PDF
Save the content in a DocLing-compatible format (e.g., HTML or Markdown with clear structure)
Optionally generate metadata for easier annotation
β π Code: PDF Extraction for DocLing Annotationο
# Required Libraries
import os
import pdfplumber
import fitz # PyMuPDF
from pathlib import Path
# Create Output Folder
output_dir = Path("docling_output")
output_dir.mkdir(exist_ok=True)
def extract_pdf_content(pdf_path):
"""
Extracts text, images, and tables from a PDF file.
Outputs a structured HTML-like file ready for DocLing annotation.
"""
output_file = output_dir / f"{Path(pdf_path).stem}_docling_ready.html"
..................................................
..................................................
# The function code is in `structuration_pdf.ipynb`. Open the notebook to view and experiment.
..................................................
........................................................
print(f"β
Extracted content saved to: {output_file}")
# Example usage
extract_pdf_content("example.pdf")
code-link:
GitHub link - for DocLing Annotation code
π Outputο
β docling_output/example_docling_ready.html: contains all structured content
β Images saved as page1_img1.png, page2_img1.jpg etc.
β Human-readable format for manual annotation in DocLing
π§ How to Use with DocLingο
1. Open the HTML file in a browser or upload it to a DocLing-compatible editor
2. Use DocLing to:Add metadata (e.g., title, author)
Annotate sections (e.g., introduction, methods, conclusion)
Tag named entities (dates, places, etc.)
β Summaryο
| Feature | Included |
|---|---|
| Text Extraction | β |
| Table Extraction | β |
| Image Extraction | β |
| Prepares for DocLing | β |