← 返回
未分类

mdtopdf

Convert Markdown files to professionally formatted PDF documents using Python's md2pdf library with WeasyPrint engine. Produces publication-quality output with optimized typography, syntax highlighting, proper page layout, and Chinese font support. Use this skill whenever the user wants to generate a PDF from Markdown, export documentation as PDF, create a printable report from MD content, convert .md files to PDF, or produce a formatted document from text. Also trigger when users mention "PDF r
>将md转换成pdf的skilss
大森
未分类 community v1.0.0 1 版本 99453.6 Key: 无需
★ 0
Stars
📥 182
下载
💾 2
安装
1
版本
#latest

概述

md2pdf — Markdown to Professional PDF

Convert Markdown content into beautifully formatted PDF documents using md2pdf (WeasyPrint engine) with a bundled professional CSS stylesheet.

Prerequisites

Before first use, install the required dependencies:

# macOS system dependencies for WeasyPrint
brew install cairo pango gdk-pixbuf libffi

# Python package
pip install md2pdf

On Ubuntu/Debian:

sudo apt install libpango-1.0-0 libpangocairo-1.0-0 libgdk-pixbuf2.0-0 libffi-dev shared-mime-info
pip install md2pdf

Verify installation:

python -c "from md2pdf.core import md2pdf; print('md2pdf ready')"

Quick Start

The simplest way — use the bundled conversion script:

python @scripts/convert.py input.md output.pdf

The script automatically uses the bundled CSS stylesheet at @assets/markdown.css for professional formatting. If no output path is given, it defaults to the input filename with .pdf extension.

Using the Python API Directly

For more control, call md2pdf directly in Python:

from pathlib import Path
from md2pdf.core import md2pdf

# From a Markdown file
md2pdf(
    pdf=Path("output.pdf"),
    md=Path("input.md"),
    css=Path("@assets/markdown.css"),
    base_url=Path("."),  # resolves relative image paths
)

# From a Markdown string
md2pdf(
    pdf=Path("output.pdf"),
    raw="# Hello\n\nThis is **bold** text.",
    css=Path("@assets/markdown.css"),
)

Bundled Stylesheet Features

The @assets/markdown.css provides:

  • Page layout: A4 size, 25mm/20mm margins, centered page numbers in footer
  • Typography: Serif body text (Noto Serif SC / Libre Baskerville / Georgia), sans-serif headings (Noto Sans SC / Helvetica Neue), optimized line-height 1.7
  • Chinese font support: Noto Serif SC, Source Han Serif SC, Songti SC for body; Noto Sans SC, Source Han Sans SC, PingFang SC for headings
  • Code: Monospace stack (SF Mono / Cascadia Code / Fira Code), light background, Pygments GitHub-style syntax highlighting
  • Tables: Clean borders, alternating row colors, header styling, repeat headers across pages
  • Blockquotes: Left blue border, subtle background
  • Links: Print-friendly — external URLs shown inline after link text
  • Page breaks: Headings never orphaned, tables and code blocks avoid breaking across pages
  • First page: Extra top margin, no page number

Customizing the Output

Custom CSS

Pass your own CSS file to override or extend the default styles:

python @scripts/convert.py input.md output.pdf --css my-styles.css

Or via Python API:

md2pdf(pdf=Path("output.pdf"), md=Path("input.md"), css=Path("my-styles.css"))

Common CSS Customizations

Change page size to Letter:

@page { size: letter; }

Add a header with document title:

@page {
    @top-center {
        content: "My Document";
        font-size: 8pt;
        color: #999;
    }
}

Add "Page X of Y" footer:

@page {
    @bottom-center {
        content: counter(page) " / " counter(pages);
        font-size: 9pt;
        color: #888;
    }
}

Change body font:

body { font-family: "Merriweather", Georgia, serif; }

Disable URL display in print:

a[href^="http"]:after { content: none; }

Document Title

Use --title to inject a title heading when the Markdown content doesn't start with one:

python @scripts/convert.py report.md --title "Q4 Engineering Report"

Working with Images

Images with relative paths require base_url to resolve correctly. The conversion script sets this automatically to the Markdown file's parent directory. When using the Python API directly:

md2pdf(
    pdf=Path("output.pdf"),
    md=Path("docs/report.md"),
    css=Path("@assets/markdown.css"),
    base_url=Path("docs/"),  # images in docs/ will resolve correctly
)

Markdown Extensions Supported

The conversion script enables these extensions by default:

  • tables — GFM table syntax
  • pymdownx.superfences — Nested code blocks with syntax highlighting
  • pymdownx.betterem — Improved emphasis handling
  • pymdownx.magiclink — Auto-link URLs and issue references
  • footnotes[^1] style footnotes
  • smarty — Smart quotes, dashes, ellipses

Troubleshooting

ProblemSolution
-------------------
ImportError: No module named 'md2pdf'Run pip install md2pdf
OSError: cannot load library 'pango'Install system deps: brew install cairo pango gdk-pixbuf libffi (macOS)
Chinese characters show as squaresEnsure Chinese fonts are installed: brew install font-noto-serif-cjk font-noto-sans-cjk (or install via system font manager)
Images not appearingSet base_url to the directory containing the images
PDF is blankCheck that the Markdown file has content and is UTF-8 encoded
SyntaxError: invalid syntax on `str \None`Requires Python 3.10+. Use python3.10 or later

File Reference

FilePurpose
---------------
@assets/markdown.cssProfessional PDF stylesheet (A4, Chinese fonts, syntax highlighting, page layout)
@scripts/convert.pyCLI converter script with bundled CSS, title injection, and error handling

版本历史

共 1 个版本

  • v1.0.0 Initial release 当前
    2026-04-27 23:56 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

ai-intelligence

self-improving agent

pskoett
捕获经验教训、错误及修正内容,以实现持续改进。适用于以下场景:(1)命令或操作意外失败;(2)用户纠正Claude(如“不,那不对……”“实际上……”);(3)用户请求的功能不存在;(4)外部API或工具出现故障;(5)Claude发现自身
★ 4,065 📥 802,057
ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,364 📥 319,268
developer-tools

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 672 📥 324,669