Markdown PDF Converter
A Python library to convert Markdown files to PDF with Chinese support and intelligent code wrapping.
✨ Features
- 📝 Markdown to PDF: Convert Markdown files to beautifully formatted PDF documents
- 🇨🇳 Chinese Support: Automatic Chinese font detection and rendering
- 💻 Code Block Handling: Smart formatting for code blocks with proper line wrapping
- 🎨 Customizable Styles: Multiple style presets (default, compact, large print)
- 🔧 Flexible Configuration: Customize fonts, margins, font sizes, and colors
- 🚀 Command Line Interface: Easy-to-use CLI for quick conversions
- 📦 Pure Python: No external dependencies except reportlab
📦 Installation
pip install markdown-pdf-converter
Development Installation
git clone https://github.com/yourusername/markdown-pdf-converter.git
cd markdown-pdf-converter
pip install -e ".[dev]"
🚀 Quick Start
Command Line Usage
# Basic conversion
md2pdf input.md
# Specify output file
md2pdf input.md -o output.pdf
# Use compact style for dense content
md2pdf input.md --style compact
# Use custom font
md2pdf input.md --font /path/to/chinese/font.ttf
# Customize font sizes and margins
md2pdf input.md --title-size 18 --body-size 10 --margin 2.5
Python API Usage
from markdown_pdf_converter import MarkdownToPDFConverter
from markdown_pdf_converter.styles import PDFStyles
# Basic usage
converter = MarkdownToPDFConverter()
converter.convert("input.md", "output.pdf")
# With custom font
converter = MarkdownToPDFConverter(font_path="/path/to/font.ttf")
converter.convert("input.md", "output.pdf")
# With custom styles
styles = PDFStyles(
title_font_size=20,
body_font_size=11,
right_margin=2.5,
left_margin=2.5
)
converter = MarkdownToPDFConverter()
converter.convert("input.md", "output.pdf", **styles.to_dict())
# Use preset styles
converter = MarkdownToPDFConverter()
converter.convert("input.md", "output.pdf", **PDFStyles.compact().to_dict())
📖 Supported Markdown Features
- ✅ Headers (H1, H2, H3)
- ✅ Bold and italic text
- ✅ Lists (ordered and unordered)
- ✅ Code blocks and inline code
- ✅ Blockquotes
- ✅ Links
- ✅ Horizontal rules
- ✅ Tables (simplified)
🎨 Style Presets
Default Style
Standard formatting suitable for most documents.
from markdown_pdf_converter.styles import PDFStyles
styles = PDFStyles.default()
Compact Style
Optimized for dense content like technical documentation.
from markdown_pdf_converter.styles import PDFStyles
styles = PDFStyles.compact()
Large Print Style
Enhanced readability with larger fonts.
from markdown_pdf_converter.styles import PDFStyles
styles = PDFStyles.large_print()
🔧 Configuration Options
PDFStyles Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
page_size |
str | "A4" | Page size (A4, Letter, Legal) |
right_margin |
float | 2.0 | Right margin in cm |
left_margin |
float | 2.0 | Left margin in cm |
top_margin |
float | 2.0 | Top margin in cm |
bottom_margin |
float | 2.0 | Bottom margin in cm |
title_font_size |
int | 16 | H1 font size |
heading_font_size |
int | 13 | H2 font size |
subheading_font_size |
int | 11 | H3 font size |
body_font_size |
int | 9 | Body text font size |
code_font_size |
int | 8 | Code block font size |
🌐 Chinese Font Support
The library automatically detects and uses Chinese fonts from common system locations:
- macOS: PingFang, STHeiti, Hiragino Sans GB
- Linux: WQY Zenhei, WQY Microhei, Noto Sans CJK
- Windows: SimSun, Microsoft YaHei (add your font path manually)
Custom Font
If automatic detection fails, specify a font path:
converter = MarkdownToPDFConverter(font_path="/path/to/your/font.ttf")
🧪 Testing
# Run tests
pytest
# Run tests with coverage
pytest --cov=markdown_pdf_converter
# Run linting
black markdown_pdf_converter/
flake8 markdown_pdf_converter/
📁 Project Structure
markdown-pdf-converter/
├── markdown_pdf_converter/ # Main package
│ ├── __init__.py
│ ├── converter.py # Core conversion logic
│ ├── styles.py # Style configuration
│ └── cli.py # Command line interface
├── tests/ # Test files
├── examples/ # Example files
├── pyproject.toml # Package configuration
├── README.md # This file
└── LICENSE # MIT License
🤝 Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
📝 License
This project is licensed under the MIT License - see the LICENSE file for details.
🐛 Issues
If you encounter any issues or have suggestions, please open an issue.
🙏 Acknowledgments
- Built with ReportLab - PDF generation library
- Inspired by the need for simple Markdown to PDF conversion with Chinese support
Happy Converting! 🎉
Metadata
Release files for markdown-pdf-converter 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| markdown_pdf_converter-1.0.0.tar.gz | 14.0 kB | Without provenance | Details for markdown_pdf_converter-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| markdown_pdf_converter-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for markdown_pdf_converter-1.0.0-py3-none-any.whl |
Total release size: 24.7 kB
Release files / markdown_pdf_converter-1.0.0.tar.gz
| Download URL | markdown_pdf_converter-1.0.0.tar.gz |
|---|---|
| Size | 14.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c6f36c1c8a9520eb89edda08875e420e5eec6269539f3fdb5938ab23bb8a51df
|
|
BLAKE2b-256 checksum How to use checksums |
69ffabee8e1834de1783ab3adaafd84319b651ba366c45b4567b4b41ef64c34a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.9
|
Release files / markdown_pdf_converter-1.0.0-py3-none-any.whl
| Download URL | markdown_pdf_converter-1.0.0-py3-none-any.whl |
|---|---|
| Size | 10.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0ca743fd3c17be42b562a213ec1644b8b92c7e8043b38b7439a830c5fdad6f3f
|
|
BLAKE2b-256 checksum How to use checksums |
159f6ea069041999167272808091827ee490306fbe793b8a73aaad0563a49811
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.9
|
Security for markdown-pdf-converter 1.0.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.