A Python library to convert Markdown files to PDF with Chinese support
Project description
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! ๐
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file markdown_pdf_converter-1.0.0.tar.gz.
File metadata
- Download URL: markdown_pdf_converter-1.0.0.tar.gz
- Upload date:
- Size: 14.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c6f36c1c8a9520eb89edda08875e420e5eec6269539f3fdb5938ab23bb8a51df
|
|
| MD5 |
78d8a44b5b588af5632a3624f4c27afe
|
|
| BLAKE2b-256 |
69ffabee8e1834de1783ab3adaafd84319b651ba366c45b4567b4b41ef64c34a
|
File details
Details for the file markdown_pdf_converter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: markdown_pdf_converter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0ca743fd3c17be42b562a213ec1644b8b92c7e8043b38b7439a830c5fdad6f3f
|
|
| MD5 |
4738bd8ca5fea4e2500baaaa0eed07bd
|
|
| BLAKE2b-256 |
159f6ea069041999167272808091827ee490306fbe793b8a73aaad0563a49811
|