# PDF API

> exportPdf 配置项参考：页面大小、页边距、缩放方式、导出模式、TrueType 字体和返回的警告。

Source: https://fabricjs-document-engine.jscrate.dev/zh/docs/api/pdf-api
Last updated: 2026-10-01

`fabricjs-document-engine/pdf` 入口需要可选的 `jspdf` 和 `svg2pdf.js` 两个包，只有生成 PDF 时才会加载。exportPdf 配置项怎样配合使用，见 [PDF 导出指南](https://fabricjs-document-engine.jscrate.dev/zh/docs/guides/pdf-export)。

## exportPdf

```ts
function exportPdf(sources: PdfSource | readonly PdfSource[], options?: PdfExportOptions): Promise<PdfExportResult>
```

Makes a PDF with one page per source: an engine, a Fabric canvas or a saved document. Needs `jspdf`, and `svg2pdf.js` for vector and hybrid mode.

```ts
function layoutPage(content: {
  width: number;
  height: number;
}, options?: PageLayoutOptions): PageLayout
```

Where a canvas of `{ width, height }` pixels lands on a page, in points.

## exportPdf 配置项

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `mode` | `PdfMode` | `"hybrid"` | `vector` draws everything as PDF vectors and text, `raster` draws each page as one picture, and `hybrid` draws only what PDF vectors cannot show as a picture of that object. |
| `dpi` | `number` | `300` | Resolution of everything drawn as a picture. |
| `fonts` | `PdfFont[]` | `[]` | TrueType (.ttf) files for the families the canvas uses. Text in these families stays real, selectable text. |
| `missingFonts` | `"rasterize" \| "substitute"` | `"rasterize" in hybrid mode` | Text in a family with no file: `rasterize` draws it as a picture, `substitute` uses the closest built-in PDF font. |
| `background` | `ExportBackground` | `"keep"` | As for `export`. |
| `metadata` | `{ title?: string; author?: string; subject?: string; keywords?: string; creator?: string; }` | `none` | `{ title, author, subject, keywords, creator }`. |
| `signal` | `AbortSignal` | `none` | Cancels with `EXPORT_ABORTED`. |
| `customObjects` | `CustomObjectDefinition[]` | `[]` | Used for saved documents passed as sources. |
| `assets` | `AssetOptions` | `{}` | Used for saved documents passed as sources. |
| `limits` | `ContentLimits` | `defaults` | Used for saved documents passed as sources. |
| `page` | `PdfPageSize` | `"canvas"` | `A3`, `A4`, `A5`, `Letter`, `Legal`, `Tabloid`, `canvas` for a page the size of the canvas, or `[width, height]` in points. Inherited from PageLayoutOptions. |
| `orientation` | `PdfOrientation` | `"auto"` | `portrait`, `landscape`, or `auto` to turn named pages to match the drawing. Inherited from PageLayoutOptions. |
| `margin` | `number \| Partial<PdfMargin>` | `0` | Points, as one number or `{ top, right, bottom, left }`. Inherited from PageLayoutOptions. |
| `fit` | `PdfFit` | `"contain"` | `contain` shows all of the canvas, `cover` fills the box and clips, `none` prints at real size (96 pixels to the inch). Inherited from PageLayoutOptions. |

页面大小、方向、页边距和缩放方式来自 `PageLayoutOptions`：

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `page` | `PdfPageSize` | `"canvas"` | `A3`, `A4`, `A5`, `Letter`, `Legal`, `Tabloid`, `canvas` for a page the size of the canvas, or `[width, height]` in points. |
| `orientation` | `PdfOrientation` | `"auto"` | `portrait`, `landscape`, or `auto` to turn named pages to match the drawing. |
| `margin` | `number \| Partial<PdfMargin>` | `0` | Points, as one number or `{ top, right, bottom, left }`. |
| `fit` | `PdfFit` | `"contain"` | `contain` shows all of the canvas, `cover` fills the box and clips, `none` prints at real size (96 pixels to the inch). |

## 字体

`fonts` 的每一项都是一个 `PdfFont`。字体必须是 TrueType（.ttf）文件：

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `family` (required) | `string` | — | The family exactly as the canvas uses it. |
| `source` (required) | `string \| ArrayBuffer \| Uint8Array \| Blob` | — | The .ttf file: its URL, or its bytes. |
| `weight` | `"normal" \| "bold" \| number` | `"normal"` | `normal`, `bold` or a number. Weights from 600 count as bold. |
| `style` | `PdfFontStyle` | `"normal"` | `normal` or `italic`. |

## 返回结果

| Name | Type | Default | Description |
| --- | --- | --- | --- |
| `blob` (required) | `Blob` | — | The PDF file. |
| `pageCount` (required) | `number` | — | How many pages it has. |
| `warnings` (required) | `PdfWarning[]` | — | `PDF_RASTERIZED`, `PDF_UNSUPPORTED`, `PDF_FONT_SUBSTITUTED` or `IMAGE_NOT_EMBEDDED`, each with the page and object ids. |

每条警告都是 `{ code, message, page, objectIds, family?, url? }`，`code` 为 `PDF_RASTERIZED`、`PDF_UNSUPPORTED`、`PDF_FONT_SUBSTITUTED` 或 `IMAGE_NOT_EMBEDDED`。`PDF_UNAVAILABLE`、`PDF_FAILED` 等错误和 exportPdf 配置项相关的说明，列在[错误码](https://fabricjs-document-engine.jscrate.dev/zh/docs/api/error-codes)里。
