---
title: "EPUB 中的自定义页面布局"
description: "了解如何使用 Export EPUB 扩展自定义您的 EPUB 页面和边距大小。"
canonical_url: "https://tiptap.zhcndoc.com/conversion/export/epub/page-layout"
---

# EPUB 中的自定义页面布局

了解如何使用 Export EPUB 扩展自定义您的 EPUB 页面和边距大小。

- **1. 激活试用或订阅**

  在您的账户中开始一个[免费试用](https://cloud.tiptap.dev/v2?trial=true)或[订阅起始套餐](https://cloud.tiptap.dev/v2/billing)。
- **2. 从私有注册表安装**

  要安装此前端扩展，请按照[设置指南](https://tiptap.zhcndoc.com/guides/pro-extensions.md)进行身份验证，访问 Tiptap 的私有 npm 注册表。

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:

| Unit | Description                |
| ---- | -------------------------- |
| `cm` | centimeters (default unit) |
| `in` | inches                     |
| `pt` | points                     |
| `pc` | picas                      |
| `mm` | millimeters                |
| `px` | pixels                     |

### Configuration Options

| Property | Type     | Description                                                                                           | Default Value |
| -------- | -------- | ----------------------------------------------------------------------------------------------------- | ------------- |
| `width`  | `string` | Page width. Must be a positive number followed by a valid unit (`cm`, `in`, `pt`, `pc`, `mm`, `px`).  | `"21cm"`      |
| `height` | `string` | Page height. Must be a positive number followed by a valid unit (`cm`, `in`, `pt`, `pc`, `mm`, `px`). | `"29.7cm"`    |

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

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

## 页面边距配置

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

### 配置选项

| 属性       | 类型       | 描述                                            | 默认值     |
| -------- | -------- | --------------------------------------------- | ------- |
| `top`    | `string` | 页面顶部边距。可以为负值。必须是数字，后跟有效单位（cm、in、pt、pc、mm、px）。 | `"1cm"` |
| `bottom` | `string` | 页面底部边距。可以为负值。必须是数字，后跟有效单位（cm、in、pt、pc、mm、px）。 | `"1cm"` |
| `left`   | `string` | 页面左侧边距。必须是正数，后跟有效单位（cm、in、pt、pc、mm、px）。       | `"1cm"` |
| `right`  | `string` | 页面右侧边距。必须是正数，后跟有效单位（cm、in、pt、pc、mm、px）。       | `"1cm"` |

```js
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 导出扩展配置自定义页面布局：

```js
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()
```

## 常见页面尺寸

以下是一些常用页面尺寸，供您参考：

| 格式          | 宽度       | 高度       |
| ----------- | -------- | -------- |
| **A4**      | `21cm`   | `29.7cm` |
| **A5**      | `14.8cm` | `21cm`   |
| **Letter**  | `8.5in`  | `11in`   |
| **Legal**   | `8.5in`  | `14in`   |
| **Tabloid** | `11in`   | `17in`   |

## 不同的测量单位

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

```js
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',    // 毫米
  },
})
```

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