Document-to-Markdown conversion

Also known as: PDF to Markdown

On this page 7

Document-to-Markdown conversion is the process of transforming documents such as PDFs, scanned pages, slide decks and word-processor files into Markdown text that preserves their logical structure: headings, paragraphs, lists, tables, formulas and reading order. In Document AI, it has become a standard preprocessing step for retrieval-augmented generation, search indexing and large language model pipelines, where it is often described simply as PDF to Markdown.

Markdown is attractive because it is lightweight, human-readable and familiar to language models, which saw large amounts of it during training. A good conversion keeps the structural signals a reader would use, such as which line is a section title or which cells belong to one table row, while discarding visual noise like page headers, footers and decorative layout.

How document-to-Markdown conversion works

A conversion pipeline recovers logical structure from visual or binary layout and then serializes it as Markdown. The exact steps depend on whether the source has a usable text layer, but most modern systems follow the same broad sequence.

  1. Text acquisition: extract the embedded text layer from digital files, or run OCR on scanned images.
  2. Layout analysis: detect regions such as titles, body text, tables, figures, captions and footnotes.
  3. Reading order detection: arrange regions in the order a human would read them, including multi-column pages.
  4. Element recognition: reconstruct table cells, convert formulas to LaTeX and identify list hierarchy.
  5. Serialization: emit Markdown syntax, such as # headings, pipe tables and fenced code blocks.

The hardest step is usually reading order and table structure, because PDF files store glyphs at positions on a page rather than paragraphs or cells. A two-column scientific paper, for example, can easily be read across columns if the layout model fails, producing interleaved, meaningless sentences.

Main conversion approaches

Converters fall into three broad families: rule-based parsers, modular pipelines with specialized models, and end-to-end vision-language models that read a page image and write Markdown directly. Each makes a different trade-off between speed, cost and robustness.

ApproachHow it worksStrengthsWeaknesses
Rule-based text extractionReads the PDF text layer and applies heuristicsVery fast, cheap, deterministicFails on scans and complex layouts
Modular ML pipelineLayout detection, OCR and table models combinedGood structure, controllable stepsMore components to tune and maintain
End-to-end vision modelPage image in, Markdown text outHandles formulas and unusual layoutsSlower, costly, can hallucinate text
Hybrid with LLM cleanupPipeline output refined by a language modelFixes small structural errorsAdds cost and possible content drift
Comparison of conversion approaches

Open-source tools such as Docling represent the modular approach, research models such as Nougat show the end-to-end approach for scientific papers, and hosted services such as LlamaParse combine parsing with language model postprocessing.

Which document elements survive conversion

Markdown is intentionally limited, so some document features map cleanly and others are lost or approximated. Knowing these limits helps teams decide when Markdown is enough and when a richer format such as JSON with coordinates is needed.

Document elementMarkdown representationFidelity
HeadingsHash-prefixed heading levelsHigh if levels are detected correctly
ListsBulleted or numbered items with indentationHigh for simple nesting
Simple tablesPipe tables with a header rowGood
Merged or nested cellsFlattened pipe table or embedded HTMLLow, structure often lost
Mathematical formulasInline or block LaTeXGood with specialized models
Images and chartsImage reference or generated descriptionContent lost unless described
Fonts, colors, exact positionsNot representedDiscarded by design
Mapping of document elements to Markdown

Because pipe tables cannot express merged header cells, financial statements and scientific tables are frequent sources of error. Many pipelines therefore keep complex tables as embedded HTML or separate structured output while using Markdown for running text.

Role in retrieval and language model pipelines

In retrieval-augmented generation, conversion quality directly limits answer quality: a chunk that mixes two columns or breaks a table cannot be retrieved or interpreted correctly. Markdown headings also provide natural boundaries for structure-aware chunking, where each chunk carries its section path as context.

Compared with plain text extraction, Markdown keeps hierarchy and table alignment, and compared with full layout JSON, it is far more compact, which reduces token counts and inference cost when documents are passed to a language model. It is less suitable when downstream steps need bounding boxes for citations or redaction, because Markdown carries no page coordinates.

Quality evaluation and common failure modes

Teams evaluate conversion with text-level and structure-level metrics: character or word error rate for content, edit distance against reference Markdown, and table-structure scores such as TEDS for tables. A sample of manually reviewed pages from the real document mix remains essential, since public benchmarks rarely match a company’s own files.

The table below lists the failures practitioners encounter most often and how to address them.

FailureTypical causeRemedy
Interleaved sentencesWrong reading order on multi-column pagesUse a layout model with column detection
Repeated headers on every chunkPage furniture not removedFilter running headers and footers
Garbled charactersBroken font encoding in the PDFFall back to OCR for affected pages
Invented or missing sentencesHallucination in generative modelsCompare against OCR text, flag low-confidence pages
Shifted table columnsMerged cells or missing ruling linesUse a table-structure model or HTML output
Typical failures and remedies

A key warning applies to end-to-end models: their output can be fluent but unfaithful, for example repeating a line or completing a truncated sentence. For legal, medical or financial content, conversion should include automated consistency checks and human review of uncertain pages.

In plain words

A PDF is like a printed page. It knows where each letter sits, but not what the letters mean together. Document-to-Markdown conversion rebuilds the meaning. It finds the titles, the paragraphs, the lists and the tables.

The result is a simple text file with light marks, such as a hash sign before a title. People can read it easily. AI tools can read it even better. Good conversion means the AI sees the document the way a person does, in the right order and with tables intact.

A real-world example

An insurance company wants an internal assistant that answers adjusters’ questions about roughly 8,000 policy documents. The first prototype uses plain text extraction, and answers are often wrong, because coverage tables lose their columns and two-column clauses are read across the page.

The team switches to a modular conversion pipeline with layout detection, OCR for older scanned policies and a table-structure model. Each document becomes Markdown with proper headings, and chunks are split at section boundaries with the section path attached. Pages with low OCR confidence are flagged for manual review, which affects roughly 3 percent of pages.

In a review of 200 test questions, the share of answers judged correct rises noticeably, and adjusters report that cited passages now match the original policy wording. The company also saves tokens, because Markdown chunks are shorter than the earlier JSON layout output.