md2pdf: Markdown to PDF Converter
md2pdf is a Python library and CLI tool for converting Markdown files to PDF, offering features like custom styling, Jinja templating, and support for various Markdown extensions. It is currently at version 3.1.1 and is actively maintained, receiving regular updates for new features, bug fixes, and security enhancements.
Common errors
-
md2pdf: command not found
cause The `md2pdf` CLI executable was not installed because the `cli` extra was omitted during installation.fixInstall `md2pdf` with the CLI extras: `pip install "md2pdf[cli]`. -
ModuleNotFoundError: No module named 'md2pdf'
cause The `md2pdf` library is not installed in the active Python environment, or the environment is not correctly activated.fixInstall the library using pip: `pip install md2pdf` (or `pip install "md2pdf[cli]` for CLI). -
md2pdf: error: unrecognized arguments: README.md output.pdf
cause This error typically occurs when using pre-v3.0.0 CLI syntax (positional arguments) on `md2pdf` versions 3.0.0 or later, where the CLI switched to Typer and uses only options.fixUpdate your CLI command to use options: `md2pdf --input README.md --output output.pdf`. -
md2pdf.exceptions.ConversionError: WeasyPrint exited with error code 1. Make sure all external dependencies are installed. More info: https://doc.weasyprint.org/en/stable/install.html
cause WeasyPrint, a core dependency of md2pdf, relies on external system libraries like Pango, Cairo, and GDK-Pixbuf. These need to be installed at the operating system level.fixInstall the necessary system dependencies for WeasyPrint. For Debian/Ubuntu: `sudo apt-get install python3-dev libffi-dev libxml2-dev libxslt1-dev libjpeg-dev libpng-dev zlib1g-dev libpango1.0-0 libcairo2 libgdk-pixbuf2.0-0`. For macOS: `brew install pango cairo gdk-pixbuf`.
Warnings
- breaking The command-line interface (CLI) underwent a significant breaking change in `v3.0.0`. It switched to Typer and moved from positional arguments to exclusively using options.
- breaking `md2pdf` dropped support for Python versions older than 3.10 starting from `v2.0.0`. Attempts to install or run on older Python interpreters will fail.
- gotcha Running the `md2pdf` command-line tool requires installing the `cli` extra (`pip install "md2pdf[cli]"`). A basic `pip install md2pdf` will only install the library API, not the executable CLI.
- gotcha Since `v2.0.0`, all input Markdown content is automatically treated as a Jinja2 template. This means any Jinja-like syntax (e.g., `{{ variable }}` or `{% for item in list %}`) will be processed, potentially leading to errors or unexpected output if not intended.
Install
-
pip install md2pdf -
pip install "md2pdf[cli]"
Imports
- md2pdf
from md2pdf.core import md2pdf
Quickstart
from md2pdf.core import md2pdf
import os
markdown_content = """
# My Awesome Document
Hello, **md2pdf**!
This is an example of converting Markdown to PDF using the library's API.
- Item 1
- Item 2
```python
print("Code blocks are also supported")
```
"""
output_filename = "my_document.pdf"
try:
md2pdf(
md_content=markdown_content,
dest=output_filename,
css_file=None, # Path to a CSS file for custom styling, or None
base_url=None, # Base URL for relative paths in markdown
md_extensions=['extra', 'fenced_code', 'tables'] # Common Markdown extensions
)
print(f"Successfully created PDF: {output_filename}")
except Exception as e:
print(f"Error converting markdown to PDF: {e}")
# For WeasyPrint related errors, check system dependencies (see 'Problems')