Skip to main content

A Python library to convert Markdown files to PDF with Chinese support

Project description

Markdown PDF Converter

PyPI version Python version License

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

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdown_pdf_converter-1.0.0.tar.gz (14.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdown_pdf_converter-1.0.0-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

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

Hashes for markdown_pdf_converter-1.0.0.tar.gz
Algorithm Hash digest
SHA256 c6f36c1c8a9520eb89edda08875e420e5eec6269539f3fdb5938ab23bb8a51df
MD5 78d8a44b5b588af5632a3624f4c27afe
BLAKE2b-256 69ffabee8e1834de1783ab3adaafd84319b651ba366c45b4567b4b41ef64c34a

See more details on using hashes here.

File details

Details for the file markdown_pdf_converter-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for markdown_pdf_converter-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ca743fd3c17be42b562a213ec1644b8b92c7e8043b38b7439a830c5fdad6f3f
MD5 4738bd8ca5fea4e2500baaaa0eed07bd
BLAKE2b-256 159f6ea069041999167272808091827ee490306fbe793b8a73aaad0563a49811

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page