Skip to content

gekakud/mark2pdf

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mark2pdf

CI Python 3.9+ License: MIT

A Python library for converting Markdown files to PDF with cross-platform support.

Features

  • Cross-platform: Automatically selects the best PDF rendering backend for your OS
  • Simple API: One function to convert Markdown to PDF
  • Extensible: Support for tables, fenced code blocks, syntax highlighting, and table of contents
  • Customizable: Use your own CSS for styling

Installation

Windows

pip install mark2pdf[windows]

This installs Playwright, which uses your installed Chrome browser for PDF generation.

Linux

pip install mark2pdf[linux]

This installs WeasyPrint for PDF generation.

All Platforms

pip install mark2pdf[all]

Installs both backends for maximum compatibility.

Usage

Basic Usage

from mark2pdf import mark_convert

# Convert a Markdown file to PDF
mark_convert("output.pdf", md_file_path="document.md")

From String Content

from mark2pdf import mark_convert

md_content = """
# Hello World

This is a **Markdown** document.

- Item 1
- Item 2
- Item 3
"""

mark_convert("output.pdf", md_content=md_content)

With Custom CSS

from mark2pdf import mark_convert

mark_convert(
    "output.pdf",
    md_file_path="document.md",
    css_file_path="custom-styles.css"
)

API Reference

mark_convert(pdf_file_path, md_file_path=None, md_content=None, css_file_path=None)

Convert Markdown to PDF.

Parameters:

  • pdf_file_path (str): Output PDF file path
  • md_file_path (str, optional): Path to input Markdown file
  • md_content (str, optional): Markdown content as string (alternative to md_file_path)
  • css_file_path (str, optional): Path to custom CSS file

Raises:

  • ValueError: If neither md_file_path nor md_content is provided
  • FileNotFoundError: If md_file_path doesn't exist

How It Works

mark2pdf uses platform-specific backends for PDF generation:

Platform Backend Description
Windows Playwright Uses Chrome's PDF printing capabilities
Linux WeasyPrint Direct HTML-to-PDF conversion
macOS Auto Tries WeasyPrint first, then Playwright

The conversion process:

  1. Parse Markdown to HTML using Python's markdown library
  2. Apply default CSS styling (or custom CSS if provided)
  3. Generate PDF using the platform-appropriate backend

Supported Markdown Features

  • Headings
  • Bold, italic, strikethrough
  • Lists (ordered and unordered)
  • Tables
  • Fenced code blocks with syntax highlighting
  • Links and images
  • Blockquotes
  • Table of contents (via [TOC] marker)

Testing

Run the test script:

python test.py

This will convert a sample Markdown file to PDF using the default settings.

Development

Install in development mode:

pip install -e .[all]

License

MIT License

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published