PDF生成服务

一款强大的MCP服务器,用于从Markdown内容生成具有自定义样式、图像支持和多种格式化选项的专业PDF文件。

  • by FabianGenell
  • JavaScript
  • 本地部署
  • 2 星标
  • 更新于
在 GitHub 查看 fabiangenell/pdf-mcp-server

使用说明

PDF生成器MCP服务器

一个强大的MCP(模型上下文协议)服务器,用于从带有自定义样式、图像支持和各种格式选项的markdown内容生成专业PDF。

特性

  • 🎨 多个内置主题:默认、专业、极简和深色主题
  • 📸 智能图像处理:自动优化、缓存和嵌入
  • 📄 高级PDF选项:页眉、页脚、页码、TOC生成
  • 🎯 模板系统:创建和重用自定义模板
  • ✅ Markdown验证:生成前检查内容
  • 🚀 性能优化:浏览器实例重用、图像缓存
  • 💼 自定义样式:保存和重用带有提示和格式的自定义PDF样式

安装

GitHub上的npx快速入门(推荐)

无需安装!直接添加到您的Claude桌面配置中:

{
  "mcpServers": {
    "pdf-generator": {
      "command": "npx",
      "args": ["github:FabianGenell/pdf-mcp-server"]
    }
  }
}

就是这样!服务器将自动下载并运行。

备选方案:地方发展

# Clone the repository
git clone https://github.com/FabianGenell/pdf-mcp-server
cd pdf-mcp-server
npm install

# Add to Claude desktop configuration
{
  "mcpServers": {
    "pdf-generator": {
      "command": "node",
      "args": ["/path/to/pdf-mcp-server/src/index.js"]
    }
  }
}

可用工具

1.generate.pdf

从markdown内容生成PDF。 默认情况下,PDF会保存到您的下载文件夹中.

await generate_pdf({
  content: "# My Document\n\nThis is **markdown** content.",
  output_path: "output.pdf",  // Saves to ~/Downloads/output.pdf
  options: {
    theme: "professional",
    include_toc: true,
    page_numbers: true
  }
});

备注:如果您只提供文件名或相对路径,PDF将保存到您的下载文件夹中。使用绝对路径保存到其他位置。

2.嵌入图片

通过优化在markdown中处理和嵌入图像。 支持本地路径、URL和base64数据URL.

图像源类型

本地文件(来自您的计算机):

await embed_images({
  markdown_content: "# Report\n\n[IMAGE_1]\n\nSome text here.",
  image_sources: [{
    placeholder: "[IMAGE_1]",
    source: "/Users/username/Pictures/screenshot.png",  // Absolute path
    caption: "Application Screenshot",
    alignment: "center"
  }]
});

远程URL:

await embed_images({
  markdown_content: "# Report\n\n[LOGO]\n\nCompany overview.",
  image_sources: [{
    placeholder: "[LOGO]",
    source: "https://example.com/images/logo.png",  // Remote URL
    caption: "Company Logo",
    alignment: "center"
  }]
});

Base64数据URL:

await embed_images({
  markdown_content: "# Report\n\n[CHART]\n\nData analysis.",
  image_sources: [{
    placeholder: "[CHART]",
    source: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==",  // Base64 data URL
    caption: "Generated Chart",
    alignment: "center"
  }]
});

AI代理指南

对于AI代理,您可以通过三种方式提供图像:

  1. 本地计算机路径:使用绝对路径,如 /Users/username/Documents/image.png 或 C:\Users\username\Documents\image.png
  2. 在线图片:使用直接URL,如 https://example.com/image.png
  3. 生成/上传的图像:使用以开头的base64数据URL data:image/[type];base64,[data]

所有图像类型都会自动优化、缓存和调整大小,以适应PDF性能。图像自动限制为页面高度的70%,以确保布局正确。

3.创建样式模板

创建可重复使用的模板以实现一致的样式。

await create_styled_template({
  template_name: "company-report",
  css_content: "/* Custom CSS */",
  html_template: "<!-- Optional HTML structure -->"
});

4.validate_markdown

在生成PDF之前验证标记。

await validate_markdown({
  content: "# Document\n\n...",
  check_images: true,
  check_links: true
});

5.创建_自定义_样式

使用格式首选项和书写提示创建自定义PDF样式。

await create_custom_style({
  style_name: "annual_report",
  description: "Professional annual report style",
  prompt: "Format as an annual report with executive summary, metrics, and professional tone",
  theme: "professional",
  custom_css: "/* Additional styling */",
  format: "A4",
  include_toc: true,
  page_numbers: true
});

6.generate_pdf_with_style

使用保存的自定义样式生成PDF。 默认情况下,PDF会保存到您的下载文件夹中.

await generate_pdf_with_style({
  style_name: "annual_report",
  content: "# Annual Report 2024\n\n...",
  output_path: "annual_report_2024.pdf"  // Saves to ~/Downloads/annual_report_2024.pdf
});

7.列表_自定义_样式

列出所有可用的自定义样式。

const { styles } = await list_custom_styles();
// Returns array of style summaries with names, descriptions, and themes

主题

默认

干净易读的排版非常适合一般文档。

专业的

带有信头设计的企业风格,是商业报告的理想选择。

最小化

无干扰的设计,技术文档有充足的空白。

黑暗

针对演示文稿和屏幕查看优化的高对比度主题。

自定义样式

PDF服务器现在支持保存结合了格式首选项、CSS样式和书写提示的自定义样式。这允许您创建一致的文档类型,如年度报告、技术文档或新闻通讯。

预构建自定义样式

服务器包括几个示例样式:

  • 年度报告:采用行政格式的专业年度报告
  • 技术文档:用代码高亮显示干净的技术文档
  • 演示文稿_幻灯片:演示文稿的幻灯片样式格式
  • 研究_作者:学术论文格式
  • 通讯:带有专栏的现代通讯

创建自定义样式

自定义样式可以包括:

  • 书写提示:内容格式说明
  • 主题选择:基础主题
  • 自定义CSS:附加样式规则
  • 页面设置:格式、方向、边距
  • 特性:目录、页码、页眉/页脚

例子:

await create_custom_style({
  style_name: "project_proposal",
  description: "Professional project proposal format",
  prompt: "Format as a project proposal with objectives, timeline, budget sections",
  theme: "professional",
  custom_css: ".budget-table { ... }",
  format: "Letter",
  include_toc: true,
  page_numbers: true,
  header: "<div>Project Proposal</div>"
});

自定义样式

在markdown中使用CSS类,并使用markdown-it-attrs:

# Title {.center}

This is an info box {.info-box}

![Image](path.jpg){.img-shadow .img-rounded}

可用的CSS类:

  • .page-break -强制分页符
  • .no-break -防止分页符
  • .center, .right -文本对齐方式
  • .info-box, .warning-box, .error-box, .success-box -标注框
  • .img-shadow, .img-rounded, .img-border -图像样式

PDF选项

{
  // Page settings
  format: 'A4',              // A4, Letter, Legal, etc.
  orientation: 'portrait',   // portrait or landscape
  margin: {
    top: '1in',
    right: '1in',
    bottom: '1in',
    left: '1in'
  },
  
  // Styling
  theme: 'professional',     // Theme name
  custom_css: '...',        // Additional CSS
  
  // Content
  include_toc: true,        // Table of contents
  toc_depth: 3,            // TOC heading depth
  page_numbers: true,       // Show page numbers
  
  // Images
  image_quality: 85,        // JPEG quality
  max_image_width: 800,     // Max width in pixels
  
  // Advanced
  wait_for_network: true,   // Wait for images
  print_background: true    // Include backgrounds
}

示例用法

专业报告

// First, embed images (supports local files, URLs, and base64)
const { processed_markdown } = await embed_images({
  markdown_content: reportContent,
  image_sources: [
    {
      placeholder: '[HERO_IMAGE]',
      source: '/Users/username/Downloads/hero.png',  // Local file
      alignment: 'full-width'
    },
    {
      placeholder: '[LOGO]',
      source: 'https://company.com/logo.png',  // Remote URL
      alignment: 'center'
    },
    {
      placeholder: '[CHART]',
      source: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB...',  // Base64
      caption: 'Sales Data',
      alignment: 'center'
    }
  ],
  options: {
    auto_optimize: true,
    quality: 90
  }
});

// Then generate PDF
await generate_pdf({
  content: processed_markdown,
  output_path: 'report.pdf',  // Saves to Downloads
  options: {
    theme: 'professional',
    include_toc: true,
    page_numbers: true,
    header: '<div style="text-align: right">Q4 2024 Report</div>',
    footer: '<div style="text-align: center">Confidential</div>'
  }
});

技术文档

await generate_pdf({
  content: technicalDocs,
  output_path: 'docs.pdf',  // Saves to Downloads
  options: {
    theme: 'minimal',
    include_toc: true,
    toc_depth: 4,
    enable_javascript: false
  }
});

文件结构

pdf-mcp-server/
├── src/
│   ├── index.js              # MCP server entry point
│   ├── pdf-generator.js      # Core PDF generation
│   ├── markdown-processor.js # Markdown processing
│   ├── image-processor.js    # Image optimization
│   ├── template-manager.js   # Template system
│   └── themes/              # Built-in themes
├── templates/               # User templates
├── temp/                   # Temporary files
└── docs/                   # Documentation

需求

  • Node.js 18+
  • Playwright浏览器(自动安装)

许可证

国际协调委员会

贡献

欢迎拉取请求!请先阅读投稿指南。

支持

对于问题和功能请求,请使用GitHub问题跟踪器。