一个强大的MCP(模型上下文协议)服务器,用于从带有自定义样式的Markdown内容生成专业PDF,支持图像处理和多种格式选项。
无需安装!直接添加到您的Claude桌面配置中:
{
"mcpServers": {
"pdf-generator": {
"command": "npx",
"args": ["github:FabianGenell/pdf-mcp-server"]
}
}
}
就这样!服务器将被自动下载并运行。
# 克隆仓库
git clone https://github.com/FabianGenell/pdf-mcp-server
cd pdf-mcp-server
npm install
# 添加到Claude桌面配置
{
"mcpServers": {
"pdf-generator": {
"command": "node",
"args": ["/path/to/pdf-mcp-server/src/index.js"]
}
}
}
从Markdown内容生成PDF。PDF默认保存到您的下载文件夹。
await generate_pdf({
content: "# 我的文档\n\n这是**Markdown**内容。",
output_path: "output.pdf", // 保存到~/Downloads/output.pdf
options: {
theme: "professional",
include_toc: true,
page_numbers: true
}
});
注意:如果您仅提供文件名或相对路径,PDF将保存到您的下载文件夹。使用绝对路径以保存到其他位置。
处理并嵌入优化过的Markdown中的图像。支持本地路径、URL和base64数据URL。
本地文件(来自您的计算机):
await embed_images({
markdown_content: "# 报告\n\n[IMAGE_1]\n\n这里有一些文本。",
image_sources: [{
placeholder: "[IMAGE_1]",
source: "/Users/username/Pictures/screenshot.png", // 绝对路径
caption: "应用程序截图",
alignment: "center"
}]
});
远程URL:
await embed_images({
markdown_content: "# 报告\n\n[LOGO]\n\n公司概述。",
image_sources: [{
placeholder: "[LOGO]",
source: "https://example.com/images/logo.png", // 远程URL
caption: "公司Logo",
alignment: "center"
}]
});
Base64数据URL:
await embed_images({
markdown_content: "# 报告\n\n[CHART]\n\n数据分析。",
image_sources: [{
placeholder: "[CHART]",
source: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8/5+hHgAHggJ/PchI7wAAAABJRU5ErkJggg==", // Base64数据URL
caption: "生成图表",
alignment: "center"
}]
});
对于AI代理,您可以按以下三种方式提供图像:
/Users/username/Documents/image.png 或 C:\Users\username\Documents\image.pnghttps://example.com/image.pngdata:image/[type];base64,[data] 开头的base64数据URL所有类型的图像都会自动优化、缓存,并适当调整大小以提高PDF性能。图像会自动限制在页面高度的70%,以确保布局正确。
创建可复用的模板以保持一致的样式。
await create_styled_template({
template_name: "company-report",
css_content: "/* 自定义CSS */",
html_template: "<!-- 可选HTML结构 -->"
});
在生成PDF之前验证Markdown。
await validate_markdown({
content: "# 文档\n\n...",
check_images: true,
check_links: true
});
创建具有格式偏好和写作提示的自定义PDF样式。
await create_custom_style({
style_name: "年度报告",
description: "专业的年度报告样式",
prompt: "格式化为年度报告,包含执行摘要、指标和专业语气",
theme: "professional",
custom_css: "/* 额外样式 */",
format: "A4",
include_toc: true,
page_numbers: true
});
使用已保存的自定义样式生成PDF。PDF默认保存到您的下载文件夹。
await generate_pdf_with_style({
style_name: "年度报告",
content: "# 2024年度报告\n\n...",
output_path: "annual_report_2024.pdf" // 保存到~/Downloads/annual_report_2024.pdf
});
列出所有可用的自定义样式。
const { styles } = await list_custom_styles();
// 返回样式摘要数组,包括名称、描述和主题
干净易读的字体,适合一般文档。
企业风格,带信笺设计,适合商业报告。
无干扰设计,大量空白空间,适合技术文档。
高对比度主题,优化用于演示和屏幕查看。
PDF服务器现在支持保存结合了格式偏好、CSS样式和写作提示的自定义样式。这允许您创建一致的文档类型,如年度报告、技术文档或新闻通讯。
服务器包含几个示例样式:
自定义样式可以包括:
示例:
await create_custom_style({
style_name: "项目提案",
description: "专业的项目提案格式",
prompt: "格式化为项目提案,包含目标、时间表、预算部分",
theme: "professional",
custom_css: ".budget-table { ... }",
format: "Letter",
include_toc: true,
page_numbers: true,
header: "<div>项目提案</div>"
});
在Markdown中使用CSS类,通过markdown-it-attrs:
# 标题 {.center}
这是一个信息框 {.info-box}
{.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 - 图像样式{
// 页面设置
format: 'A4', // A4, Letter, Legal等
orientation: 'portrait', // 竖排或横排
margin: {
top: '1in',
right: '1in',
bottom: '1in',
left: '1in'
},
// 样式
theme: 'professional', // 主题名称
custom_css: '...', // 额外CSS
// 内容
include_toc: true, // 目录
toc_depth: 3, // 目录标题深度
page_numbers: true, // 显示页码
// 图像
image_quality: 85, // JPEG质量
max_image_width: 800, // 最大宽度(像素)
// 高级
wait_for_network: true, // 等待图像
print_background: true // 包括背景
}
// 首先,嵌入图像(支持本地文件、URL和base64)
const { processed_markdown } = await embed_images({
markdown_content: reportContent,
image_sources: [
{
placeholder: '[HERO_IMAGE]',
source: '/Users/username/Downloads/hero.png', // 本地文件
alignment: 'full-width'
},
{
placeholder: '[LOGO]',
source: 'https://company.com/logo.png', // 远程URL
alignment: 'center'
},
{
placeholder: '[CHART]',
source: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAB...', // Base64
caption: '销售数据',
alignment: 'center'
}
],
options: {
auto_optimize: true,
quality: 90
}
});
// 然后生成PDF
await generate_pdf({
content: processed_markdown,
output_path: 'report.pdf', // 保存到下载文件夹
options: {
theme: 'professional',
include_toc: true,
page_numbers: true,
header: '<div style="text-align: right">2024年第四季度报告</div>',
footer: '<div style="text-align: center">机密</div>'
}
});
await generate_pdf({
content: technicalDocs,
output_path: 'docs.pdf', // 保存到下载文件夹
options: {
theme: 'minimal',
include_toc: true,
toc_depth: 4,
enable_javascript: false
}
});
pdf-mcp-server/
├── src/
│ ├── index.js # MCP服务器入口点
│ ├── pdf-generator.js # 核心PDF生成
│ ├── markdown-processor.js # Markdown处理
│ ├── image-processor.js # 图像优化
│ ├── template-manager.js # 模板系统
│ └── themes/ # 内置主题
├── templates/ # 用户模板
├── temp/ # 临时文件
└── docs/ # 文档
ISC
欢迎提交拉取请求!请先阅读贡献指南。
对于问题和功能请求,请使用GitHub问题跟踪器。