md2pdf: Markdown to PDF Converter

3.1.1 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `md2pdf` function from the core API to convert a Markdown string directly into a PDF file. You can specify Markdown content, output destination, optional CSS for styling, and desired Markdown extensions.

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')

view raw JSON →