AgentSkillsCN

md-to-pdf

将Markdown文件转换为带语法高亮与CSS支持的格式化PDF

SKILL.md
--- frontmatter
name: md-to-pdf
description: Convert Markdown files to formatted PDF with syntax highlighting and CSS support
license: MIT
compatibility: opencode
metadata:
  audience: developers
  workflow: documentation
  languages: "English, Multi-language"

What I do

  • Scan a specified directory for Markdown (.md) files
  • Convert each file to HTML using the Python markdown library
  • Render diagrams from code blocks (Mermaid, PlantUML, GraphViz, etc.) using Kroki.io
  • Apply CSS styling for better readability (GitHub-like style)
  • Generate a PDF for each Markdown file using weasyprint
  • Preserve code blocks with syntax highlighting
  • Support tables, images, and standard Markdown syntax

When to use me

Use this skill when you need to:

  • Batch convert documentation from Markdown to PDF
  • Create printable reports from Markdown notes
  • Render technical diagrams directly within your PDF documentation
  • Share documentation with non-technical users
  • Archive project documentation in a portable format

How to use me

Ask me to convert Markdown files in a folder:

code
Use the md-to-pdf skill to convert files in /path/to/docs

Or convert the current directory:

code
Use the md-to-pdf skill to convert files in the current directory

Supported Diagrams

I can automatically render diagrams defined in code blocks with the following languages:

  • mermaid
  • plantuml
  • graphviz / dot
  • ditaa
  • blockdiag, actdiag, nwdiag, packetdiag, rackdiag
  • c4plantuml
  • erd
  • nomnoml

Just use a fenced code block with the appropriate language identifier:

mermaid
graph TD;
    A-->B;
    A-->C;
    B-->D;
    C-->D;

What I need

  1. Target Directory (optional) - The folder containing .md files
    • Defaults to current directory if not specified
    • Must be a valid directory path
  2. Internet Connection - Required to fetch rendered diagrams from Kroki.io

What I provide

  1. Dependency setup - Automatically installs required packages:

    • markdown - Markdown to HTML conversion
    • weasyprint - HTML to PDF conversion
    • pygments - Syntax highlighting for code blocks
  2. Conversion process:

    • Reads each .md file
    • Converts Markdown to HTML with extensions (tables, fenced code blocks)
    • Detects and renders diagram code blocks
    • Applies a clean, readable CSS stylesheet
    • Generates a .pdf file with the same name in the same directory
  3. Output:

    • PDF files created alongside source Markdown files
    • Summary of converted files

Implementation approach

python
1. Validate input directory
2. Install/verify required dependencies (markdown, weasyprint, pygments)
3. Find all .md files in the directory
4. For each file:
   - Read Markdown content
   - Pre-process diagram blocks:
     - Detect diagram language (mermaid, plantuml, etc.)
     - Compress diagram source
     - Generate Kroki.io URL
     - Replace block with image tag
   - Convert to HTML using `markdown` lib with extensions
   - Add CSS for styling and syntax highlighting
   - Generate PDF using `weasyprint`
5. Report success/failure for each file