The switches on the PDF pipeline
The PDF conversion so far used the default settings. Those defaults do work you may not need, and the way to change them is one object passed in at converter construction.
A converter maps each format to the options for that format. Change the PDF ones and the rest of the course keeps working unchanged.
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
options = PdfPipelineOptions()
converter = DocumentConverter(
format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=options)})Three imports and two objects, and it is the shape every customisation in this lesson takes. PdfFormatOption is the wrapper that ties one set of options to one format.
What is on by default
from docling.datamodel.pipeline_options import PdfPipelineOptions
defaults = PdfPipelineOptions()
print("ocr ", defaults.do_ocr)
print("table structure", defaults.do_table_structure)
print("page images ", defaults.generate_page_images)OCR and table structure are on, page images are off. So every PDF you have converted so far ran an optical character recognition pass looking for text that is a picture of text.
Turning OCR off
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption
options = PdfPipelineOptions(do_ocr=False)
quick = DocumentConverter(
format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=options)})
print(quick.convert("returns.pdf").document.export_to_markdown()[:38])Identical text, noticeably faster. This PDF has real text in it, so there was never anything for OCR to find; the pass ran anyway and found nothing. On a folder of files like this one, turning it off is free speed.
do_ocr=False it converts to a document with no text in it at all. No error, no warning, an empty document. Lesson 17 is how you notice.Page images
options = PdfPipelineOptions(generate_page_images=True, images_scale=2.0)
shown = DocumentConverter(
format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=options)})
page = shown.convert("returns.pdf").document.pages[1]
print(page.image.pil_image.size, "at", page.image.dpi, "dpi")An image per page, rendered at twice the document's own resolution. A page is 612 points wide and 72 points is an inch, so scale 2 gives 144 dots per inch and a 1224 pixel image. That image plus the boxes from lesson 14 is everything you need to draw a highlight over the original page.
The rest of the switches
| Option | Default | What it does |
|---|---|---|
do_ocr | True | Look for text that is a picture of text |
do_table_structure | True | Run the table model to find rows and columns |
table_structure_options.mode | accurate | Swap to fast for a quicker, rougher table |
generate_page_images | False | Keep a rendered image of each page |
images_scale | 1.0 | How large those images are |
enable_remote_services | False | Allow a step that sends your document away |
The last one is a lock rather than a feature. Docling runs everything locally unless you turn that on, and a step that would call out to a hosted model raises instead. Lesson 26 is the one place this course goes near it.
- Set
table_structure_options.modetoTableFormerMode.FASTand convert again. - Convert with
do_table_structure=Falseand check the markdown is unchanged for this file. - Render the pages at
images_scale=1.0and compare the pixel size.
Every expert started right here.