通过 REST API 导出 DOCX

Available in Start planBetav2.12.0

DOCX 导出 API 会将 Tiptap JSON 文档转换为 .docx(Microsoft Word)文件。它使用与 编辑器扩展 相同的转换库,并为标准内容生成等效的 DOCX 输出。

该 REST API 支持 样式覆盖、页面尺寸和边距、页眉/页脚,以及元素级覆盖(tableOverridesparagraphOverridestextRunOverridestableCellOverridesimageOverrides)。它不支持自定义节点渲染,也不支持任何需要 JavaScript 函数的选项。如果您需要这些能力,请使用 编辑器扩展,它也适用于服务器端。

查看 Postman 集合

您也可以前往我们的Postman 集合来试用文档转换 API。

导出 DOCX

POST /v2/convert/export/docx

/v2/convert/export/docx 端点将 Tiptap 文档转换为 DOCX 格式。用户可以向此端点 POST 文档,并使用多种参数自定义转换过程中不同文档元素的处理方式。

导出自定义支持

/v2/convert/export/docx 端点接受 样式覆盖、页面尺寸/边距、页眉/页脚,以及五种元素级覆盖(tableOverridesparagraphOverridestextRunOverridestableCellOverridesimageOverrides)。它不支持自定义节点转换,因为这些功能需要无法序列化的 JavaScript 函数。对于自定义节点转换,请使用 编辑器扩展服务器端导出指南

示例(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

请求体

名称类型说明默认值
docStringTiptap 的 JSONN/A
exportTypestring期望的导出类型blob
styleOverridesObject样式覆盖{}
pageSizeObject页面尺寸配置undefined
pageMarginsObject页面边距配置undefined
headersObject页眉配置undefined
footersObject页脚配置undefined
tableOverridesObject表格级渲染覆盖undefined
paragraphOverridesObject段落级渲染覆盖undefined
textRunOverridesObject文本运行级渲染覆盖undefined
tableCellOverridesObject表格单元格渲染覆盖undefined
imageOverridesObject图片渲染覆盖undefined

页面尺寸配置

pageSize 对象允许您自定义导出 DOCX 文档的页面尺寸:

属性类型说明默认值
widthstring页面宽度。必须是一个正数,后跟有效单位 (cm, in, pt, pc, mm, px)。"21.0cm"
heightstring页面高度。必须是一个正数,后跟有效单位 (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 文档的页面边距:

属性类型说明默认值
topstring页面上边距。可为负值。必须是一个数字加有效单位 (cm, in, pt, pc, mm, px)。"1.0cm"
bottomstring页面下边距。可为负值。必须是一个数字加有效单位 (cm, in, pt, pc, mm, px)。"1.0cm"
leftstring页面左边距。必须为正数加有效单位 (cm, in, pt, pc, mm, px)。"1.0cm"
rightstring页面右边距。必须为正数加有效单位 (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"}
    }'

页眉和页脚

headersfooters 对象允许您自定义导出 DOCX 的一系列页眉和页脚选项。

页眉配置

headers 对象允许您自定义导出 DOCX 文档的页眉:

属性类型说明
evenAndOddHeadersboolean是否为奇偶页使用不同的页眉
differentFirstPageboolean是否为首页使用不同的页眉。当为 true 时,第一页使用 first 的值,替代 default
defaultstring所有页面的默认页眉,或在启用 evenAndOddHeaders 时奇数页的页眉。支持纯文本字符串或字符串化的 Tiptap JSONContent,实现富格式。
firststring首页的页眉,仅当 differentFirstPagetrue 时生效。支持纯文本字符串或字符串化的 Tiptap JSONContent。
evenstring启用 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 文档的页脚:

属性类型说明
evenAndOddFootersboolean是否为奇偶页使用不同的页脚
differentFirstPageboolean是否为首页使用不同的页脚。当为 true 时,第一页使用 first 的值,替代 default
defaultstring所有页面的默认页脚,或在启用 evenAndOddFooters 时奇数页的页脚。支持纯文本字符串或字符串化的 Tiptap JSONContent,实现富格式。
firststring首页的页脚,仅当 differentFirstPagetrue 时生效。支持纯文本字符串或字符串化的 Tiptap JSONContent。
evenstring启用 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 转换库;来自文档的按节点值在计算后仍然优先。

不进行深度合并

覆盖项中的嵌套对象(例如 bordersmarginstransformation)会被完全替换,而不是进行深度合并。自定义边框某一侧时,请提供你需要的每一侧,以免未定义值漏入。

tableOverrides

应用于每个表格的默认值。最有用的属性:

PropertyTypeDescription
bordersObject各边定义:topbottomleftrightinsideHorizontalinsideVertical。每项都接受 { style, size, color }
marginsObject默认单元格边距 { top, bottom, left, right }(以二十分之一磅为单位)。
widthObject表格宽度 { size, type },其中 type"pct""dxa""auto"
layoutstring"fixed""autofit"
alignmentstring"left" | "center" | "right" | "start" | "end"
cantSplitboolean防止表格跨页拆分。
visuallyRightToLeftboolean使表格从右到左渲染。

paragraphOverrides

应用于每个段落的默认值。最有用的属性:

PropertyTypeDescription
spacingObject{ before, after, line, lineRule },单位为二十分之一磅。line 会被计算出的行高覆盖。
alignmentstring"left" | "center" | "right" | "justified" | "start" | "end"
indentObject{ left, right, firstLine, hanging, start, end },单位为二十分之一磅。
keepNextboolean将此段落与下一段保持在一起。
keepLinesboolean保持段落中的所有行在一起。
pageBreakBeforeboolean在段落前插入分页符。
bidirectionalboolean将段落从右到左渲染。
stylestring指向 styleOverrides 中定义的段落样式 id 的引用。

textRunOverrides

应用于每个文本运行的默认值。按标记格式设置(粗体、斜体、颜色 ……)仍会覆盖这些值。最有用的属性:

PropertyTypeDescription
fontstring字体族名称。
sizenumber字体大小,单位为半磅(24 = 12pt)。
boldboolean默认以粗体渲染。
italicsboolean默认以斜体渲染。
underlineObject{ type, color } — 例如 { type: "single", color: "auto" }
strikeboolean删除线。
colorstring不含前导 # 的十六进制颜色("FF0000")。
highlightstring预定义的高亮颜色名称("yellow""green"、……)。
superScriptboolean以上标形式渲染。
subScriptboolean以下标形式渲染。

tableCellOverrides

应用于每个表格单元格的默认值。最有用的属性:

PropertyTypeDescription
shadingObject{ fill, type, color } — 例如 { fill: "F0F0F0", type: "clear" }
verticalAlignstring"top" | "center" | "bottom"
bordersObject各边单元格边框定义,形状与 tableOverrides.borders 相同。
marginsObject单元格内边距 { top, bottom, left, right },单位为二十分之一磅。
widthObject单元格宽度 { size, type }

imageOverrides

应用于每张图片的默认值。根据文档计算出的图片尺寸 (固有大小或用户调整后的值)在存在时仍然优先。最有用的 属性:

PropertyTypeDescription
transformationObject{ width, height, rotation?, flip? } — 以 96 dpi 计算的像素。
altTextObject{ title, description, name }
floatingObject浮动定位选项(锚点、对齐、偏移、环绕)。

组合多个元素覆盖的示例 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

错误响应

状态码错误码说明
400NO_DOCUMENT_PROVIDED请求体中未提供文档
422FAILED_TO_PARSE_DOCX_FILEJSON 输入解析失败
422FAILED_TO_EXPORT_DOCX_FILE导出 DOCX 失败
422FAILED_TO_CONVERT_DOCX_TO_BLOB转换结果类型失败