跳到主要内容

准备文档解析信息

本指南涵盖的内容

准备解析信息 是仪表板设置,它将原始数据转换为原始数据。 PDF 进入 自动化 PDF 数据提取您可以定义捕获键,在示例文档上绘制区域,并使用以下任一方式为每个键分配提取规则: 正则表达式 对于稳定模式或 JavaScript 表达 用于条件逻辑。保存后,同一个模板将从此处运行。 REST APIMakeZapierPower Automate, 和 n8n 通过提及它 模板 ID

相关博客文章
目前尚无关于此功能的博客文章——敬请期待。
在此期间,您可以浏览 PDF4me 博客,查看适用于各平台的教程和工作流程。
访问博客

验证您的设置

解析模板的创建发生在 PDF4me 开发者控制面板。使用您的帐户登录,然后创建或复制一个 API 解析文档的键 API 使用您在此处创建的模板的调用。

您不容错过的重要事实

首先是正则表达式, JavaScript 仅在需要时
使用 正则表达式 适用于任何具有稳定形状的字段(发票编号、日期、总额、税号)。使用 JavaScript 表达 仅当需要条件逻辑、多规则分支或文档分类时才需要使用。在同一个模板中混合使用这两种方法也是可以的,并且推荐这样做。
每个稳定布局系列一个模板
单个模板可以处理字体更改或位置偏移等细微差别。对于真正不同的布局(例如,两个供应商的发票设计不同),应创建单独的模板,并在调用“解析文档”之前,根据分类器或源系统将文件路由到正确的模板。
拯救 TemplateId不是名字
保存更改后,仪表板会生成一个 GUID。 模板 ID 用于模板。始终传递 模板 ID 生产中 API 电话。 模板名称 这样做也行,但是重命名模板会破坏任何通过名称引用它的自动化操作。

步骤 1:创建解析模板

  1. 打开 解析文档仪表板
  2. 点击 添加 并输入一个清晰的模板名称(例如, invoice 或者 vendor-statement)。
  3. 点击 节省 创建空白模板。
  4. 打开模板 编辑 进入配置捕获密钥的模式。
开发者控制面板中的 PDF4me 文档解析模板列表,以及用于创建新解析模板的“添加”按钮,方便用户自动提取 PDF 数据。

步骤 2:上传样本并配置捕获密钥

仪表盘会显示已上传的内容。 PDF 右侧显示了 解析信息 左侧是表格。使用真实的生产环境样本,而不是合成测试文件,这样捕获区域才能与实际交通中的情况相匹配。

  1. 点击 上传模板文件 并挑选一个有代表性的样本(发票、合同、表格)。
  2. 在下面 钥匙, 点击 + 添加捕获键。请用驼峰命名法给它命名: invoiceNumbercustomerNametotalAmount
  3. 选择 选择表达式类型 关键在于: Javascript Expression 或者 Regex Expression
  4. 将表达式正文粘贴到出现的字段中。
  5. all 扫描每一页,或扫描到特定的页码(12等等)以限制范围。
  6. 切换 搜索整页 当该值不在固定位置时启用。禁用时,仅搜索绘制的捕获区域。
PDF4me 文档解析配置界面。左侧面板显示解析信息,包括模板名称“发票”、解析 ID 以及一个键名,该键名包含“选择表达式类型”(JavaScript 表达式)、“JavaScript 表达式正文”以及“全部页面”和“搜索整页”切换开关。右侧面板显示已上传的发票 PDF 文件,顶部按从左到右的执行顺序排列着“上传模板文件”、“测试解析”和“保存更改”操作按钮。

解析文档设置界面。左图:包含键和表达式类型的解析信息表单。右图:已上传的示例 PDF操作按钮从左到右依次为:上传模板文件、测试解析、保存更改。

步骤 3:为每个键选择表达式类型

表达式类型最适合典型用途复杂
Regex ExpressionFixed text patternsInvoice number, dates, totals, tax IDs, postal codesLow
JavaScript ExpressionConditional and multi-rule extractionDocument classification, fallback logic, rule orchestrationMedium to High

正则表达式模式(正则表达式 PDF 解析器基础)

使用 正则表达式 当场地形状稳定且可预测时,扫描选定区域以进行第一场比赛。

INV-\d{6,10}
\d{2}/\d{2}/\d{4}
\$?\d{1,3}(?:,\d{3})*(?:\.\d{2})?

发票的常用键映射:

  • invoiceNumberINV-\d{6,10}
  • invoiceDate\d{2}/\d{2}/\d{4}
  • totalAmount\$?\d{1,3}(?:,\d{3})*(?:\.\d{2})?

这些相同的模式也适用于供应商对账单、采购订单和运输单据中的相同字段,因为发票号码、日期和金额在大多数商业文件中都具有相同的形式。

JavaScript 条件逻辑表达式

使用 JavaScript 表达 当输出取决于文档中多个标记的存在或组合时。 PDF4me 将提取的文本作为变量传递给你的函数。 text你的函数会评估文本并返回一个字符串,该字符串将成为键的值。

示例 1:按内容标记分类

通过检查出现的标记短语来区分条款和条件文件与订单文件:

function functionFormatTextDate1(text) {
console.log("Hello");
var term = [...text.matchAll(/General Terms and Conditions/gi)];
var order = [...text.matchAll(/your ordernumber/gi)];

if (term.length) {
if (order.length) {
return "Order document";
} else {
return "Terms and Conditions";
}
} else {
return "Not Terms and Conditions";
}
}

return functionFormatTextDate1(text);

示例 2:发票与订单的区分

一个简短的分类器,根据文本中是否存在特定词语来判断文档是发票还是订单。 invoiceordernumber

function functionGetInvoiceOrder(text) {
// You get all PDF text in `text`
var invoice = [...text.matchAll(/invoice/gi)];
var order = [...text.matchAll(/ordernumber/gi)];

if (invoice.length) {
if (order.length) {
return "Order document";
} else {
return "invoice";
}
} else {
return "";
}
}

return functionGetInvoiceOrder(text);

实施提示: 将你的逻辑封装在一个命名函数中,并用以下方式调用它: 返回函数名(文本); 在底部。仪表板将表达式主体作为函数执行,其最终返回值填充到键中。

步骤 4:测试解析并保存更改

编辑器顶部的操作按钮从左到右排列,顺序与使用顺序一致:

  1. 上传模板文件 加载样本 PDF 用于绘制捕获区域。
  2. 测试解析 对上传的样本运行所有键值对,并将提取的值直接显示。使用此功能可验证每个正则表达式或 JavaScript 保存前请先输入表达式。
  3. 保存更改 持久化模板并分配一个稳定的 TemplateId (GUID)。复制该 GUID 以供使用。 API 呼叫和自动化平台。

遍历每个键直到 测试解析 返回每个字段的预期值。如果看到相邻文本,请缩小捕获区域;如果模式匹配过于宽松,请优化表达式。

使用模板 API 或自动化呼叫

一次 保存更改 分配一个 TemplateId同一个解析模板可以通过引用在任何位置运行。您无需在每个平台上重新创建配置。

场地来源目的
TemplateId保存后,模板详细信息面板中会显示 GUID。用于生产自动化的稳定标识符。始终优先选择此标识符。 TemplateName
TemplateName您在步骤 1 中输入的名称查找的替代方法。重命名模板会破坏按名称引用它的调用。
ParseId客户端生成的 GUID(每次调用生成一个)将您的请求与解析输出关联起来,这对于日志记录和审计跟踪非常有用。
docName来源 PDF 文件名用于跟踪和错误消息。
docContent来源 PDF 编码为 Base64待解析的文件。
asyncfalse 对于同步, true 用于民意调查控制响应的传递。

例子 REST 请求正文:

{
"docName": "invoice.pdf",
"docContent": "BASE64_ENCODED_PDF_CONTENT",
"TemplateId": "12345678-1234-1234-1234-123456789abc",
"ParseId": "87654321-4321-4321-4321-cba987654321",
"async": false
}

响应中包含模板中定义的每个键对应的字段。路由该响应。 JSON 进入任何下游节点: Google SheetsAirtable数据库、Excel 表格或 webhook。

常用工作流程

典型的解析模板模式How a saved parse template moves from dashboard to production.
发票收件箱到会计电子表格
  1. 供应商发票 PDF 到达受监控的电子邮件或云文件夹。
  2. MakeZapierPower Automate, 或者 n8n 调用 Parse Document 并传入您的 TemplateId
  3. 结构化的 JSON 输出(发票号、总金额、发票日期)将作为一行追加到 Google Sheets 或Excel。
  4. 会计部门直接通过电子表格进行审核和批准。
表格录入数据库记录
  1. 客户上传了一份已填写的表格。 PDF 通过您的门户网站填写表格。
  2. 您的后端调用 Parse Document 函数 TemplateId 以及 Base64 PDF
  3. 已解析 JSON 映射到数据库 INSERT 语句,每个模板键对应一列。
  4. 系统会使用解析后的姓名和参考编号向客户发送确认邮件。
文档分类器加提取器
  1. 一个受监控的文件夹接收混合文档(发票、订单、合同)。
  2. A JavaScript 模板中的表达式键返回文档类型(参见上面的示例 2)。
  3. 您的工作流程会根据返回的文件类型,将每个文件路由到正确的下游系统。
  4. 被识别为发票的文件将继续在同一模板调用中进行基于正则表达式的字段提取。

模板配置最佳实践

  • 绘制捕获区域略大于预期值,以防止字体或位置偏移将值推出画面。
  • 在所有模板中保持关键名称使用驼峰命名法一致,以便电子表格、数据库和 Webhook 中的下游映射保持可预测性。
  • 每个键都至少针对三个真实样本进行测试,包括缺少可选字段、第二页发票等极端情况,以及 OCR从扫描件中提取的文本 PDFs
  • 使用 JavaScript 仅当正则表达式无法表达规则时才使用表达式。保持逻辑简洁有助于模板的调试。
  • 按名称对模板进行版本控制(invoice-v1invoice-v2)在进行重大变更时,以便生产自动化能够按自己的节奏迁移。
  • 运行扫描 PDFs 通过 OCR 首先( PDF4me OCR 解析之前,先读取端点。模板从文本层提取,该文本层已扫描。 PDFs 直到 OCR 已应用。

相关行动

常见问题解答

Should I use Regex Expression or JavaScript Expression first?+
Start with Regex Expression for fixed-shape fields like invoice numbers, dates, totals, and tax IDs. Move to JavaScript Expression when extraction depends on multiple conditions, fallback rules, or document classification. Most production templates use Regex for around 80% of keys and JavaScript for the rest.
Where does the text variable in JavaScript Expression come from?+
PDF4me passes the extracted text of the captured region (or the full PDF when Search Whole Page is enabled) into the expression context as the variable named text. Your function evaluates that string and returns a string for the configured key.
Does parse template extraction work on scanned PDFs?+
Yes, when the PDF has been OCR-processed first. Scanned PDFs are images until OCR is applied. Run the source file through the PDF4me OCR endpoint or upload an already-OCR'd PDF to the dashboard before drawing capture areas. The template then extracts from the OCR text layer.
Can one template handle multiple invoice layouts?+
It can absorb small differences in font, position, or page count. For genuinely different layouts (two vendors with different invoice designs) create separate templates and route files to the right one by classifier or source system before calling Parse Document.
What do I need to call the parse template from the API?+
Send docName, docContent (the PDF as Base64), TemplateId (the GUID copied from the dashboard after Save Changes), ParseId (a client-generated GUID), and async (false for immediate response, true for polling). TemplateName works instead of TemplateId but TemplateId is recommended for stable automation.
How do I move extracted data into Excel, Google Sheets, or a database?+
The parse output is structured JSON, with one field per key you defined in the template. Route that JSON to any destination from your automation platform: Add Row in Google Sheets, Insert Row in Airtable or a database, or write to Excel via the Excel module in Make, Power Automate, n8n, or Zapier. The same TemplateId works on every platform.
How does this compare with Python PDF data extraction libraries?+
Tools like pdfplumber, PyMuPDF, and pdfminer give you raw extraction primitives in Python, but you write and maintain the matching logic yourself. PDF4me parse templates give you a saved, named template you configure once in a dashboard and call from any language or platform. Use Python libraries when you need full programmatic control inside a single codebase; use PDF4me parse templates when you want the same extraction running across the REST API, Make, Zapier, Power Automate, and n8n without copying logic between systems.
How should I test a parse template before production?+
Validate each key against at least three real samples, including edge cases such as missing optional fields, alternate fonts, multi-page documents, and second-page positions. Use Test Parse in the dashboard for each sample, confirm the output matches expectations, then run a small automation against live files in monitor mode before enabling end-to-end automation.
How do I connect this setup to no-code platforms?+
Use the platform-specific guides under Related actions. Each platform exposes the same TemplateId and ParseId fields so the parsing logic stays identical to what you tested in the dashboard. The output structure also matches across platforms.

获取帮助