Parse PDFs with OCR and table structure

Most RAG tutorials start with pdf.read() and wonder why retrieval quality is terrible. The real bottleneck in long-document RAG is parsing. If your PDF has scanned pages, multi-column layouts, or tables, a naive extractor will destroy the structure before you ever embed a chunk.

Docling is built for this. It detects layout, runs OCR on image-only pages, extracts table cells into proper markdown tables, and preserves heading hierarchy. That means your chunks arrive with chapter titles still attached, which matters a lot when a learner asks about a specific section.

booktutor.py
python
from docling.document_converter import DocumentConverter, PdfFormatOption
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import (
    AcceleratorDevice,
    AcceleratorOptions,
    PdfPipelineOptions,
)

accelerator_options = AcceleratorOptions(
    num_threads=8, device=AcceleratorDevice.AUTO
)

pipeline_options = PdfPipelineOptions()
pipeline_options.accelerator_options = accelerator_options
pipeline_options.do_ocr = True
pipeline_options.do_table_structure = True
pipeline_options.table_structure_options.do_cell_matching = True

converter = DocumentConverter(
    format_options={
        InputFormat.PDF: PdfFormatOption(
            pipeline_options=pipeline_options,
        )
    }
)

Three flags matter: do_ocr for scanned pages, do_table_structure for cell extraction, and do_cell_matching for merging cells correctly. Eight threads keeps parsing quick on a modern laptop.

If you know your PDF is a pure text export, yes, turning OCR off is faster. But books, scientific papers, and scanned manuals often have image-only pages mixed in with text pages. Leaving OCR on is cheap insurance. Docling skips OCR automatically on pages that already have a text layer, so the cost only shows up on the pages that actually need it.

Quiz: Quiz

Loading practice…