# 导入导出

- Human documentation: [https://docs.univer.ai/zh-CN/guides/pdfs/features/import-export](https://docs.univer.ai/zh-CN/guides/pdfs/features/import-export)

- Agent Markdown: [https://docs.univer.ai/zh-CN/guides/pdfs/features/import-export.md](https://docs.univer.ai/zh-CN/guides/pdfs/features/import-export.md)

- Requested language: `zh-CN`

- Content language: `zh-CN`

- Documentation version: `1.0.0-rc.0`

- Source: [pdfs/features/import-export.zh-CN.mdx](https://github.com/dream-num/documentation/blob/dev/content/guides/pdfs/features/import-export.zh-CN.mdx)

---

下方 API 需要配套的转换后端。请先按[导入导出集成](https://docs.univer.ai/zh-CN/server/import-export.md)接好文件上传、转换、任务查询和下载，再配置前端插件。快照 API 也需要转换后端，但不要求创建协同文档。官方协同 Exchange 示例演示 Sheets，接入前请确认目标版本支持所需的文件类型。

#### Package metadata

```json
{
  "preset": [],
  "plugins": [
    {
      "client": "@univerjs-pro/exchange-client",
      "locale": "@univerjs-pro/exchange-client/locale/zh-CN",
      "style": "@univerjs-pro/exchange-client/lib/index.css",
      "facade": "@univerjs-pro/exchange-client/facade"
    },
    {
      "client": "@univerjs-pro/pdfs-exchange-client",
      "locale": "@univerjs-pro/pdfs-exchange-client/locale/zh-CN",
      "facade": "@univerjs-pro/pdfs-exchange-client/facade"
    }
  ],
  "license": true,
  "server": true
}
```

## 支持的格式

下表列出本页前端 API 支持的格式。Office 文件转换还需要后端支持对应的文档类型和格式。

| 导入     | 导出     |
| ------ | ------ |
| `.pdf` | `.pdf` |

这些 API 导入、导出的都是 PDF 文件，不提供将 PDF 内容转为 DOCX、XLSX 或 PPTX 的功能。浏览器打印是独立流程。

## 选择数据路径

| 路径    | 是否持久化服务器单元 | 是否需要协作 | 转换服务器 |
| ----- | :--------: | :----: | :---: |
| 单元 ID |      是     |    是   |   必需  |
| 快照    |      否     |    否   |   必需  |

### 导入为持久化单元

输入可以是浏览器 `File` 或 URL。通过已配置的协作客户端加载返回的单元：

```ts
const unitId = await univerAPI.importPdfToUnitIdAsync(file)

if (unitId) {
  const pdf = await univerAPI.getCollaboration().loadPdfAsync(unitId)
  console.log(pdf?.getName())
}
```

### 导入为快照

对于不应作为转换任务一部分持久化的本地 PDF 单元，请使用快照导入：

```ts
const snapshot = await univerAPI.importPdfToSnapshotAsync(file)

if (snapshot) {
  univerAPI.createPdf(snapshot)
}
```

### 导出和下载

按 ID 导出持久化单元：

```ts
const file = await univerAPI.exportPdfByUnitIdAsync(unitId)

if (file) {
  univerAPI.downloadFile(file, 'project-brief', 'pdf')
}
```

无需先持久化即可导出当前本地快照：

```ts
const pdf = univerAPI.getActivePdf()

if (pdf) {
  const file = await univerAPI.exportPdfBySnapshotAsync(pdf.save())

  if (file) {
    univerAPI.downloadFile(file, pdf.getName(), 'pdf')
  }
}
```

这些 API 返回 `File`。浏览器打印是独立的工作流，会打开系统打印对话框；请参阅[打印](https://docs.univer.ai/zh-CN/guides/pdfs/features/print.md)。
