PDF Processing Pipeline Documentation ==================================== Overview ------- .. raw:: html
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 _________________________________ .. raw:: htmlDocling 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 ____________________ .. raw:: htmlThe architecture consists of the following components:
.. figure:: /Documentation/images/docling_arch.png :width: 100% :alt: Alternative text for the image :name: logo In a nutshell, Docling's architecture is outlined in the diagram above. .. raw:: htmlFor 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: .. raw:: htmlExportType.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 ____________________ .. raw:: htmlTo install Docling, use the following command:
```bash pip install -qU langchain-docling ``` .. raw:: htmlThis will install the latest version of Docling along with the necessary integrations for LangChain.
π¦ Usage ____________________ .. raw:: htmlTo use Docling, you can import the DoclingLoader and specify the document path and export type. Hereβs a simple example:
1. Import Required Modules
.. code-block:: python from langchain_docling import DoclingLoader from langchain_docling.schema import ExportType .. raw:: html2. Choose Export Type
You can choose between:
* ExportType.DOC_CHUNKS β Document is split into chunks * ExportType.MARKDOWN β Whole document as a single markdown output .. code-block:: python export_type = ExportType.DOC_CHUNKS # or ExportType.MARKDOWN .. raw:: html3. Load Document
Specify the document path and export type:
.. code-block:: python 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() .. raw:: htmlThis 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 ____________________ .pdf .docx .pptx .html .txt .. raw:: htmlOther common document formats
--------------------------------------------------------------------------------------------------------------------------------------- π PDF Data Extraction docling ==================================== π§ Goal ------------------- **We will:** .. raw:: htmlExtract 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 -------------------------------------------------- .. code-block:: python # 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") .. raw:: htmlcode-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
--------------------
.. raw:: html
1. Open the HTML file in a browser or upload it to a DocLing-compatible editor
.. raw:: html2. Use DocLing to:
* Add metadata (e.g., title, author) * Annotate sections (e.g., introduction, methods, conclusion) * Tag named entities (dates, places, etc.) .. raw:: html3. Export as JSON, XML, or HTML for NLP training or archiving
β Summary ------------------ .. raw:: html| Feature | Included |
|---|---|
| Text Extraction | β |
| Table Extraction | β |
| Image Extraction | β |
| Prepares for DocLing | β |
DocLing is not a Python API, but a manual annotation platform.
This script prepares your PDFs to be easily annotated using DocLing.
If you need a script where annotations are automated, consider:
* Using spaCy + pdfplumber * Or using Label Studio (which has a Python API) Practice ---------------------- .. raw:: htmllink:
Colab Notebook -docling extracted PDF
GitHub Repository - PDF Extraction Script "docling"