跳到主要内容

解析文档 API

此端点的作用

PDF4me 解析文档 使用您保存的解析模板对 PDF 并返回提取的字段 JSON 单个 REST 致电。发送 PDF 作为 Base64, 这 模板 ID 从控制面板和客户端生成的 解析 ID并接收一个结构化的响应,该响应以您在模板中定义的名称为键。该模板包含提取逻辑(正则表达式 对于稳定模式, JavaScript 表达 对于条件规则),因此,同一个调用可以提取发票、合同、收据以及您配置的任何自定义文档布局。

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

调用此端点之前: 在解析模板中创建解析模板 PDF4me 仪表盘。请参阅 准备文档解析信息 有关完整的设置步骤,请参阅正则表达式示例(INV-\d{6,10} 发票号码 \d{2}/\d{2}/\d{4} (日期),以及两个工作单位 JavaScript 表达式分类器样本。

验证您的身份 API 要求

每一个 PDF4me REST 通话必须包含您的 API 关键在于 Authorization 头部信息。在开发者控制面板中创建或选择一个密钥,并将其保存在服务器端。切勿将其暴露在浏览器代码中。

您不容错过的重要事实

最小有效载荷为三个字段,而不是五个。
仅有的 文档内容文档名称, 和 异步 是必需的。 模板 ID模板名称, 和 解析 ID 这些字段是可选的,只有当您希望响应内容与自定义捕获字段关联时才需要。如果没有它们, API 仍然返回有用的默认字段,例如 文档类型页数
回答是 JSON不是二进制
解析文档返回 application/json 模板中每个捕获键对应一个字段,外加默认字段。这与返回原始二进制文件的 Protect、Compress 和 Convert 端点不同。 PDFs解析文档始终返回 JSON 因为它返回的是结构化数据,而不是文件。
生产环境中请使用 TemplateId,而不是 TemplateName。
TemplateId 是一个稳定的 GUID 此值由控制面板在“保存更改”时分配,在模板的整个生命周期内都不会改变。TemplateName 可用作查找替代方案,但如果您重命名模板,则此方法会失效。请始终从模板详细信息面板复制 TemplateId 并将其固定在您的代码中。

REST API 端点

方法: 邮政
URL https://api.pdf4me.com/api/v2/ParseDocument

发送 Content-Type: application/json 以及 授权 带有您标题的标题 API 键。设置 异步错误的 对于同步响应(HTTP 200 已解析 JSON), 或者 真的 接收 HTTP 202 加一个 地点 轮询该标头,直到它返回 200 并包含已解析的内容。 JSON

Postman 请求设置

环境价值
MethodPOST
URLhttps://api.pdf4me.com/api/v2/ParseDocument
HeadersContent-Type: application/json
AuthorizationBasic Auth with your API key, or header Authorization: Basic YOUR_API_KEY
Bodyraw JSON with docContent, docName, async (and optional TemplateId, TemplateName, ParseId)
Response (sync)When async is false: HTTP 200 with parsed JSON containing one field per template key plus default fields such as documentType and pageCount.
Response (async)When async is true: HTTP 202 with a Location header. GET that URL until you receive 200 with the parsed JSON. Useful for large PDFs or batch processing.

参数

始终需要: 文档内容文档名称异步条件(基于模板的提取): 模板 ID (推荐)或 模板名称解析 ID如果没有这些, API 仍然返回有用的默认字段(documentType、pageCount),但不返回自定义键值。

范围必需的类型它的作用例子
docContentYesBase64 StringThe source PDF file encoded as Base64 (no data: prefix). Read the file as bytes and run it through your language's Base64 encoder.JVBERi0xLjQK...
docNameYesStringFilename of the source PDF including .pdf extension. Used for tracking and error messages.invoice.pdf
asyncYesBooleanProcessing mode. false returns parsed JSON immediately with HTTP 200. true returns HTTP 202 plus a Location header that you poll until it returns 200 with the parsed JSON. Use true for large PDFs or batch processing.true
TemplateIdConditionalString (GUID)GUID of the saved parse template. Recommended over TemplateName for stable production automation. Get it from the template detail panel after Save Changes in the dashboard.12345678-1234-1234-1234-123456789abc
TemplateNameConditionalStringTemplate name as typed in the dashboard. Lookup alternative to TemplateId. Renaming the template breaks calls that reference it by name, so prefer TemplateId in production.invoice_template
ParseIdConditionalString (GUID)Client-generated GUID per call. Used to correlate the request with the parse output for logging and audit trails. Generate with uuid.uuid4 (Python), Guid.NewGuid (C#), UUID.randomUUID (Java).87654321-4321-4321-4321-cba987654321

请求示例

示例 A:最小有效载荷(无模板)

最小的呼叫 API 接受请求。返回默认字段(documentType、pageCount),但不返回自定义键值,因为没有引用模板。

{
"docContent": "JVBERi0xLjQK...",
"docName": "invoice.pdf",
"async": true
}

示例 B:基于模板的提取(生产模式)

推荐的生产环境有效负载。它会根据模板中定义的捕获键返回一个字段,外加一些默认字段。

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

示例 C:按名称查找模板

当您没有现成的 TemplateId 时,可以使用此查找方法。请避免在生产环境中使用此方法,因为重命名模板会导致此调用失效。

{
"docContent": "JVBERi0xLjQK...",
"docName": "invoice.pdf",
"TemplateName": "invoice_template",
"ParseId": "87654321-4321-4321-4321-cba987654321",
"async": true
}

成功响应(同步, async: false

HTTP 200 已解析 JSON模板中的每个捕获键都会成为一个字段。默认字段(documentTypepageCount) 总是会被返回。

{
"parsedData": {
"invoiceNumber": "INV-2024-001",
"invoiceDate": "15/01/2024",
"totalAmount": "$1,250.50",
"customerName": "Acme Corporation"
},
"documentType": "invoice",
"pageCount": 1
}

成功响应(异步, async: true

HTTP 202 带一个 Location 标题。轮询 URLGET (相同的 Authorization 标头)直到您收到 HTTP 200 已解析 JSON

HTTP/1.1 202 Accepted
Location: https://api.pdf4me.com/api/v2/ParseDocumentStatus/<job-id>

curl 示例

curl -X POST https://api.pdf4me.com/api/v2/ParseDocument \
-H "Content-Type: application/json" \
-H "Authorization: Basic YOUR_API_KEY" \
-d '{
"docContent": "JVBERi0xLjQK...",
"docName": "invoice.pdf",
"TemplateId": "12345678-1234-1234-1234-123456789abc",
"ParseId": "87654321-4321-4321-4321-cba987654321",
"async": true
}'

模板设置

解析模板包含了所有提取逻辑。只需在控制面板中配置一次,然后调用即可。 TemplateId 从任何地方。

正则表达式稳定模式
发票号码(INV-\d{6,10}),日期(\d{2}/\d{2}/\d{4}),金额($?\d{1,3}(?:,\d{3})*(?:.\d{2})?)、税务识别号、邮政编码。约 80% 的生产密钥使用这些信息。
JavaScript 表达式条件逻辑和分类器
多标记分类、回退规则、文档类型检测。提取的文本作为变量传递。 文本你的函数返回一个字符串。参见 准备文档解析信息 两个工作分类器示例(functionFormatTextDate1 和 functionGetInvoiceOrder)。

代码示例

预构建的示例加载 PDF将其编码为 Base64POST/api/v2/ParseDocument并处理同步/异步响应。

集成示例

常见的 REST 集成模式Typical ways developers call Parse Document.
发票收件箱到会计数据库
  1. 观察员发现了一家新供应商 PDFs 从电子邮件收件箱或云文件夹。
  2. 您的服务会读取每个 PDF 将其编码为字节并进行编码 Base64
  3. POST/api/v2/解析文档 使用发票模板 ID 和新的解析 ID。
  4. 映射返回结果 发票号总金额, 和 发票日期 直接导入数据库 INSERT
混合文档分类器加提取器
  1. A JavaScript 模板中的表达式键返回文档类型(发票、订单、条款)。
  2. POST 返回类型以及通过正则表达式提取的字段。 JSON 回复。
  3. 您的代码根据类型字段进行分支,并将结构化数据路由到正确的下游系统。
批量异步处理大量数据 PDFs
  1. 对于超过几兆字节的文件, POST异步:是
  2. 阅读 地点 202 响应的头部信息。
  3. 民意调查 URLGET 每10秒( Python 示例最多使用 15 次重试)。
  4. 当响应状态为 200 时,解析 JSON 主体并继续进行下游加工。

常见问题解答

What is the minimum payload required by the Parse Document REST API?+
Three fields: docContent (the PDF as Base64), docName (filename with .pdf), and async (boolean for sync vs polling). TemplateId, TemplateName, and ParseId are optional. Without a template the API returns default information (documentType, pageCount) but no custom-keyed values.
Should I use TemplateId or TemplateName?+
Use TemplateId in production. It is a stable GUID generated by the dashboard at Save Changes and never changes for the life of the template. TemplateName works as a lookup alternative but breaks if you rename the template. Always pin TemplateId in your code.
What is ParseId and where does it come from?+
ParseId is a client-generated GUID you create per call: uuid.uuid4 in Python, Guid.NewGuid in C#, UUID.randomUUID in Java. Pass it in the request body for logging and audit trail correlation. The API does not validate it against a registry, so any valid GUID works.
Is the response JSON or binary?+
JSON. The response body is application/json containing one field per capture key in your template plus default fields such as documentType and pageCount. This is different from Protect, Compress, and Convert endpoints which return raw binary PDFs.
How does async work for large or batch PDFs?+
Set async to true. The API responds with 202 Accepted plus a Location header containing a poll URL. GET that URL with the same Authorization header. While the document is still processing the poll URL returns 202; when finished it returns 200 with the parsed JSON. Use async true for files over a few MB or when processing in batches.
How is this different from regex parsing in Python with pdfplumber?+
Python libraries like pdfplumber, PyMuPDF, and pdfminer give you raw text extraction primitives and you write the matching logic in your application code. PDF4me Parse Document uses templates you configure once in a hosted dashboard, then calls run that template from any language or platform. The matching logic lives in the template, not your code, which keeps it consistent across systems.
Where do I learn the Regex Expression and JavaScript Expression syntax?+
See the full Prepare Parse Info for Document setup guide. It covers Regex patterns for invoice numbers, dates, and amounts, and includes two working JavaScript Expression classifier samples (functionFormatTextDate1 for Terms and Conditions vs Order classification, functionGetInvoiceOrder for invoice vs order detection).
Can I run the same template from Make, Zapier, Power Automate, or n8n?+
Yes. The TemplateId is the same across all platforms. The Make, Zapier, Power Automate, and n8n PDF4me modules call this same endpoint under the hood. Build and test the template once in the dashboard, then reference its TemplateId from any platform.

相关行动

在其他平台上执行相同的任务

获取帮助