pdf-generator
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePDF Generator
PDF生成工具
When to Use This Skill
何时使用该技能
Use this skill when:
- Creating PDF documents programmatically from data or specifications
- Filling PDF forms with dynamic data
- Adding watermarks, stamps, or overlays to existing PDFs
- Extracting text and metadata from PDF files
- Merging multiple PDFs into one document
- Analyzing PDF structure and form fields
Do NOT use this skill when:
- User wants to open/view PDFs (use native PDF viewer)
- Complex page layout with flowing text is needed (consider HTML-to-PDF tools)
- Working with password-protected PDFs (limited support)
- OCR is needed for scanned documents
在以下场景使用该技能:
- 根据数据或规范以编程方式创建PDF文档
- 使用动态数据填写PDF表单
- 为现有PDF添加水印、印章或覆盖层
- 从PDF文件中提取文本和元数据
- 将多个PDF合并为一个文档
- 分析PDF结构和表单字段
请勿在以下场景使用该技能:
- 用户想要打开/查看PDF(使用原生PDF查看器)
- 需要带有流式文本的复杂页面布局(考虑使用HTML转PDF工具)
- 处理受密码保护的PDF(支持有限)
- 需要对扫描文档进行OCR识别
Prerequisites
前置条件
- Deno installed (https://deno.land/)
- Input PDF files for template-based operations
- JSON specification for scratch generation
- 已安装Deno(https://deno.land/)
- 用于模板化操作的输入PDF文件
- 用于从头创建的JSON规范
Quick Start
快速开始
Two Modes of Operation
两种操作模式
-
Template Mode: Modify existing PDF templates
- Fill form fields (text, checkbox, dropdown)
- Add overlays (text, images, shapes)
- Merge and combine PDFs
-
Scratch Mode: Create PDFs from nothing using JSON specifications
-
模板模式:修改现有PDF模板
- 填写表单字段(文本、复选框、下拉菜单)
- 添加覆盖层(文本、图片、形状)
- 合并PDF文档
-
从头创建模式:使用JSON规范从零开始创建PDF
Instructions
操作指南
Mode 1: Template-Based Generation
模式1:基于模板的生成
Step 1a: Analyze the Template
步骤1a:分析模板
Extract form fields and structure from an existing PDF:
bash
deno run --allow-read scripts/analyze-template.ts form-template.pdf > inventory.jsonOutput (inventory.json):
json
{
"filename": "form-template.pdf",
"pageCount": 2,
"title": "Application Form",
"author": "Company Inc",
"pages": [
{ "pageNumber": 1, "width": 612, "height": 792, "text": "..." }
],
"formFields": [
{ "name": "FullName", "type": "text", "value": "" },
{ "name": "Email", "type": "text", "value": "" },
{ "name": "AgreeToTerms", "type": "checkbox", "value": false }
],
"placeholders": [
{ "tag": "{{DATE}}", "location": "page 1", "pageNumber": 1 }
],
"hasFormFields": true
}从现有PDF中提取表单字段和结构:
bash
deno run --allow-read scripts/analyze-template.ts form-template.pdf > inventory.json输出(inventory.json):
json
{
"filename": "form-template.pdf",
"pageCount": 2,
"title": "Application Form",
"author": "Company Inc",
"pages": [
{ "pageNumber": 1, "width": 612, "height": 792, "text": "..." }
],
"formFields": [
{ "name": "FullName", "type": "text", "value": "" },
{ "name": "Email", "type": "text", "value": "" },
{ "name": "AgreeToTerms", "type": "checkbox", "value": false }
],
"placeholders": [
{ "tag": "{{DATE}}", "location": "page 1", "pageNumber": 1 }
],
"hasFormFields": true
}Step 1b: Create Fill Specification
步骤1b:创建填写规范
Create :
form-data.jsonjson
{
"formFields": [
{ "name": "FullName", "value": "John Smith" },
{ "name": "Email", "value": "john@example.com" },
{ "name": "AgreeToTerms", "value": true }
],
"flattenForm": true
}创建:
form-data.jsonjson
{
"formFields": [
{ "name": "FullName", "value": "John Smith" },
{ "name": "Email", "value": "john@example.com" },
{ "name": "AgreeToTerms", "value": true }
],
"flattenForm": true
}Step 1c: Generate Filled PDF
步骤1c:生成已填写的PDF
bash
deno run --allow-read --allow-write scripts/generate-from-template.ts \
form-template.pdf form-data.json filled-form.pdfbash
deno run --allow-read --allow-write scripts/generate-from-template.ts \
form-template.pdf form-data.json filled-form.pdfAdding Overlays (Watermarks, Stamps)
添加覆盖层(水印、印章)
Create :
watermark-spec.jsonjson
{
"overlays": [
{
"type": "text",
"page": 1,
"x": 200,
"y": 400,
"text": "CONFIDENTIAL",
"fontSize": 48,
"color": { "r": 1, "g": 0, "b": 0 },
"rotate": 45
},
{
"type": "image",
"page": 1,
"x": 450,
"y": 700,
"path": "logo.png",
"width": 100,
"height": 50
}
]
}创建:
watermark-spec.jsonjson
{
"overlays": [
{
"type": "text",
"page": 1,
"x": 200,
"y": 400,
"text": "CONFIDENTIAL",
"fontSize": 48,
"color": { "r": 1, "g": 0, "b": 0 },
"rotate": 45
},
{
"type": "image",
"page": 1,
"x": 450,
"y": 700,
"path": "logo.png",
"width": 100,
"height": 50
}
]
}Merging PDFs
合并PDF
Create :
merge-spec.jsonjson
{
"prependPdfs": [
{ "path": "cover-page.pdf" }
],
"appendPdfs": [
{ "path": "appendix-a.pdf", "pages": [1, 2, 3] },
{ "path": "appendix-b.pdf" }
],
"excludePages": [5, 6]
}创建:
merge-spec.jsonjson
{
"prependPdfs": [
{ "path": "cover-page.pdf" }
],
"appendPdfs": [
{ "path": "appendix-a.pdf", "pages": [1, 2, 3] },
{ "path": "appendix-b.pdf" }
],
"excludePages": [5, 6]
}Mode 2: From-Scratch Generation
模式2:从头创建
Step 2a: Create Specification
步骤2a:创建规范
Create :
spec.jsonjson
{
"title": "Quarterly Report",
"author": "Finance Team",
"pages": [
{
"size": "A4",
"elements": [
{
"type": "text",
"x": 50,
"y": 750,
"text": "Q4 2024 Financial Report",
"fontSize": 28,
"font": "HelveticaBold",
"color": { "r": 0, "g": 0, "b": 0.5 }
},
{
"type": "line",
"startX": 50,
"startY": 740,
"endX": 550,
"endY": 740,
"thickness": 2
},
{
"type": "text",
"x": 50,
"y": 700,
"text": "Executive Summary",
"fontSize": 18,
"font": "HelveticaBold"
},
{
"type": "text",
"x": 50,
"y": 670,
"text": "This quarter showed strong growth across all divisions...",
"fontSize": 12,
"maxWidth": 500,
"lineHeight": 16
}
]
}
]
}创建:
spec.jsonjson
{
"title": "Quarterly Report",
"author": "Finance Team",
"pages": [
{
"size": "A4",
"elements": [
{
"type": "text",
"x": 50,
"y": 750,
"text": "Q4 2024 Financial Report",
"fontSize": 28,
"font": "HelveticaBold",
"color": { "r": 0, "g": 0, "b": 0.5 }
},
{
"type": "line",
"startX": 50,
"startY": 740,
"endX": 550,
"endY": 740,
"thickness": 2
},
{
"type": "text",
"x": 50,
"y": 700,
"text": "Executive Summary",
"fontSize": 18,
"font": "HelveticaBold"
},
{
"type": "text",
"x": 50,
"y": 670,
"text": "This quarter showed strong growth across all divisions...",
"fontSize": 12,
"maxWidth": 500,
"lineHeight": 16
}
]
}
]
}Step 2b: Generate PDF
步骤2b:生成PDF
bash
deno run --allow-read --allow-write scripts/generate-scratch.ts spec.json output.pdfbash
deno run --allow-read --allow-write scripts/generate-scratch.ts spec.json output.pdfExamples
示例
Example 1: Fill Application Form
示例1:填写申请表单
Scenario: Automatically fill a job application form.
bash
undefined场景:自动填写求职申请表单。
bash
undefined1. Analyze form to find field names
1. 分析表单以获取字段名称
deno run --allow-read scripts/analyze-template.ts application.pdf --pretty
deno run --allow-read scripts/analyze-template.ts application.pdf --pretty
2. Create form-data.json with applicant info
2. 使用申请人信息创建form-data.json
3. Generate filled form
3. 生成已填写的表单
deno run --allow-read --allow-write scripts/generate-from-template.ts
application.pdf form-data.json john-smith-application.pdf
application.pdf form-data.json john-smith-application.pdf
undefineddeno run --allow-read --allow-write scripts/generate-from-template.ts
application.pdf form-data.json john-smith-application.pdf
application.pdf form-data.json john-smith-application.pdf
undefinedExample 2: Add Approval Stamp
示例2:添加审批印章
Scenario: Add an "APPROVED" stamp to a document.
stamp-spec.json:
json
{
"overlays": [
{
"type": "rectangle",
"page": 1,
"x": 400,
"y": 700,
"width": 150,
"height": 50,
"color": { "r": 0.9, "g": 1, "b": 0.9 }
},
{
"type": "text",
"page": 1,
"x": 410,
"y": 720,
"text": "APPROVED",
"fontSize": 20,
"font": "HelveticaBold",
"color": { "r": 0, "g": 0.5, "b": 0 }
},
{
"type": "text",
"page": 1,
"x": 410,
"y": 705,
"text": "2024-12-15",
"fontSize": 10
}
]
}场景:为文档添加“APPROVED”印章。
stamp-spec.json:
json
{
"overlays": [
{
"type": "rectangle",
"page": 1,
"x": 400,
"y": 700,
"width": 150,
"height": 50,
"color": { "r": 0.9, "g": 1, "b": 0.9 }
},
{
"type": "text",
"page": 1,
"x": 410,
"y": 720,
"text": "APPROVED",
"fontSize": 20,
"font": "HelveticaBold",
"color": { "r": 0, "g": 0.5, "b": 0 }
},
{
"type": "text",
"page": 1,
"x": 410,
"y": 705,
"text": "2024-12-15",
"fontSize": 10
}
]
}Example 3: Create Report with Table
示例3:创建带表格的报告
Scenario: Generate a simple report with a data table.
report-spec.json:
json
{
"title": "Sales Report",
"pages": [{
"size": "Letter",
"elements": [
{
"type": "text",
"x": 72,
"y": 720,
"text": "Monthly Sales Report",
"fontSize": 24,
"font": "HelveticaBold"
},
{
"type": "table",
"x": 72,
"y": 680,
"rows": [
["Product", "Units", "Revenue"],
["Widget A", "150", "$15,000"],
["Widget B", "75", "$11,250"],
["Widget C", "200", "$8,000"]
],
"columnWidths": [150, 80, 100],
"rowHeight": 25,
"headerBackground": { "r": 0.9, "g": 0.9, "b": 0.9 }
}
]
}]
}场景:生成包含数据表格的简单报告。
report-spec.json:
json
{
"title": "Sales Report",
"pages": [{
"size": "Letter",
"elements": [
{
"type": "text",
"x": 72,
"y": 720,
"text": "Monthly Sales Report",
"fontSize": 24,
"font": "HelveticaBold"
},
{
"type": "table",
"x": 72,
"y": 680,
"rows": [
["Product", "Units", "Revenue"],
["Widget A", "150", "$15,000"],
["Widget B", "75", "$11,250"],
["Widget C", "200", "$8,000"]
],
"columnWidths": [150, 80, 100],
"rowHeight": 25,
"headerBackground": { "r": 0.9, "g": 0.9, "b": 0.9 }
}
]
}]
}Script Reference
脚本参考
| Script | Purpose | Permissions |
|---|---|---|
| Extract text, metadata, form fields from PDF | |
| Fill forms, add overlays, merge PDFs | |
| Create PDF from JSON specification | |
| 脚本 | 用途 | 权限 |
|---|---|---|
| 从PDF中提取文本、元数据和表单字段 | |
| 填写表单、添加覆盖层、合并PDF | |
| 根据JSON规范创建PDF | |
Element Types (Scratch Mode)
元素类型(从头创建模式)
| Type | Description | Key Options |
|---|---|---|
| Text content | |
| PNG/JPEG images | |
| Filled/outlined rectangles | |
| Straight lines | |
| Filled/outlined circles | |
| Basic table layout | |
| 类型 | 描述 | 关键选项 |
|---|---|---|
| 文本内容 | |
| PNG/JPEG图片 | |
| 填充/描边矩形 | |
| 直线 | |
| 填充/描边圆形 | |
| 基础表格布局 | |
Available Fonts
可用字体
- (default)
Helvetica HelveticaBoldHelveticaObliqueTimesRomanTimesBoldCourierCourierBold
- (默认)
Helvetica HelveticaBoldHelveticaObliqueTimesRomanTimesBoldCourierCourierBold
Page Sizes
页面尺寸
- (595.28 x 841.89 points)
A4 - (612 x 792 points)
Letter - (612 x 1008 points)
Legal - Custom: in points
[width, height]
- (595.28 x 841.89 点)
A4 - (612 x 792 点)
Letter - (612 x 1008 点)
Legal - 自定义:(单位:点)
[width, height]
Common Issues and Solutions
常见问题与解决方案
Issue: Form fields not found
问题:未找到表单字段
Symptoms: Error saying field name doesn't exist.
Solution:
- Run to get exact field names
analyze-template.ts - Field names are case-sensitive
- Some PDFs have non-fillable text that looks like form fields
症状:提示字段名称不存在的错误。
解决方案:
- 运行获取准确的字段名称
analyze-template.ts - 字段名称区分大小写
- 部分PDF中存在看似表单字段的不可填写文本
Issue: Text positioning is off
问题:文本位置错误
Symptoms: Text appears in wrong location.
Solution:
- PDF coordinates start from bottom-left (0,0)
- Y increases upward, X increases rightward
- Use to see page dimensions
analyze-template.ts
症状:文本显示在错误位置。
解决方案:
- PDF坐标从左下角(0,0)开始
- Y轴向上递增,X轴向右递增
- 使用查看页面尺寸
analyze-template.ts
Issue: Images not appearing
问题:图片未显示
Symptoms: Image overlay not visible.
Solution:
- Check file path is relative to spec.json location
- Verify image is PNG or JPEG format
- Ensure coordinates are within page bounds
症状:图片覆盖层不可见。
解决方案:
- 检查文件路径相对于spec.json的位置是否正确
- 验证图片为PNG或JPEG格式
- 确保坐标在页面范围内
Issue: Merged PDF has wrong page order
问题:合并后的PDF页面顺序错误
Symptoms: Pages appear in unexpected order.
Solution:
- add pages before template
prependPdfs - add pages after template
appendPdfs - Use array to select specific pages:
pages[1, 3, 5]
症状:页面显示顺序不符合预期。
解决方案:
- 在模板前添加页面
prependPdfs - 在模板后添加页面
appendPdfs - 使用数组选择特定页面:
pages[1, 3, 5]
Limitations
局限性
- No built-in table layout: Tables require manual column width specification
- Standard fonts only: Custom font embedding not supported in scratch mode
- No flowing text: Text doesn't automatically wrap to next page
- Limited form field creation: Can fill existing forms, not create new fields
- No encryption: Cannot create password-protected PDFs
- Basic graphics: No gradients, patterns, or complex paths
- Text extraction: May not work perfectly on all PDFs (depends on PDF structure)
- 无内置表格布局:表格需要手动指定列宽
- 仅支持标准字体:从头创建模式不支持自定义字体嵌入
- 无流式文本:文本不会自动换行到下一页
- 表单字段创建有限:只能填写现有表单,无法创建新字段
- 无加密功能:无法创建受密码保护的PDF
- 基础图形:不支持渐变、图案或复杂路径
- 文本提取:可能无法在所有PDF上完美工作(取决于PDF结构)
Related Skills
相关技能
- pptx-generator: For creating PowerPoint presentations
- docx-generator: For creating Word documents
- xlsx-generator: For creating Excel spreadsheets
- pptx-generator:用于创建PowerPoint演示文稿
- docx-generator:用于创建Word文档
- xlsx-generator:用于创建Excel电子表格