PDF to Markdown Online

How to Convert PDF to Markdown with Docling

Docling is IBM's open-source document converter (MIT license). It uses layout and table models to turn PDFs into structured Markdown, and it's one of the better choices for PDFs with tables. This guide takes you from install to batch conversion. Every snippet below was run with Docling 2.155.0 on Python 3.12 before we published it.

1. Install Docling

Docling needs Python 3.10 or newer. Install it into a virtual environment:

bash
python3 -m venv .venv
source .venv/bin/activate
pip install docling

The install pulls in PyTorch and is large (our environment came to about 1.3 GB). The first conversion also downloads Docling's layout and table models from Hugging Face. That one-time download is why the first run is slow.

2. Convert a PDF to Markdown in Python

The minimal example converts one file and writes the Markdown next to it:

python
from pathlib import Path
from docling.document_converter import DocumentConverter

converter = DocumentConverter()
result = converter.convert("report.pdf")
markdown = result.document.export_to_markdown()
Path("report.md").write_text(markdown, encoding="utf-8")

convert() also accepts a URL. Create the DocumentConverter once and reuse it: building it loads the models, and that is most of the time for small files.

On our two-table sales report, the output starts like this:

markdown
## Quarterly Sales Report

Revenue grew in three of four regions this quarter. The table below lists revenue and unit sales by region.

## Revenue by region

| Region        |   Revenue (USD) |   Units | Change   |
|---------------|-----------------|---------|----------|
| North America |       1,240,000 |   8,310 | +12%     |

3. Or use the command line

Docling installs a docling command. To convert a file to Markdown into a folder:

bash
docling report.pdf --to md --output out/

This writes out/report.md. On a text-based PDF, add --no-ocr to skip OCR. On our one-page report that cut the run from 11.7 to 6.6 seconds, model loading included:

bash
docling report.pdf --to md --no-ocr --output out/

4. OCR and table options

Docling's PDF pipeline is configured with PdfPipelineOptions. The two switches that matter most are OCR (needed only for scanned pages) and table structure recognition:

python
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions, TableFormerMode
from docling.document_converter import DocumentConverter, PdfFormatOption

pipeline_options = PdfPipelineOptions()
pipeline_options.do_ocr = False                  # text-based PDFs: skip OCR, much faster
pipeline_options.do_table_structure = True       # rebuild tables
pipeline_options.table_structure_options.mode = TableFormerMode.ACCURATE
pipeline_options.table_structure_options.do_cell_matching = True

converter = DocumentConverter(
    format_options={InputFormat.PDF: PdfFormatOption(pipeline_options=pipeline_options)}
)
markdown = converter.convert("report.pdf").document.export_to_markdown()

5. Batch convert a folder of PDFs

convert_all() processes many files with one converter. With raises_on_error=False a broken PDF is reported instead of stopping the run:

python
from pathlib import Path
from docling.datamodel.base_models import ConversionStatus
from docling.document_converter import DocumentConverter

converter = DocumentConverter()
pdfs = sorted(Path("pdfs").glob("*.pdf"))
out_dir = Path("markdown")
out_dir.mkdir(exist_ok=True)

for result in converter.convert_all(pdfs, raises_on_error=False):
    name = result.input.file.stem
    if result.status == ConversionStatus.SUCCESS:
        (out_dir / f"{name}.md").write_text(result.document.export_to_markdown(), encoding="utf-8")
        print("converted", name)
    else:
        print("failed", name, result.status)

6. Run offline (download the models once)

On servers without internet access, or behind a flaky connection, download the models ahead of time and point Docling at the folder:

bash
docling-tools models download layout tableformer -o ./docling-models
python
from docling.datamodel.base_models import InputFormat
from docling.datamodel.pipeline_options import PdfPipelineOptions
from docling.document_converter import DocumentConverter, PdfFormatOption

pipeline_options = PdfPipelineOptions(artifacts_path="./docling-models")
pipeline_options.do_ocr = False

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

We ran this with HF_HUB_OFFLINE=1 set, which blocks all Hugging Face downloads, to confirm it needs no network.

7. Common errors

LocalEntryNotFoundError … cannot find the appropriate snapshot folder

The model download from Hugging Face failed, usually because of a dropped connection or a proxy. We hit this ourselves on the first run. Run the conversion again, or use the offline setup in step 6 so the download happens once, separately.

The first file takes 20+ seconds

That's model loading, plus the one-time download on the very first run. In our test the first file took about 23 seconds and the next ones 0.5 to 5 seconds. Reuse one DocumentConverter for all files and switch off OCR for text PDFs.

All headings come out as ##

Docling's Markdown export uses the same heading level for every section title, so the outline is flat (we saw this on every test file). If heading levels matter, for Obsidian notes or chunking for RAG, you'll need to post-process, or use a converter that keeps levels.

Formulas show as <!-- formula-not-decoded -->

By default Docling detects formulas but does not convert them. It has an optional formula model (do_formula_enrichment), which downloads an extra model and is slower. We did not test it.

Docling vs Marker vs MarkItDown

From our test of the same five PDFs:

The full results, including five websites, are in best PDF to Markdown converters, and a side-by-side code comparison is in PDF to Markdown in Python.

Last updated