EPUB 中的自定义页面布局

Available in Start planBetav0.4.2

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

Page Size Configuration

Use the pageSize option to define custom page dimensions for exported EPUB files. Page size configuration supports width and height values in multiple measurement units.

Supported Units

All page size and margin measurements support the following units:

UnitDescription
cmcentimeters (default unit)
ininches
ptpoints
pcpicas
mmmillimeters
pxpixels

Configuration Options

PropertyTypeDescriptionDefault Value
widthstringPage width. Must be a positive number followed by a valid unit (cm, in, pt, pc, mm, px)."21cm"
heightstringPage height. Must be a positive number followed by a valid unit (cm, in, pt, pc, mm, px)."29.7cm"
import { ExportEpub } from '@tiptap-pro/extension-export-epub'

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

页面边距配置

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',
  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',
      // 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',
  // US Letter size in inches
  pageSize: {
    width: '8.5in',
    height: '11in',
  },
  // 不同单位的边距
  pageMargins: {
    top: '0.75in',    // 英寸
    bottom: '72pt',   // 磅(1 英寸 = 72 磅)
    left: '2cm',      // 厘米
    right: '20mm',    // 毫米
  },
})

单位一致性

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