pdf-generator

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

PDF 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

两种操作模式

  1. Template Mode: Modify existing PDF templates
    • Fill form fields (text, checkbox, dropdown)
    • Add overlays (text, images, shapes)
    • Merge and combine PDFs
  2. Scratch Mode: Create PDFs from nothing using JSON specifications
  1. 模板模式:修改现有PDF模板
    • 填写表单字段(文本、复选框、下拉菜单)
    • 添加覆盖层(文本、图片、形状)
    • 合并PDF文档
  2. 从头创建模式:使用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.json
Output (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.json
:
json
{
  "formFields": [
    { "name": "FullName", "value": "John Smith" },
    { "name": "Email", "value": "john@example.com" },
    { "name": "AgreeToTerms", "value": true }
  ],
  "flattenForm": true
}
创建
form-data.json
json
{
  "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.pdf
bash
deno run --allow-read --allow-write scripts/generate-from-template.ts \
  form-template.pdf form-data.json filled-form.pdf

Adding Overlays (Watermarks, Stamps)

添加覆盖层(水印、印章)

Create
watermark-spec.json
:
json
{
  "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.json
json
{
  "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.json
:
json
{
  "prependPdfs": [
    { "path": "cover-page.pdf" }
  ],
  "appendPdfs": [
    { "path": "appendix-a.pdf", "pages": [1, 2, 3] },
    { "path": "appendix-b.pdf" }
  ],
  "excludePages": [5, 6]
}
创建
merge-spec.json
json
{
  "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.json
:
json
{
  "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.json
json
{
  "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.pdf
bash
deno run --allow-read --allow-write scripts/generate-scratch.ts spec.json output.pdf

Examples

示例

Example 1: Fill Application Form

示例1:填写申请表单

Scenario: Automatically fill a job application form.
bash
undefined
场景:自动填写求职申请表单。
bash
undefined

1. 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
undefined
deno run --allow-read --allow-write scripts/generate-from-template.ts
application.pdf form-data.json john-smith-application.pdf
undefined

Example 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

脚本参考

ScriptPurposePermissions
analyze-template.ts
Extract text, metadata, form fields from PDF
--allow-read
generate-from-template.ts
Fill forms, add overlays, merge PDFs
--allow-read --allow-write
generate-scratch.ts
Create PDF from JSON specification
--allow-read --allow-write
脚本用途权限
analyze-template.ts
从PDF中提取文本、元数据和表单字段
--allow-read
generate-from-template.ts
填写表单、添加覆盖层、合并PDF
--allow-read --allow-write
generate-scratch.ts
根据JSON规范创建PDF
--allow-read --allow-write

Element Types (Scratch Mode)

元素类型(从头创建模式)

TypeDescriptionKey Options
text
Text content
x
,
y
,
text
,
fontSize
,
font
,
color
,
rotate
image
PNG/JPEG images
x
,
y
,
path
,
width
,
height
,
opacity
rectangle
Filled/outlined rectangles
x
,
y
,
width
,
height
,
color
,
borderColor
line
Straight lines
startX
,
startY
,
endX
,
endY
,
thickness
circle
Filled/outlined circles
x
,
y
,
radius
,
color
,
borderColor
table
Basic table layout
x
,
y
,
rows
,
columnWidths
,
rowHeight
类型描述关键选项
text
文本内容
x
,
y
,
text
,
fontSize
,
font
,
color
,
rotate
image
PNG/JPEG图片
x
,
y
,
path
,
width
,
height
,
opacity
rectangle
填充/描边矩形
x
,
y
,
width
,
height
,
color
,
borderColor
line
直线
startX
,
startY
,
endX
,
endY
,
thickness
circle
填充/描边圆形
x
,
y
,
radius
,
color
,
borderColor
table
基础表格布局
x
,
y
,
rows
,
columnWidths
,
rowHeight

Available Fonts

可用字体

  • Helvetica
    (default)
  • HelveticaBold
  • HelveticaOblique
  • TimesRoman
  • TimesBold
  • Courier
  • CourierBold
  • Helvetica
    (默认)
  • HelveticaBold
  • HelveticaOblique
  • TimesRoman
  • TimesBold
  • Courier
  • CourierBold

Page Sizes

页面尺寸

  • A4
    (595.28 x 841.89 points)
  • Letter
    (612 x 792 points)
  • Legal
    (612 x 1008 points)
  • Custom:
    [width, height]
    in points
  • A4
    (595.28 x 841.89 点)
  • Letter
    (612 x 792 点)
  • Legal
    (612 x 1008 点)
  • 自定义:
    [width, height]
    (单位:点)

Common Issues and Solutions

常见问题与解决方案

Issue: Form fields not found

问题:未找到表单字段

Symptoms: Error saying field name doesn't exist.
Solution:
  1. Run
    analyze-template.ts
    to get exact field names
  2. Field names are case-sensitive
  3. Some PDFs have non-fillable text that looks like form fields
症状:提示字段名称不存在的错误。
解决方案
  1. 运行
    analyze-template.ts
    获取准确的字段名称
  2. 字段名称区分大小写
  3. 部分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
    analyze-template.ts
    to see page dimensions
症状:文本显示在错误位置。
解决方案
  • PDF坐标从左下角(0,0)开始
  • Y轴向上递增,X轴向右递增
  • 使用
    analyze-template.ts
    查看页面尺寸

Issue: Images not appearing

问题:图片未显示

Symptoms: Image overlay not visible.
Solution:
  1. Check file path is relative to spec.json location
  2. Verify image is PNG or JPEG format
  3. Ensure coordinates are within page bounds
症状:图片覆盖层不可见。
解决方案
  1. 检查文件路径相对于spec.json的位置是否正确
  2. 验证图片为PNG或JPEG格式
  3. 确保坐标在页面范围内

Issue: Merged PDF has wrong page order

问题:合并后的PDF页面顺序错误

Symptoms: Pages appear in unexpected order.
Solution:
  • prependPdfs
    add pages before template
  • appendPdfs
    add pages after template
  • Use
    pages
    array to select specific 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电子表格