PDF to Markdown for LLMs and RAG: Real-World Tests and Pitfall Guide

A real-world test and best-practices guide for converting PDFs to Markdown for LLMs and RAG, using NIST CSF 2.0 as an example.
The article explains that LLMs read text, not pages, so PDFs must be converted into character streams. Markdown is ideal because it preserves structure while being compact. Using the official NIST CSF 2.0 PDF (1.53M bytes, 32 pages), one API call produced ~65K characters (~16K tokens) of Markdown in 1.7 seconds — 4% of the original size — while preserving headings, lists, and tables. It also notes merged-cell misalignment, explains why XFA forms and scanned PDFs fail due to missing text layers, and stresses that good converters should flag emptyText. It concludes with five rules for LLM document processing and details the Leafmelt API's multi-format conversion, token counts, MCP support, and pricing.
Language models read text, not pages. Before handing a PDF to a model, someone must first convert the fonts, columns, headers, footers, and tables into a character stream — and this step directly determines how much the model can understand. Markdown is currently the best format for this task: it preserves structure such as headings, lists, and tables, while being only a fraction of the original PDF's size.

A Real Test: NIST Cybersecurity Framework 2.0
To verify conversion quality, the author converted the official PDF of the NIST Cybersecurity Framework 2.0 (CSWP 29, 32 pages) with a single API call. Here are the results:
| Metric | Result |
|---|---|
| PDF size | 1,518,858 bytes |
| Markdown characters | 65,426 |
| Approximate tokens | 16,357 |
| Conversion time | 1,451 ms (1.7 s end-to-end) |
| Structure preserved | Page markers, headings, lists, Table 1 converted to a Markdown table |
The original PDF was about 1,483 KB, while the converted Markdown text is only about 64 KB — compressed to roughly 4% of the original size.
At around 16,000 tokens, the entire framework easily fits into the context window of current models, with room to spare. For RAG scenarios, each ### heading is a natural chunk boundary, making it easy for retrieval systems to split by semantics.
Here is the beginning of Table 1 after conversion:
Table 1. CSF 2.0 Core Function and Category names and identifiers
| Function | Category | Category Identifier |
| - | - | - |
| Organizational Context | GV.OC |
| Risk Management Strategy | GV.RM |
| Roles, Responsibilities, and Authorities | GV.RR |
An honest caveat: the merged "Function" cells in this table (Govern, Identify, etc.) were not repeated on every row, which caused the first column to become misaligned. Merged cells are the hardest part of all PDF table conversion. If a table matters, be sure to check it manually before indexing.
About NIST CSF and CSWP 29: The NIST Cybersecurity Framework (CSF) is a cybersecurity risk management guide published by the U.S. National Institute of Standards and Technology, widely used by enterprises and government agencies for compliance and security programs. CSF 2.0 was released in February 2024 and added a "Govern" function for the first time, expanding the core functions from the original five to six: Govern, Identify, Protect, Detect, Respond, and Recover. The "CSWP" in document number CSWP 29 stands for the NIST Cybersecurity White Paper series. Such documents often contain many structured tables, making them an ideal sample for testing PDF-to-Markdown quality.
Which PDFs Fail, and How to Spot Them
The author also tested the IRS W-9 form PDF. The conversion itself completed in about one second, but the 6-page document returned only 85 characters — because the form is in XFA format, where the text lives in the script layer rather than the page layer. Scanned PDFs (photos of paper) fail in the same way, because they have no text layer at all.
A good converter should not silently return an empty string, but should say so explicitly. In this case, Leafmelt sets emptyText: true with a note, allowing downstream pipelines to route these files to OCR processing.
To determine whether a PDF is a "risk," watch for a few signals: an unusually short conversion result, no text output, or metadata indicating XFA or scanned-document characteristics.
Why XFA and scanned documents are different: XFA (XML Forms Architecture) is a dynamic form specification designed by Adobe for PDF forms. The form's field definitions, data bindings, and rendering logic are stored as XML, and in many cases no visible text layer is generated in the PDF page content stream. As a result, the parser sees the page as "empty," even though users can see the complete form in a reader. By contrast, a scanned PDF is an image-based PDF created by photographing or scanning a paper document; the page itself is a pixel image with no extractable characters. Both types of files need to go through an OCR (optical character recognition) path: first perform layout analysis to locate text regions, then recognize characters block by block. Recognition quality is affected by resolution, skew angle, and fonts, and usually requires manual spot-checking before indexing, especially for tables and numeric fields.
Five Rules for LLM-Oriented Document Processing
- Preserve structure. Headings and tables in Markdown let both the model and the chunker see where topics begin and end.
- Count tokens before sending. A token count for each document helps you decide whether to chunk, summarize, or send the whole thing.
- Remove noise. Repeated headers, footers, and page numbers waste tokens in every chunk.
- Flag empty conversions. Scanned files and XFA forms require OCR. Do not index blank pages.
- Keep source links. Every chunk should include a source link so answers can cite the originating page.
Converting Documents via API
Leafmelt supports converting up to 5 documents per call and can process PDF, Word, Excel, OpenDocument, Apple Numbers, CSV, HTML, and XML via URL or upload, with a maximum of 10 MB per file. It returns clean Markdown (with headings and tables preserved), character and token counts, document metadata, and an empty-text flag. The same key can also be used as a remote MCP server, allowing agents to read documents directly during a conversation.
curl "https://leafmelt.cybermaxtools.com/api/convert?url=https://nvlpubs.nist.gov/nistpubs/CSWP/NIST.CSWP.29.pdf"
On pricing: 50 free calls per day, no key or credit card required. Paid plans start at $19/month (3,000 calls), or $5 pay-as-you-go (750 calls, no expiration).
For comparison, publicly listed prices for PDF extractors on the Apify Store range from $0.005 to $0.02 per PDF, plus per-page fees. Leafmelt's free tier is already sufficient for low-frequency use cases.
Source document: NIST CSWP 29, The NIST Cybersecurity Framework (CSF) 2.0, February 26, 2024 (U.S. government work).
The role of MCP: MCP (Model Context Protocol) is an open protocol that lets AI agents call external tools and data sources in a standardized way. When a conversion service is exposed as a remote MCP server, the agent does not need to pre-convert documents in bulk. Instead, it can call a "read document" tool on demand during a conversation, obtain Markdown content in real time, and then reason or cite based on it. This pattern is especially suitable for handling files users upload on the fly or scenarios requiring dynamic retrieval from multiple sources, and it saves tokens and storage compared with preprocessing all documents at once.
One-line Summary
Using NIST CSF 2.0 as an example, this shows methods, pitfalls, and best practices for converting PDFs to Markdown for LLMs and RAG.
Paragraph Summary
The article notes that language models can only read text, not pages, so PDFs must first be converted into character streams. Markdown is ideal because it preserves structure while being much smaller. The author ran a real test with the official NIST CSF 2.0 PDF (1.53 million bytes, 32 pages). One API call produced about 65,000 characters, or 16,000 tokens, of Markdown — compressed to 4% of the original size — in 1.7 seconds, while preserving headings, lists, and tables. The article also candidly points out that merged cells cause table misalignment, and explains that XFA forms and scanned PDFs fail conversion because they lack a text layer. A good converter should explicitly report this via an emptyText flag. Finally, it summarizes five rules for LLM-oriented document processing and introduces the Leafmelt API's multi-format conversion, token counting, MCP service capabilities, and pricing plans.
Related articles

Chain of Thought Isn't Just for AI: How Humans Can Use CoT to Improve Thinking Quality
Chain of Thought isn't just a prompt trick for large models. Starting from a Hacker News discussion, this article analyzes why CoT works for human thinking too, breaks down actionable ways to transfer it, and highlights the key differences between human CoT and machine CoT.
Cloudflare Teams Up with OpenAI: Closi…
Cloudflare Teams Up with OpenAI: Closing the Loop from Vulnerability Discovery to Defense with AI
Cloudflare uses OpenAI Daybreak to investigate security vulnerabilities and generate reviewable remediation plans, while developers use Codex to boost coding efficiency and quality.

Orca Multi-Agent Orchestration in Practice: A Guide to Parallel Collaboration with Claude and Codex
Orca is Stable AI's free open-source agent workbench supporting 34 agents like Claude Code and Codex in parallel. Learn its Git worktree isolation, multi-model orchestration, and mobile management.