通过 REST API 导出 DOCX
DOCX 导出 API 会将 Tiptap JSON 文档转换为 .docx(Microsoft Word)文件。它使用与 编辑器扩展 相同的转换库,并为标准内容生成等效的 DOCX 输出。
该 REST API 支持 样式覆盖、页面尺寸和边距、页眉/页脚,以及元素级覆盖(tableOverrides、paragraphOverrides、textRunOverrides、tableCellOverrides、imageOverrides)。它不支持自定义节点渲染,也不支持任何需要 JavaScript 函数的选项。如果您需要这些能力,请使用 编辑器扩展,它也适用于服务器端。
查看 Postman 集合
您也可以前往我们的Postman 集合来试用文档转换 API。
导出 DOCX
POST /v2/convert/export/docx
/v2/convert/export/docx 端点将 Tiptap 文档转换为 DOCX 格式。用户可以向此端点 POST 文档,并使用多种参数自定义转换过程中不同文档元素的处理方式。
示例(cURL)
curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-App-Id: YOUR_APP_ID" \
-H "Content-Type: application/json" \
-d '{
"doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":\"left\"},\"content\":[{\"type\":\"text\",\"text\":\"欢迎参加本次演示,展示我们的编辑器如何将各种格式选项导出为 DOCX,确保您的内容在 Word 中保持预期外观。\"}]}]}",
"exportType": "blob"
}'需要订阅支持
使用此端点需要有效的 Tiptap 订阅。详情请参阅我们的定价页面。
必填请求头
| 名称 | 说明 |
|---|---|
Authorization | 用于验证请求的 JWT 令牌。例如:Bearer your-jwt-token |
X-App-Id | 来自 Convert 设置页面的 Convert 应用 ID:https://cloud.tiptap.dev/v2/cloud/convert |
Content-Type | 必须为 application/json |
请求体
| 名称 | 类型 | 说明 | 默认值 |
|---|---|---|---|
doc | String | Tiptap 的 JSON | N/A |
exportType | string | 期望的导出类型 | blob |
styleOverrides | Object | 样式覆盖 | {} |
pageSize | Object | 页面尺寸配置 | undefined |
pageMargins | Object | 页面边距配置 | undefined |
headers | Object | 页眉配置 | undefined |
footers | Object | 页脚配置 | undefined |
tableOverrides | Object | 表格级渲染覆盖 | undefined |
paragraphOverrides | Object | 段落级渲染覆盖 | undefined |
textRunOverrides | Object | 文本运行级渲染覆盖 | undefined |
tableCellOverrides | Object | 表格单元格渲染覆盖 | undefined |
imageOverrides | Object | 图片渲染覆盖 | undefined |
页面尺寸配置
pageSize 对象允许您自定义导出 DOCX 文档的页面尺寸:
| 属性 | 类型 | 说明 | 默认值 |
|---|---|---|---|
width | string | 页面宽度。必须是一个正数,后跟有效单位 (cm, in, pt, pc, mm, px)。 | "21.0cm" |
height | string | 页面高度。必须是一个正数,后跟有效单位 (cm, in, pt, pc, mm, px)。 | "29.7cm" |
带自定义页面尺寸的示例 cURL:
curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-App-Id: YOUR_APP_ID" \
-H "Content-Type: application/json" \
-d '{
"doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"自定义页面尺寸的文档\"}]}]}",
"exportType": "blob",
"pageSize": {"width": "21.0cm", "height": "29.7cm"}
}'页面边距配置
pageMargins 对象允许您自定义导出 DOCX 文档的页面边距:
| 属性 | 类型 | 说明 | 默认值 |
|---|---|---|---|
top | string | 页面上边距。可为负值。必须是一个数字加有效单位 (cm, in, pt, pc, mm, px)。 | "1.0cm" |
bottom | string | 页面下边距。可为负值。必须是一个数字加有效单位 (cm, in, pt, pc, mm, px)。 | "1.0cm" |
left | string | 页面左边距。必须为正数加有效单位 (cm, in, pt, pc, mm, px)。 | "1.0cm" |
right | string | 页面右边距。必须为正数加有效单位 (cm, in, pt, pc, mm, px)。 | "1.0cm" |
带自定义页面边距的示例 cURL:
curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-App-Id: YOUR_APP_ID" \
-H "Content-Type: application/json" \
-d '{
"doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"自定义边距的文档\"}]}]}",
"exportType": "blob",
"pageMargins": {"top": "2.0cm", "bottom": "2.0cm", "left": "1.5cm", "right": "1.5cm"}
}'页眉和页脚
headers 和 footers 对象允许您自定义导出 DOCX 的一系列页眉和页脚选项。
页眉配置
headers 对象允许您自定义导出 DOCX 文档的页眉:
| 属性 | 类型 | 说明 |
|---|---|---|
evenAndOddHeaders | boolean | 是否为奇偶页使用不同的页眉 |
differentFirstPage | boolean | 是否为首页使用不同的页眉。当为 true 时,第一页使用 first 的值,替代 default。 |
default | string | 所有页面的默认页眉,或在启用 evenAndOddHeaders 时奇数页的页眉。支持纯文本字符串或字符串化的 Tiptap JSONContent,实现富格式。 |
first | string | 首页的页眉,仅当 differentFirstPage 为 true 时生效。支持纯文本字符串或字符串化的 Tiptap JSONContent。 |
even | string | 启用 evenAndOddHeaders 后偶数页的页眉。支持纯文本字符串或字符串化的 Tiptap JSONContent。 |
纯文本与 Tiptap JSONContent
每个页眉值可以是纯文本字符串(生成简单无样式的页眉),也可以是字符串化的 Tiptap JSONContent 对象(支持粗体、斜体、链接等富格式)。传递 JSONContent 时,请先用 JSON.stringify() 进行字符串化,然后再放入请求体。
带自定义页眉的示例 cURL:
curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-App-Id: YOUR_APP_ID" \
-H "Content-Type: application/json" \
-d '{
"doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"带自定义页眉的文档\"}]}]}",
"exportType": "blob",
"headers": {"evenAndOddHeaders": true, "default": "默认页眉", "first": "首页页眉", "even": "偶数页页眉"}
}'页脚配置
footers 对象允许您自定义导出 DOCX 文档的页脚:
| 属性 | 类型 | 说明 |
|---|---|---|
evenAndOddFooters | boolean | 是否为奇偶页使用不同的页脚 |
differentFirstPage | boolean | 是否为首页使用不同的页脚。当为 true 时,第一页使用 first 的值,替代 default。 |
default | string | 所有页面的默认页脚,或在启用 evenAndOddFooters 时奇数页的页脚。支持纯文本字符串或字符串化的 Tiptap JSONContent,实现富格式。 |
first | string | 首页的页脚,仅当 differentFirstPage 为 true 时生效。支持纯文本字符串或字符串化的 Tiptap JSONContent。 |
even | string | 启用 evenAndOddFooters 后偶数页的页脚。支持纯文本字符串或字符串化的 Tiptap JSONContent。 |
纯文本与 Tiptap JSONContent
每个页脚值可以是纯文本字符串(生成简单无样式的页脚),也可以是字符串化的 Tiptap JSONContent 对象(支持粗体、斜体、链接等富格式)。传递 JSONContent 时,请先用 JSON.stringify() 进行字符串化,然后再放入请求体。
带自定义页脚的示例 cURL:
curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-App-Id: YOUR_APP_ID" \
-H "Content-Type: application/json" \
-d '{
"doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"带自定义页脚的文档\"}]}]}",
"exportType": "blob",
"footers": {"evenAndOddFooters": true, "default": "默认页脚", "first": "首页页脚", "even": "偶数页页脚"}
}'元素覆盖
五种元素覆盖对象可让你调整各个 DOCX 元素 (表格、段落、文本运行、表格单元格、图片)的渲染方式。每个 覆盖项都会作为基础默认值传递给底层的 DOCX 转换库;来自文档的按节点值在计算后仍然优先。
不进行深度合并
覆盖项中的嵌套对象(例如 borders、margins、transformation)会被完全替换,而不是进行深度合并。自定义边框某一侧时,请提供你需要的每一侧,以免未定义值漏入。
tableOverrides
应用于每个表格的默认值。最有用的属性:
| Property | Type | Description |
|---|---|---|
borders | Object | 各边定义:top、bottom、left、right、insideHorizontal、insideVertical。每项都接受 { style, size, color }。 |
margins | Object | 默认单元格边距 { top, bottom, left, right }(以二十分之一磅为单位)。 |
width | Object | 表格宽度 { size, type },其中 type 为 "pct"、"dxa" 或 "auto"。 |
layout | string | "fixed" 或 "autofit"。 |
alignment | string | "left" | "center" | "right" | "start" | "end"。 |
cantSplit | boolean | 防止表格跨页拆分。 |
visuallyRightToLeft | boolean | 使表格从右到左渲染。 |
paragraphOverrides
应用于每个段落的默认值。最有用的属性:
| Property | Type | Description |
|---|---|---|
spacing | Object | { before, after, line, lineRule },单位为二十分之一磅。line 会被计算出的行高覆盖。 |
alignment | string | "left" | "center" | "right" | "justified" | "start" | "end"。 |
indent | Object | { left, right, firstLine, hanging, start, end },单位为二十分之一磅。 |
keepNext | boolean | 将此段落与下一段保持在一起。 |
keepLines | boolean | 保持段落中的所有行在一起。 |
pageBreakBefore | boolean | 在段落前插入分页符。 |
bidirectional | boolean | 将段落从右到左渲染。 |
style | string | 指向 styleOverrides 中定义的段落样式 id 的引用。 |
textRunOverrides
应用于每个文本运行的默认值。按标记格式设置(粗体、斜体、颜色 ……)仍会覆盖这些值。最有用的属性:
| Property | Type | Description |
|---|---|---|
font | string | 字体族名称。 |
size | number | 字体大小,单位为半磅(24 = 12pt)。 |
bold | boolean | 默认以粗体渲染。 |
italics | boolean | 默认以斜体渲染。 |
underline | Object | { type, color } — 例如 { type: "single", color: "auto" }。 |
strike | boolean | 删除线。 |
color | string | 不含前导 # 的十六进制颜色("FF0000")。 |
highlight | string | 预定义的高亮颜色名称("yellow"、"green"、……)。 |
superScript | boolean | 以上标形式渲染。 |
subScript | boolean | 以下标形式渲染。 |
tableCellOverrides
应用于每个表格单元格的默认值。最有用的属性:
| Property | Type | Description |
|---|---|---|
shading | Object | { fill, type, color } — 例如 { fill: "F0F0F0", type: "clear" }。 |
verticalAlign | string | "top" | "center" | "bottom"。 |
borders | Object | 各边单元格边框定义,形状与 tableOverrides.borders 相同。 |
margins | Object | 单元格内边距 { top, bottom, left, right },单位为二十分之一磅。 |
width | Object | 单元格宽度 { size, type }。 |
imageOverrides
应用于每张图片的默认值。根据文档计算出的图片尺寸 (固有大小或用户调整后的值)在存在时仍然优先。最有用的 属性:
| Property | Type | Description |
|---|---|---|
transformation | Object | { width, height, rotation?, flip? } — 以 96 dpi 计算的像素。 |
altText | Object | { title, description, name }。 |
floating | Object | 浮动定位选项(锚点、对齐、偏移、环绕)。 |
组合多个元素覆盖的示例 cURL
curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
-H "Authorization: Bearer YOUR_TOKEN" \
-H "X-App-Id: YOUR_APP_ID" \
-H "Content-Type: application/json" \
-d '{
"doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"Doc with element-level overrides\"}]}]}",
"tableOverrides": {
"borders": {
"top": { "style": "none" },
"bottom": { "style": "none" },
"left": { "style": "none" },
"right": { "style": "none" },
"insideHorizontal": { "style": "none" },
"insideVertical": { "style": "none" }
}
},
"paragraphOverrides": {
"spacing": { "before": 200, "after": 200 }
},
"textRunOverrides": {
"font": "Arial",
"size": 24
},
"tableCellOverrides": {
"shading": { "fill": "F0F0F0", "type": "clear" },
"verticalAlign": "center"
},
"imageOverrides": {
"transformation": { "width": 400, "height": 300 }
}
}'响应
成功时,API 以二进制形式返回 DOCX 文件下载:
- 状态:
200 OK - Content-Type:
application/vnd.openxmlformats-officedocument.wordprocessingml.document - Content-Disposition:
attachment; filename=export-{timestamp}.docx
错误响应
| 状态码 | 错误码 | 说明 |
|---|---|---|
| 400 | NO_DOCUMENT_PROVIDED | 请求体中未提供文档 |
| 422 | FAILED_TO_PARSE_DOCX_FILE | JSON 输入解析失败 |
| 422 | FAILED_TO_EXPORT_DOCX_FILE | 导出 DOCX 失败 |
| 422 | FAILED_TO_CONVERT_DOCX_TO_BLOB | 转换结果类型失败 |