EPUB 中的自定义页面布局

Available in Start planBetav0.3.0

EPUB 导出扩展支持自定义页面尺寸和边距,允许您创建符合需求的精确布局文档。无论您需要 A5 页面、自定义纸张尺寸,还是特定的边距设置,都可以直接在扩展中配置这些参数。

页面尺寸配置

使用 pageSize 选项定义导出 EPUB 文件的自定义页面尺寸。页面尺寸配置支持多种测量单位的宽度和高度值。

支持的单位

所有页面尺寸和边距的测量均支持以下单位:

单位描述
cm厘米(默认单位)
in英寸
pt
pc派卡
mm毫米
px像素

配置选项

属性类型描述默认值
widthstring页面宽度。必须是正数,且后跟有效单位(cm、in、pt、pc、mm、px)。"21cm"
heightstring页面高度。必须是正数,且后跟有效单位(cm、in、pt、pc、mm、px)。"29.7cm"
import { ExportEpub } from '@tiptap-pro/extension-export-epub'

ExportEpub.configure({
  token: 'YOUR_TOKEN',
  appId: 'YOUR_APP_ID',
  pageSize: {
    width: '14.8cm',    // A5 宽度
    height: '21cm',     // A5 高度
  },
})

页面边距配置

pageMargins 选项允许您为文档页面的所有边设置自定义边距。与页面尺寸不同,顶部和底部边距可以接受负值。

配置选项

属性类型描述默认值
topstring页面顶部边距。可以为负值。必须是数字,后跟有效单位(cm、in、pt、pc、mm、px)。"1cm"
bottomstring页面底部边距。可以为负值。必须是数字,后跟有效单位(cm、in、pt、pc、mm、px)。"1cm"
leftstring页面左侧边距。必须是正数,后跟有效单位(cm、in、pt、pc、mm、px)。"1cm"
rightstring页面右侧边距。必须是正数,后跟有效单位(cm、in、pt、pc、mm、px)。"1cm"
import { ExportEpub } from '@tiptap-pro/extension-export-epub'

ExportEpub.configure({
  token: 'YOUR_TOKEN',
  appId: 'YOUR_APP_ID',
  pageMargins: {
    top: '2cm',
    bottom: '2cm',
    left: '1.5cm',
    right: '1.5cm',
  },
})

完整示例

下面是一个完整示例,展示如何使用 EPUB 导出扩展配置自定义页面布局:

import { ExportEpub } from '@tiptap-pro/extension-export-epub'

const editor = new Editor({
  extensions: [
    // 其他扩展...
    ExportEpub.configure({
      token: 'YOUR_TOKEN',
      appId: 'YOUR_APP_ID',
      // A5 页面尺寸
      pageSize: {
        width: '14.8cm',
        height: '21cm',
      },
      // 自定义边距
      pageMargins: {
        top: '2cm',
        bottom: '2cm',
        left: '2cm',
        right: '2cm',
      },
    }),
    // 其他扩展...
  ],
})

// 使用自定义布局导出
editor
  .chain()
  .exportEpub({
    onCompleteExport(result) {
      const url = URL.createObjectURL(result)
      const a = document.createElement('a')

      a.href = url
      a.download = 'custom-layout.epub'
      a.click()

      URL.revokeObjectURL(url)
    },
  })
  .run()

常见页面尺寸

以下是一些常用页面尺寸,供您参考:

格式宽度高度
A421cm29.7cm
A514.8cm21cm
Letter8.5in11in
Legal8.5in14in
Tabloid11in17in

不同的测量单位

您可以根据偏好混合使用不同单位:

ExportEpub.configure({
  token: 'YOUR_TOKEN',
  appId: 'YOUR_APP_ID',
  // 美国 Letter 纸尺寸,单位为英寸
  pageSize: {
    width: '8.5in',
    height: '11in',
  },
  // 不同单位的边距
  pageMargins: {
    top: '0.75in',    // 英寸
    bottom: '72pt',   // 磅(1 英寸 = 72 磅)
    left: '2cm',      // 厘米
    right: '20mm',    // 毫米
  },
})

单位一致性

虽然可以混合使用不同单位,但通常建议在整个配置中使用一致的单位,以便于维护和清晰理解。