如何將 PDF 轉成 Markdown,供 RAG 和聊天機械人使用,以及何時不應轉換
How to Convert PDFs to Markdown for RAG and Chatbots (and When Not To)
將 PDF 轉成 Markdown 供 RAG 使用前,先確認是否含文字層;掃描檔須先做 OCR,再按版面選擇轉換工具。文中列出 PyMuPDF、Marker、Docling 和 MarkItDown,並建議檢查表格、多欄閲讀順序及重複頁首頁尾,再按標題分塊並保留頁碼。若所用的託管聊天機械人支援直接上載 PDF,可省去自建流程,但無法控制分塊方式。
Markdown keeps a document's headings, lists and tables in plain text that chunkers and LLMs handle well, so it's a sensible format to put between a PDF and your vector store. PDFs with a real text layer convert cleanly. Scanned PDFs need OCR first. Tables, multi-column layouts and repeated headers are where conversions usually go wrong, so check the output before you embed it.
Disclosure: this post is published by Appie.run. One of the conversion routes below is Appie's free converter and one section mentions Appie's chatbot product. The open-source routes work just as well, and for batch jobs they're the better choice.
Why Markdown before embedding
Three practical reasons:
-
Headings give you chunk boundaries. A
## Refundsheading is a natural place to split. Plain text extracted from a PDF loses that signal, and you end up splitting every N characters, often mid-sentence or mid-table. - Cleaner, smaller context. Markdown carries structure with very little markup. You avoid sending the model layout noise such as page furniture, odd line breaks and font artefacts.
- Citations. If you keep the heading and page number with each chunk, your bot or RAG app can say where an answer came from. That matters for policies, contracts and manuals, where people will want to check the source.
Markdown isn't magic, though. If the PDF has no text layer or the converter scrambles the reading order, the Markdown will be just as wrong as the plain text would have been.
Step 1: check what kind of PDF you have
Before choosing a tool, find out whether the PDF has a text layer.
The manual check: open it in any PDF viewer and try to select a sentence. If the text highlights word by word, there's a text layer. If you can only draw a box around the whole page, it's an image, which usually means a scan or a photo.
The scripted check (for a folder of files) uses PyMuPDF. Since PyMuPDF 2.x the Markdown helpers that used to live in the separate pymupdf4llm package ship inside pymupdf itself (per the pymupdf4llm repo, which is now an empty compatibility package).
# pip install --upgrade pymupdf
import pymupdf
doc = pymupdf.open("policy.pdf")
for number, page in enumerate(doc, start=1):
chars = len(page.get_text().strip())
flag = "OK" if chars > 50 else "NO TEXT LAYER? needs OCR"
print(f"page {number}: {chars} characters {flag}")
The 50-character threshold is arbitrary. A page that is mostly a diagram will also come back low, so treat the result as a prompt to look, not a verdict. We ran this against a small test PDF with PyMuPDF 2.0; adapt it for your setup.
Step 2: convert
Pick the route that matches the job.
Route A: a free web converter for one-off files
If you have one policy document or one manual and want Markdown in a minute, a web converter is the fastest path. Appie's free Convert PDF to Markdown tool (ours) extracts each page to Markdown, and you copy or download the result. Its limits, as stated on the tool page on 22 Oct 2026:
- PDFs with real text only. A photo scan needs OCR first.
- The first 30 pages are included.
- You complete a Cloudflare Turnstile captcha before it runs.
For anything longer than 30 pages or for a recurring job, use a library.
Route B: a Python library for batches
Four open-source options are worth a look. Descriptions below are taken from each project's own README, read on 22 Oct 2026; test them on your own documents, because results vary by layout.
-
PyMuPDF: its README lists "native Markdown output for RAG and AI pipelines" via
doc.to_markdown(). Lightweight and quick to install. - Marker: converts PDF, images, PPTX, DOCX, XLSX, HTML and EPUB to Markdown, JSON and chunks. The README says it formats tables, equations and code blocks, removes headers and footers, and can optionally call an LLM to improve accuracy. Runs on GPU, CPU or MPS.
- Docling: parses many formats with "advanced PDF understanding" including page layout, reading order and table structure, and exports Markdown, HTML and lossless JSON.
- MarkItDown (Microsoft): a lightweight utility for converting PDF, Office files, HTML and more to Markdown for LLM pipelines. Its README is candid that output is meant for text analysis tools, not high-fidelity human-readable conversion. It also warns that it performs I/O with the privileges of the current process, so sanitise inputs in untrusted environments.
A minimal batch script with PyMuPDF:
import pathlib
import pymupdf
src = pathlib.Path("pdfs")
out = pathlib.Path("markdown")
out.mkdir(exist_ok=True)
for pdf in src.glob("*.pdf"):
md = pymupdf.open(pdf).to_markdown()
(out / f"{pdf.stem}.md").write_text(md, encoding="utf-8")
print("converted", pdf.name)
Tested with PyMuPDF 2.0 on a two-page sample; adapt for your setup.
Route C: OCR first for scans
If Step 1 flagged pages without a text layer, you need OCR before (or as part of) conversion. Marker and Docling both list scanned-document handling in their READMEs, and MarkItDown lists OCR for images. Another approach is to run an OCR tool that adds a text layer to the PDF, then use Route A or B. Whatever you pick, OCR output needs a closer review than a born-digital PDF: check numbers, names and anything in a table.
Step 3: clean up
Run through this checklist on a sample of the output before converting hundreds of files:
- Headers and footers. Company name, document title and "Page 4 of 37" repeated on every page will pollute every chunk. Strip them with a regex or your converter's option.
- Tables. Open a few tables in a Markdown preview. Merged cells and tables that span two pages are common failure points. If a table is critical (a price list, a spec sheet), consider fixing it by hand or storing it as CSV alongside the Markdown.
- Reading order in multi-column layouts. Read a two-column page top to bottom. If paragraphs from the left and right columns interleave, try a layout-aware converter.
- Hyphenation. Words split across lines ("con-\nversion") hurt search. Rejoin them.
-
Page markers. Keep a page reference somewhere (a comment such as
<!-- page 12 -->or chunk metadata) so you can cite it later. - Images and charts. Most converters drop them or emit a placeholder. If a chart holds the answer to likely questions, describe it in text.
Step 4: chunk by heading and keep page numbers
Chunk on headings first, then split long sections by size. Keep the heading and page number as metadata on every chunk. Here's a small example using PyMuPDF's per-page output:
import re
import pymupdf
pages = pymupdf.open("policy.pdf").to_markdown(page_chunks=True) # one dict per page
chunks = []
for page in pages:
page_no = page["metadata"]["page_number"]
# split before each H1 to H3 heading, keeping the heading with its text
for section in re.split(r"\n(?=#{1,3} )", page["text"]):
section = section.strip()
if section:
heading = section.splitlines()[0].lstrip("# ").strip()
chunks.append({"text": section, "heading": heading, "page": page_no})
This splits per page, so a section that runs across a page break becomes two chunks. That's usually fine for retrieval, and the page numbers stay accurate. If you'd rather keep sections whole, convert the full document, split on headings, and record page numbers from the page markers you added in Step 3. Tested on a two-page sample with PyMuPDF 2.0; adapt for your setup.
A few rules of thumb (general guidance, not benchmarks):
- Prepend the heading path to each chunk's text ("Refund policy > EU orders") so the embedding knows the context.
- Keep tables whole inside one chunk where you can.
- Store the source file name with the page number, so answers can link back.
Skip the pipeline: upload the PDF straight to a hosted chatbot
If your goal is a support bot or an internal Q&A assistant, rather than a custom RAG system, you may not need any of the above. Many hosted chatbot builders accept PDFs directly and handle extraction and chunking for you; check whether the one you use does.
Appie AI Chatbots (ours) accepts PDFs, DOCX, CSV and text, plus URLs, sitemaps and product feeds, as knowledge sources (appie.run/ai-chatbots). Creating a bot is free, and each conversation with the bot costs 1 credit (pricing, as of 22 Oct 2026). The Free plan includes 500 credits and needs no credit card. It's a hosted bot, so you don't control the chunking; if you need that control, build the pipeline above.
To test whether a PDF answers questions well before building anything, try Appie's free AI Chat With Your PDF tool. You upload a text-based PDF and ask for a summary or a specific fact; per the tool page, answers must come from the PDF, and if the fact isn't there it says so. If it can't find answers you know are in the document, the PDF probably has a text-layer or layout problem to fix first.
For hosted options, see best AI chatbots trained on your own website data.
FAQ
How do I convert a PDF to Markdown for free?
For a single text-based PDF, use a free web converter such as Appie's Convert PDF to Markdown (first 30 pages, text PDFs only). For batches, use an open-source library such as PyMuPDF (doc.to_markdown()), Marker, Docling or MarkItDown.
Can I convert a scanned PDF to Markdown?
Only after OCR. A scan is an image of text, so a converter has nothing to extract until OCR adds a text layer. Some libraries, such as Marker and Docling, include OCR; review OCR output carefully.
Is Markdown better than plain text for RAG?
Usually, because headings, lists and tables survive, which gives you better chunk boundaries and lets you keep section context for citations. It won't fix a bad extraction, so check the output either way.
How should I chunk Markdown from a PDF?
Split on headings first, then split long sections by size. Keep the heading path, source file and page number as metadata on each chunk, and keep tables whole where you can.
Convert a text PDF to Markdown free (first 30 pages): appie.run/tools/convert-pdf-to-markdown. Or upload the PDF straight into an Appie chatbot; the Free plan has 500 credits and needs no credit card. More free tools: appie.run/tools.
來源:DEV Community · dev.to