Overview
ToMarkdown() is the primary extraction method in PDF4LLM. It reads a document and returns its content as a Markdown string, preserving headings, lists, tables, code blocks, images, and reading order as closely as possible.
Common options
Page selection
Extract only specific pages by passing a list of zero-based page indices:Per-page chunks
UseLlamaMarkdownReader to return one document object per page instead of a single concatenated string. Each chunk includes the page’s Markdown text and associated metadata:
Headers and footers
PDF4LLM uses bounding box position to identify and exclude repeating page headers and footers. Filter them by building the page list and usingToJson to identify the margin bands, or exclude them at the chunking stage by filtering short leading and trailing lines from each page chunk.
For documents with consistent header and footer heights, the most reliable approach is to filter blocks by their bounding box position using ParseDocument:
Images
To extract embedded images and reference them inline in the Markdown output:Tables
Table extraction runs automatically. PDF4LLM renders detected tables as GitHub-flavoured Markdown tables:Full example
A more complete call combining several options:LlamaMarkdownReader for per-page output:
For the full API signature including all parameters and return types, see the ToMarkdown() API reference.
Next steps
Extract JSON
Bounding boxes and layout data for custom pipelines.
Extract Text
Get clean, plain text output.