PDF to Markdown Converter (Local & Privacy-First)
English | 繁體中文
A lightweight, high-performance, and privacy-preserving tool to convert PDF documents into clean, LLM-ready GitHub-Flavored Markdown (GFM).
Designed for individuals, researchers, and enterprises handling sensitive documents who want 100% offline processing, zero cloud API costs, and seamless Windows desktop integration.
Key Highlights
- 100% Local & Privacy-First: Documents never leave your machine. No telemetry, no external network requests, and zero data leakage.
- Zero API Costs: No OpenAI, Anthropic, or cloud vision API keys required. All layout analysis and OCR run locally.
- Automatic Offline OCR: Scanned PDFs and raster images are recognized automatically using local ONNX models via RapidOCR.
- LLM-Ready Markdown Structure: Preserves hierarchical headers (
#, ##, ###), tables (| col | col |), code blocks, bullet points, and multi-column reading flow.
- Windows Explorer Context Menu: Convert any PDF instantly by right-clicking it in Windows Explorer (
Convert to Markdown (.md)).
- Flexible CLI: Convert single documents, specify custom outputs, or batch convert multiple files via standard terminal commands.
System Architecture & Engine Comparison
| Feature |
This Project (PyMuPDF4LLM + RapidOCR) |
Cloud Multimodal APIs |
Standard PDF Extractors |
| Privacy & Security |
100% Offline / Local |
Sends documents to cloud |
Local |
| Cost |
$0.00 (Free) |
Per-token / per-image fees |
$0.00 |
| Scanned PDF (OCR) |
Built-in ONNX OCR |
Supported (costly) |
Usually fails / empty |
| Table Formatting |
Markdown Pipe Tables |
Markdown Tables |
Raw unstructured text |
| Heading Detection |
Font-size hierarchy |
Inferred |
Plain text |
| Speed |
Sub-second (~0.7s) |
3-10s latency |
Fast |
Installation
Prerequisites
- Python 3.10 or higher (cross-platform support for Windows, macOS, and Linux)
Setup
🪟 Windows:
git clone https://github.com/Lin-yu5/PDF_to_md.git
cd PDF_to_md
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
🍎 macOS / Linux:
git clone https://github.com/Lin-yu5/PDF_to_md.git
cd PDF_to_md
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
Quick Usage Workflow (Windows & macOS)
Convert any PDF with a single right-click in Windows Explorer:
1-Click Install
Double-click install_context_menu.bat or run:
python setup_context_menu.py --install
- No Administrator Privileges Required: Registers in
HKEY_CURRENT_USER\Software\Classes\SystemFileAssociations\.pdf.
- Silent Background Execution: Runs via
pythonw.exe, so no intrusive command prompt window flashes on your screen.
- Output Location: Generates
<filename>.md directly in the same folder as the right-clicked PDF.
Uninstallation
Double-click uninstall_context_menu.bat or run:
python setup_context_menu.py --uninstall
🍎 macOS Integration Options
- VS Code Task (Easiest Cross-Platform):
- Open this repo in VS Code.
- Open or focus on any
.pdf file and press Cmd + Shift + B to generate <filename>.md.
- Mac Finder Quick Action (via Automator):
Command-Line Interface (CLI) Usage
💡 Tip: Use python on Windows and python3 on macOS / Linux (or run within the activated virtual environment).
1. Basic Conversion
Convert a PDF to Markdown in the same directory (document.pdf -> document.md):
python pdf_to_md.py document.pdf
2. Specify Output File
Specify an explicit output markdown file:
python pdf_to_md.py document.pdf output.md
# or using flag:
python pdf_to_md.py -o /path/to/custom_output.md document.pdf
3. Batch Conversion
Convert multiple PDF files in one command:
python pdf_to_md.py report1.pdf report2.pdf thesis.pdf
Extract embedded document images into a dedicated subfolder alongside the markdown file:
python pdf_to_md.py --extract-images report.pdf
5. Force Comprehensive OCR
Force OCR execution across all pages and embedded figures/tables:
python pdf_to_md.py --force-ocr slides.pdf
6. Retain Slide / Page Dividers
Insert clear page break dividers and page number indicators (--- \n*Page N*):
python pdf_to_md.py --page-separators slides.pdf
7. CLI Options Reference
usage: pdf_to_md.py [-h] [-o OUTPUT] [--engine {auto,pymupdf4llm,markitdown}]
[--extract-images] [--force-ocr] [--page-separators] [-v]
[-q] [--version]
[inputs ...]
positional arguments:
inputs Input PDF file(s).
options:
-h, --help Show help message and exit.
-o, --output OUTPUT Output markdown file path (single file mode).
--engine {auto,pymupdf4llm,markitdown}
Engine preference (default: auto, prefers pymupdf4llm).
--extract-images Extract and save embedded images locally.
--force-ocr Force OCR processing on all pages and embedded images.
--page-separators Insert page/slide break dividers in output markdown.
-v, --verbose Enable detailed logging output.
-q, --quiet Suppress non-error console output.
--version Show program's version number and exit.
Quality & Edge-Case Engineering
- RapidOCR NoneType Guard: Upstream PyMuPDF4LLM 2.1 contains an issue where images without extractable text cause
TypeError: 'NoneType' object is not iterable. This codebase includes an active runtime guard ensuring graceful handling of purely graphic/blank pages.
- Smart Fallback: If
pymupdf4llm encounters an unhandled document exception, the tool automatically attempts fallback extraction through markitdown.
- Silent Context Menu Error Logging: If a background conversion invoked from File Explorer fails, an error traceback is written to
convert_error.log in the target file’s directory for easy troubleshooting.
Project Structure
.
├── pdf_to_md.py # Main conversion script (CLI & context menu entrypoint)
├── setup_context_menu.py # Context menu installer/uninstaller script
├── install_context_menu.bat # 1-Click Windows context menu installer
├── uninstall_context_menu.bat # 1-Click Windows context menu uninstaller
├── add_pdf_to_md_context.reg # Generated registry file for context menu
├── remove_pdf_to_md_context.reg# Generated registry file to remove context menu
├── requirements.txt # Dependency specification
├── .gitignore # Git ignore rules
├── LICENSE # MIT License
├── README.md # Documentation (English)
└── README_zh.md # Documentation (Traditional Chinese)
License
This project is licensed under the MIT License.
评论