PDF文档生成服务

MCP-PDF是一个用于生成主题化PDF文档的服务,支持多种页面类型和丰富的自定义选项,适用于技术报告、文档和演示文稿的生成。

  • by alexh-scrt
  • Python
  • 本地部署
  • 1 星标
  • 更新于
在 GitHub 查看 alexh-scrt/mcp-pdf

使用说明

MCP-PDF

MCP服务器,用于生成具有各种页面类型的主题PDF文档。

特性

  • 多种页面类型:标题、目录、章节、内容、代码、图表、图像, 美人鱼、摘要、参考文献
  • 美人鱼图支持:使用mmdc自动将Mermaid代码转换为PNG图像
  • 主题定制:自定义颜色、字体、排版和间距
  • 默认秘密AI主题:专业红/蓝配色方案
  • 丰富的内容:支持文本、项目符号、图像、表格和代码块
  • 语法高亮显示:带有可选行号的代码页
  • 图像URL支持:从URL下载并嵌入图像
  • 智能目录回退:当请求的路径不可访问时,自动使用配置的回退目录

安装

# Install dependencies
pip install -r requirements.txt

# Or install in development mode
pip install -e .

用法

运行服务器

python run_server.py

生成PDF

服务器暴露了一个 generate_pdf 接受文档规范的工具:

{
  "document_spec": {
    "title": "My Technical Report",
    "theme": {},
    "pages": [
      {
        "page_type": "title",
        "title": "Technical Report",
        "subtitle": "A Comprehensive Guide",
        "author": "John Doe",
        "date": "November 2024"
      },
      {
        "page_type": "content",
        "title": "Introduction",
        "content": [
          {"type": "text", "text": "This document..."},
          {"type": "bullet", "items": ["Point 1", "Point 2"]}
        ]
      }
    ],
    "output": {
      "filename": "report.pdf",
      "directory": "./output"
    }
  }
}

页面类型

1.标题页

  • 领域:标题、副标题、作者、日期、附加信息
  • 使用:文件封面

2.目录(TOC)

  • 领域:标题、条目(列表)
  • 使用:文档导航

3.章节页

  • 领域:标题、副标题
  • 使用:分段分隔器

4.内容页

  • 领域:标题、内容(项目列表)
  • 内容类型:文本、项目符号、图像、代码、表格
  • 使用:主要内容页面

5.代码页

  • 领域:标题、代码、语言、行号
  • 使用:带有语法高亮显示的代码示例

6.图表页面

  • 领域:title、diagram_path/diagram_url、标题、描述
  • 使用:附有说明的技术图

7.图片页面

  • 领域:title、image_path/image_url、标题、描述
  • 使用:带字幕的图像

8.美人鱼图页面

  • 领域:标题、商品代码、标题、描述
  • 使用:美人鱼图自动转换为PNG图像
  • 需求:Node.js和npm(mmdc通过npx安装)

9.摘要页

  • 领域:标题、关键点(列表)、结论
  • 使用:关键要点和结论

10.参考资料页

  • 领域:标题、参考文献(列表)、样式(编号/项目符号/纯)
  • 使用:引用和参考文献

主题定制

{
  "theme": {
    "colors": {
      "primary": "#E3342F",
      "secondary": "#1CCBD0",
      "accent": "#F59E0B",
      "background": "#FFFFFF",
      "text": "#111827",
      "code_bg": "#F5F5F5"
    },
    "fonts": {
      "heading": "Helvetica-Bold",
      "body": "Helvetica",
      "code": "Courier"
    },
    "title_font_size": 24,
    "h1_font_size": 18,
    "body_font_size": 10,
    "code_font_size": 9
  }
}

发展

项目结构

mcp-pdf/
├── src/
│   └── mcp_pdf/
│       ├── __init__.py
│       ├── __main__.py
│       ├── server.py           # MCP server implementation
│       ├── models/
│       │   ├── __init__.py
│       │   ├── theme_spec.py   # Theme models
│       │   └── document_spec.py # Document models
│       └── rendering/
│           ├── __init__.py
│           └── pdf_generator.py # PDF generation logic
├── run_server.py               # Server entry point
├── pyproject.toml
├── requirements.txt
└── README.md

运行测试

# Run all tests with pytest
pytest tests/ -v

# Run specific test file
pytest tests/test_models.py -v

# Run the example PDF generation test
python tests/test_pdf_generation.py

许可证

MIT许可证