通过 REST API 导出 DOCX

Available in Start planBetav2.25.0

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

The REST API supports style overrides, custom-node rendering via the DSL, page size and margins, headers/footers, comment threads, and element-level overrides (tableOverrides, paragraphOverrides, textRunOverrides, tableCellOverrides, imageOverrides). It does not accept JavaScript-function options. For the function-based custom-node API, use the editor extension, which also works server-side.

查看 postman 集合

您也可以前往我们的 Postman Collection 体验 Document Conversion API。

导出 DOCX

POST /v2/convert/export/docx

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

支持导出自定义

/v2/convert/export/docx 端点接受 样式覆盖、页面大小/页边距、页眉/页脚、五种元素级覆盖(tableOverridesparagraphOverridestextRunOverridestableCellOverridesimageOverrides),以及 通过 JSON DSL 的自定义节点渲染。它不 接受 JavaScript 函数选项。对于基于函数的自定义节点 API,请使用 编辑器 扩展服务端导出 指南

示例(cURL)

curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"attrs\":{\"textAlign\":\"left\"},\"content\":[{\"type\":\"text\",\"text\":\"欢迎参加本次演示,展示我们的编辑器如何将各种格式选项导出为 DOCX,确保您的内容在 Word 中保持预期外观。\"}]}]}",
      "exportType": "blob"
    }'

需要订阅

此端点需要有效的 Tiptap 订阅。更多详情请查看我们的 定价 页面

必填请求头

NameDescription
AuthorizationThe JWT token to authenticate the request. Example: Bearer your-jwt-token
Content-TypeMust be application/json

请求体

NameTypeDescriptionDefault
docStringTiptap's JSONN/A
exportTypestringThe expected export typeblob
styleOverridesObjectStyle overrides{}
pageSizeObjectPage size configurationundefined
pageMarginsObjectPage margins configurationundefined
headersObjectPage header configurationundefined
footersObjectPage footer configurationundefined
tableOverridesObjectTable-level rendering overridesundefined
paragraphOverridesObjectParagraph-level rendering overridesundefined
textRunOverridesObjectText run rendering overridesundefined
tableCellOverridesObjectTable cell rendering overridesundefined
imageOverridesObjectImage rendering overridesundefined
customNodeDslObjectCustom-node DSL rulesundefined
placeholdersObject | falseOptional { page?, total? } rename map matching Pages.configure({ placeholders }) so renamed tokens (e.g. {p} / {pages}) in plain-text headers/footers become live PAGE / NUMPAGES fields. Pass false to disable substitution entirely (literal {page} / {total} text in the exported .docx). See Custom token names.undefined
commentsObjectComment threads to embed as native Word comments: { threads: [...] }. Each thread carries its messages, author, dates, and resolved state, and is anchored to the document's inlineThread / blockThread marks. See Comments.undefined

页面尺寸配置

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

PropertyTypeDescriptionDefault
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 "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 文档的页面边距:

PropertyTypeDescriptionDefault
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 "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 文档的页眉:

PropertyTypeDescription
evenAndOddHeadersboolean是否为奇数页和偶数页使用不同的页眉
differentFirstPageboolean是否在首页使用不同的页眉。设为 true 时,首页使用 first 值而不是 default
defaultstring每一页的标准默认页眉;当启用 evenAndOddHeaders 选项时,则为奇数页页眉。可接受纯文本字符串或字符串化的 Tiptap JSONContent 以实现富文本格式。
firststring首页页眉。仅当 differentFirstPage 设为 true 时使用。可接受纯文本字符串或字符串化的 Tiptap JSONContent 以实现富文本格式。
evenstring启用 evenAndOddHeaders 选项时的偶数页页眉。可接受纯文本字符串或字符串化的 Tiptap JSONContent 以实现富文本格式。

纯文本 vs. 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 "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 文档的页脚:

PropertyTypeDescription
evenAndOddFootersboolean是否为奇数页和偶数页使用不同的页脚
differentFirstPageboolean是否在首页使用不同的页脚。设为 true 时,首页使用 first 值而不是 default
defaultstring每一页的标准默认页脚;当启用 evenAndOddFooters 选项时,则为奇数页页脚。可接受纯文本字符串或字符串化的 Tiptap JSONContent 以实现富文本格式。
firststring首页页脚。仅当 differentFirstPage 设为 true 时使用。可接受纯文本字符串或字符串化的 Tiptap JSONContent 以实现富文本格式。
evenstring启用 evenAndOddFooters 选项时的偶数页页脚。可接受纯文本字符串或字符串化的 Tiptap JSONContent 以实现富文本格式。

纯文本 vs. Tiptap JSONContent

每个页脚值都可以是 纯文本字符串(生成简单、无样式的页脚)或 字符串化的 Tiptap JSONContent 对象(可启用加粗、斜体、链接等富文本格式)。当传入 JSONContent 时,请在请求体中发送前使用 JSON.stringify() 将对象转为字符串。

`{page}` / `{total}` tokens become live Word fields

The REST API translates {page}, {total}, and the {numpages} synonym inside plain-text headers/footers into live Word PAGE / NUMPAGES fields in the exported .docx. Custom token names (e.g. {p} / {pages}) require passing the optional placeholders rename map. See Custom token names below. Pass "placeholders": false to ship the tokens as literal text instead.

自定义标记名称 (placeholders)

当编辑器通过 Pages.configure({ placeholders }) 重命名内置标记时,请将同样的重命名映射传给 REST API,以保持传输格式一致。该字段在传输层中的名称和结构与编辑器选项相同:

{
  "doc": "{\"type\":\"doc\",\"content\":[]}",
  "footers": { "default": "{p} of {pages}" },
  "placeholders": { "page": "p", "total": "pages" }
}

When the map is omitted, the service falls back to recognizing {page}, {total}, and {numpages} (Word's spelling for the total-pages field, kept as a synonym so direct REST API callers can mirror what /import/docx emits). Renaming total removes the {numpages} synonym so stale tokens render literally; the rename is strict. Pass "placeholders": false to disable substitution on every slot so all {page} / {total} text ships literally with no live fields.

带自定义页脚的 cURL 示例:

curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"Document with custom footers\"}]}]}",
      "exportType": "blob",
      "footers": {"evenAndOddFooters": true, "default": "Default Footer", "first": "First Page Footer", "even": "Even Page Footer"}
    }'

Comments

Pass a comments object to embed comment threads as native Word comments in the exported .docx. Each thread is anchored to the text it belongs to through the document's own comment markers: an inlineThread mark whose data-thread-id matches the thread id, or a blockThread node. The comments field supplies the thread bodies, authors, dates, and resolved state.

The shape mirrors the editor's export extension:

FieldTypeDescription
threadsArrayThe comment threads to embed.
threads[].idStringThread id. Must match the data-thread-id on the matching inlineThread / blockThread in doc.
threads[].resolvedAtString | nullWhen set, the thread is exported as resolved.
threads[].commentsArrayThe thread's messages, root first, then replies in order.
comments[].idStringMessage id.
comments[].contentString | ObjectThe message body as plain text or Tiptap JSON.
comments[].dataObjectOptional metadata: author, initials, dateIso, dateUtc, and round-trip identities (originalWId, originalDurableId).

When omitted, the export contains no comments, so existing requests are unaffected.

Example cURL with a comment thread:

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\":\"Reviewed text\",\"marks\":[{\"type\":\"inlineThread\",\"attrs\":{\"data-thread-id\":\"T1\"}}]}]}]}",
      "exportType": "blob",
      "comments": {
        "threads": [
          {
            "id": "T1",
            "resolvedAt": null,
            "comments": [
              {
                "id": "c1",
                "content": "Please cite a source.",
                "data": { "author": "Ada Lovelace", "initials": "AL", "dateIso": "2026-01-01T00:00:00.000Z" }
              }
            ]
          }
        ]
      }
    }'

元素覆盖

五种元素覆盖对象可让你调整各个 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浮动定位选项(anchor、alignment、offset、wrap)。

组合多个元素覆盖的示例 cURL

curl --output example.docx -X POST "https://api.tiptap.dev/v2/convert/export/docx" \
    -H "Authorization: Bearer YOUR_TOKEN" \
    -H "Content-Type: application/json" \
    -d '{
      "doc": "{\"type\":\"doc\",\"content\":[{\"type\":\"paragraph\",\"content\":[{\"type\":\"text\",\"text\":\"带元素级覆盖的文档\"}]}]}",
      "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

错误响应

StatusCodeDescription
400NO_DOCUMENT_PROVIDED未在请求体中提供文档
422FAILED_TO_PARSE_DOCX_FILE解析 JSON 输入失败
422FAILED_TO_EXPORT_DOCX_FILE导出 DOCX 失败
422FAILED_TO_CONVERT_DOCX_TO_BLOB转换结果类型失败

转换字体

POST /v2/convert/fonts/convert

将字体转换为可嵌入 DOCX 文件的形式:TTF / OTF 上传会原样返回,而 WOFF2 上传会转换为 TTF。这是编辑器扩展的 embedFonts 选项在后台调用的端点;你也可以直接调用它。自 Convert Service v2.25.0 起可用。

The endpoint is stateless: nothing is stored. Each request is converted and the temporary files are cleaned up immediately.

必需请求头

Authorization: Bearer <your-jwt>

请求体

multipart/form-data:

字段类型描述必填
fileFile字体二进制文件(TTFOTFWOFF2)。最大 25MB。
fontFamilyString字体族名称,仅用于日志记录。

示例(cURL)

curl --output MyFont.ttf -X POST "https://api.tiptap.dev/v2/convert/fonts/convert" \
  -H "Authorization: Bearer <your-jwt>" \
  -F "file=@MyFont.woff2" \
  -F "fontFamily=My Font"

响应

成功时,API 会将可嵌入的字体作为二进制下载返回:

  • 状态200 OK
  • Content-Type: font/ttf

错误响应

StatusCodeDescription
400NoneRequest validation error (e.g. no file field)
413FONT_FILE_TOO_LARGEThe uploaded font exceeds the 25MB limit
422UNSUPPORTED_FONT_FORMATThe upload is not a recognized TTF, OTF, or WOFF2 font
422FONT_CONVERSION_FAILEDThe WOFF2 → TTF conversion produced no output
500FONT_CONVERSION_FAILEDUnexpected error during conversion