生成文档(单个)
PDF4me 生成文档(单个) 通过合并模板(Word、 HTML, 或者 PDF带有数据有效载荷(JSON, XML, 或者 CSV) 在一个 REST 调用。模板可以是 Base64一个公共的 URL或原始 HTML数据可以是行内文本,也可以是…… Base64编码文件,或一个 URL返回渲染后的结果 PDF, Docx, 或者 HTML 以二进制形式 (200) 或通过 Location-header 轮询 URL (202)
验证您的身份 API 要求
每一个 PDF4me REST 通话必须包含您的 API 关键在于 Authorization 请求头设置为基本身份验证。请从开发者控制面板获取或轮换您的密钥。
端点
/api/v2/GenerateDocumentSingle您不容错过的重要事实
文档数据文本 或者 文档数据文件文档数据文本 内联 JSON / XML / CSV。 使用 文档数据文件 为 Base64编码数据文件或公共 URL。PDF 或者 DocxHTML接受 HTML 仅接受 PDF 文件。 PDF 仅限大小写。务必注意大小写。IsAsync: true 这 API 可能会响应 202 和 Location 标头。 GET 那 URL (相同的授权)直到 200 秒,并附带渲染文件(标准) pdf4meAsyncRequest 图案)。HTTP 设置
方法: 邮政
URL: https://api.pdf4me.com/api/v2/GenerateDocumentSingle
内容类型: application/json
授权: 基本 <您的 PDF4me API 键>
发送 IsAsync: true 在体内。 200 返回渲染后的文件字节。 202 返回一个 地点 带有投票的标题 URL; GET 那 URL 使用相同的 Authorization 标头,直到 200。
变异矩阵(与 Postman 相关)
选择一种模板变体(T1/T2/T3)和一种数据变体(D1/D2/D3)。六种组合涵盖所有调用。
模板 (templateFileData)
| 代码 | 来源 | Postman 的价值 templateFileData | 也需要 |
|---|---|---|---|
| T1 | Base64 | Base64 template file (strip data:...;base64, prefix if present) | templateFileName |
| T2 | URL | Full HTTPS URL string (not downloaded by the API request body) | templateFileName |
| T3 | HTML code | Base64-encoded UTF-8 HTML: Buffer.from(html, "utf8").toString("base64") | templateFileName, templateFileType: HTML |
文档数据(二选一) API 字段)
| 代码 | 来源 | API字段 | 邮递员的价值 |
|---|---|---|---|
| D1 | Text | documentDataText | Raw JSON / XML / CSV string |
| D2 | Base64 | documentDataFile | Base64 of the data file (strip data-URL prefix if present) |
| D3 | URL | documentDataFile | Full HTTPS URL to .json / .xml / .csv |
规则: 恰好一个 文档数据文本 或者 文档数据文件 必须设置。从正文中省略未使用的字段。
常见组合
| # | 模板 | 数据 | 典型用途 |
|---|---|---|---|
| 1 | T1 Docx base64 | D1 JSON text | Word mail-merge to PDF |
| 2 | T2 Docx URL | D3 JSON URL | Hosted template + hosted data |
| 3 | T3 HTML | D1 JSON text | HTML mustache rendered to HTML / PDF |
| 4 | T1 PDF form | D2 JSON base64 | PDF template + encoded data file |
| 5 | T2 MailMerge URL | D1 XML text | Word merge fields + inline XML |
templateFileType → 允许的输出类型
模板文件类型 | 允许 输出类型 | 笔记 |
|---|---|---|
Docx | PDF, Docx | PDF4me Word template |
MailMerge | PDF, Docx | Mail merge Word |
GoogleDocs | PDF, Docx | Google Docs export style |
HTML | HTML only | Mustache in HTML |
PDF | PDF only | PDF form / template |
API 身体场
总是发送
| 场地 | 必需的 | 类型 | 笔记 |
|---|---|---|---|
templateFileType | Yes | string | Docx, HTML, PDF, MailMerge, GoogleDocs |
templateFileName | Yes | string | With extension, e.g. template.docx, template.html, template.pdf |
templateFileData | Yes | string | Base64 template, or template URL string |
documentDataType | Yes | string | Json, XML, or Csv (exact casing) |
outputType | Yes | string | PDF, Docx, or HTML (see allowed-output table above) |
IsAsync | Yes | boolean | true |
文档数据(一条路径)
| 场地 | 需要 | 类型 | 笔记 |
|---|---|---|---|
documentDataText | D1 (text) | string | Valid JSON when documentDataType is Json; XML must look like XML; CSV non-empty |
documentDataFile | D2 or D3 | string | Base64 data file or URL to data file |
选修的
| 场地 | 什么时候 | 默认 |
|---|---|---|
KeepPdfEditable | outputType is PDF | false |
documentDataFile | D1 text path is used | Omitted |
documentDataText | D2 or D3 path is used | Omitted |
验证规则
| 查看 | 细节 |
|---|---|
| Template URL | Non-empty, valid URL. |
| Template base64 | Non-empty after optional data-URL strip. |
| HTML code | Only when templateFileType is HTML. Empty is rejected. Auto-wraps <html> if missing. |
| JSON text | Must parse successfully with JSON.parse. |
| XML text | Must start with < and contain >. |
| CSV text | Non-empty after trim. |
| Either data source | documentDataFile or documentDataText is required. |
占位符样本
| 物品 | 例子 |
|---|---|
| Template URL (Docx) | https://example.com/template.docx |
| Template name | template.docx / template.html / template.pdf |
| Data URL (JSON) | https://example.com/data.json |
| Data file name | data.json |
| JSON text | {"name": "John Doe", "email": "[email protected]", "items": [{"product": "Widget", "price": 29.99}]} |
| XML text | <?xml version="1.0"?><root><name>John Doe</name></root> |
| CSV text | name,email\nJohn Doe,[email protected] |
| HTML template | <!DOCTYPE html>...{{title}}... |
示例有效载荷
1. Word模板(base64编码)+ JSON 文本 → PDF
{
"templateFileType": "Docx",
"templateFileName": "template.docx",
"templateFileData": "UEsDBBQAAAAI...",
"documentDataType": "Json",
"documentDataText": "{\"name\": \"John Doe\", \"email\": \"[email protected]\", \"items\": [{\"product\": \"Widget\", \"price\": 29.99}]}",
"outputType": "PDF",
"KeepPdfEditable": false,
"IsAsync": true
}
2. Word模板(URL)+ JSON 数据 (URL)→ PDF
{
"templateFileType": "Docx",
"templateFileName": "template.docx",
"templateFileData": "https://example.com/template.docx",
"documentDataType": "Json",
"documentDataFile": "https://example.com/data.json",
"outputType": "PDF",
"KeepPdfEditable": false,
"IsAsync": true
}
3. Word模板(base64编码)+ JSON 数据(base64)→ Word 输出
documentDataFile 包含 base64 编码的 JSON 文件字节(不是已解析的对象)。
{
"templateFileType": "Docx",
"templateFileName": "template.docx",
"templateFileData": "UEsDBBQAAAAI...",
"documentDataType": "Json",
"documentDataFile": "eyJuYW1lIjogIkpvaG4gRG9lIn0=",
"outputType": "Docx",
"IsAsync": true
}
4. HTML 模板(base64 编码) HTML 文件)+ JSON 文本 → HTML
原始 HTML 在 Postman 中,发送相同的有效负载 n8n 节点将进行 base64 编码 HTML 在 templateFileData。
{
"templateFileType": "HTML",
"templateFileName": "template.html",
"templateFileData": "PCFET0NUWVBFIGh0bWw+...",
"documentDataType": "Json",
"documentDataText": "{\"title\": \"Invoice\", \"heading\": \"Hello\", \"content\": \"World\"}",
"outputType": "HTML",
"IsAsync": true
}
5. 邮件合并(URL)+ XML 文本 → PDF
{
"templateFileType": "MailMerge",
"templateFileName": "template.docx",
"templateFileData": "https://example.com/template.docx",
"documentDataType": "XML",
"documentDataText": "<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><name>John Doe</name><email>[email protected]</email></root>",
"outputType": "PDF",
"IsAsync": true
}
6. PDF 表单模板(base64)+ CSV 文本 → PDF (可编辑)
{
"templateFileType": "PDF",
"templateFileName": "template.pdf",
"templateFileData": "JVBERi0xLjQKJcfsj6IK...",
"documentDataType": "Csv",
"outputType": "PDF",
"KeepPdfEditable": true,
"IsAsync": true
}
7. Google Docs 风格(base64)+ JSON URL → Docx
{
"templateFileType": "GoogleDocs",
"templateFileName": "template.docx",
"templateFileData": "UEsDBBQAAAAI...",
"documentDataType": "Json",
"documentDataFile": "https://example.com/data.json",
"outputType": "Docx",
"IsAsync": true
}
邮递员收款小贴士
curl 示例
curl -X POST https://api.pdf4me.com/api/v2/GenerateDocumentSingle \
-H "Content-Type: application/json" \
-H "Authorization: Basic YOUR_API_KEY" \
-d '{
"templateFileType": "Docx",
"templateFileName": "template.docx",
"templateFileData": "https://example.com/template.docx",
"documentDataType": "Json",
"documentDataFile": "https://example.com/data.json",
"outputType": "PDF",
"KeepPdfEditable": false,
"IsAsync": true
}' \
--output generated.pdf
快速参考:按变体划分的必需项与可选项
| 场地 | T1+D1 | T2+D3 | T1+D2 | T3+D1 | 任何 PDF 输出 |
|---|---|---|---|---|---|
templateFileType | Req | Req | Req | Req | Req |
templateFileName | Req | Req | Req | Req | Req |
templateFileData | Req (b64) | Req (URL) | Req (b64) | Req (b64 HTML) | Req |
documentDataType | Req | Req | Req | Req | Req |
outputType | Req | Req | Req | Req | Req |
documentDataText | Req | Omit | Omit | Req | Omit |
documentDataFile | Omit | Req (URL) | Req (b64) | Omit | Omit |
IsAsync | Req | Req | Req | Req | Req |
KeepPdfEditable | Opt | Opt | Opt | N/A* | Opt if PDF |
*KeepPdf可编辑 仅适用于 输出类型 是 PDF; 这 API 否则会造成错误。