# PDFExporter

Writes the document out: to a download, a `Blob`, an `ArrayBuffer`, a base64 string, or an upload. Every method needs the `export` feature.

**Obtain the exporter from [viewer.createExporter()](https://kernstra.com/docs/web-sdk/api/pdfviewer/createexporter).** It carries what the viewer holds — the form values and signatures the user entered — and, before it reads anything, writes in what the screen holds and the document does not yet: Edit Text's pending edits (the tool stays active), an annotation selected on the page, the entry being typed into a formatted field. When pending edits cannot be written the export rejects with `INVALID_STATE` instead of going out without them; when the document was reloaded under it (an undo, a commit, a page operation) it rejects with `INVALID_STATE` too — export again. It reports [exportStarted](https://kernstra.com/docs/web-sdk/api/events#ev-exportstarted) / [exportCompleted](https://kernstra.com/docs/web-sdk/api/events#ev-exportcompleted) on the viewer, and an export of the whole document clears [hasUnsavedChanges](https://kernstra.com/docs/web-sdk/api/pdfviewer/hasunsavedchanges). `new PDFExporter(document, config?)` is the document **as loaded**, with none of that: for a headless export, or when you genuinely want the untouched file; it reports to no viewer.

`download`, `getBlob`, `getArrayBuffer` and `getBase64` accept `options?: ExportOptions` — `{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }`; `uploadTo` takes `UploadOptions`: the same options, and the request's `method` and `headers`. A `flatten` left out comes from [ExportConfig](https://kernstra.com/docs/web-sdk/config/export). A page subset is a new document and never carries a signature. Flattening/saving shows a determinate, cancelable card once the work has run 300 ms; `onProgress(done, total)` and `signal` serve a progress indicator of your own.

Each method rejects with `FEATURE_NOT_LICENSED` when the license lacks `export`, `INVALID_ARGUMENT` when `pages` is empty or names a page the document does not have, `INVALID_STATE` when `flatten` is asked of a signed document without `invalidateSignatures`, `PAGE_ERROR` when a page cannot be loaded for flattening, `EXPORT_FAILED` when the document cannot be written, `ABORTED` when `signal` aborts, and `DOCUMENT_CLOSED` after `doc.close()`.

- Kind: class
- Access: `viewer.createExporter(config?) (recommended) | new PDFExporter(document, config?)`

## Usage

```ts
const exporter = viewer.createExporter();
const { sizeBytes } = await exporter.download('report.pdf', { flatten: true });

// Only if you want the document as loaded, ignoring unsaved user input:
const raw = new PDFExporter(doc, { flatten: false });
const blob = await raw.getBlob();
```

## Constructor

```ts
new PDFExporter(document: PDFDocument, config?: ExportConfig)
```

Exports the document **as loaded**: without the form values and signatures a viewer holds, without pending Edit Text edits, and reporting to no viewer. For the document as the user sees it, use `viewer.createExporter(config?)`, whose defaults are the viewer's `export` option.

| Parameter | Type | Description |
| --- | --- | --- |
| `document` | `PDFDocument` | An open `PDFDocument`. |
| `config?` | `ExportConfig` | The export defaults — the same [ExportConfig](https://kernstra.com/docs/web-sdk/config/export) a viewer takes. |

## Export

### `download`

```ts
download(filename?: string, options?: ExportOptions): Promise<DownloadResult>
```

Download PDF as a file. A download confirms itself in one line ("Saved · 6 pages, 1.2 MB"); [export.successToast](https://kernstra.com/docs/web-sdk/config/export/successtoast)`: false` drops the line. Needs `export`.

- Kind: method
- Since: 1.0.0

| Parameter | Type | Description |
| --- | --- | --- |
| `filename?` | `string` | The file name; [export.defaultFilename](https://kernstra.com/docs/web-sdk/config/export/defaultfilename) when omitted. |
| `options?` | `ExportOptions` | `{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }`. |

Returns `Promise<DownloadResult>`: `{ sizeBytes, pdfA? }` — the file's size in bytes and, for a document that claims PDF/A, whether the file saved still claims it: `'kept'` for a document nobody changed, `'dropped'` for a changed one, or a page range.

Throws:

- `FEATURE_NOT_LICENSED` when the license lacks `export`.
- `INVALID_ARGUMENT` when `pages` is empty or names a page the document does not have.
- `INVALID_STATE` when `flatten` is asked of a signed document without `invalidateSignatures`; from a viewer's exporter, when pending edits cannot be written or the document was reloaded under it.
- `PAGE_ERROR` when a page cannot be loaded for flattening.
- `EXPORT_FAILED` when the document cannot be written.
- `ABORTED` when `signal` aborts.
- `DOCUMENT_CLOSED` after `doc.close()`.

```ts
const result = await viewer.createExporter().download('signed.pdf', {
  flatten: true,
  onProgress: (done, total) => setProgress(done / total),
});
console.log(result.sizeBytes);
```

Page: https://kernstra.com/docs/web-sdk/api/pdfexporter/download

### `getBlob`

```ts
getBlob(options?: ExportOptions): Promise<Blob>
```

Get exported PDF as a `Blob`. Needs `export`.

- Kind: method
- Since: 1.0.0

| Parameter | Type | Description |
| --- | --- | --- |
| `options?` | `ExportOptions` | `{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }`. |

Returns `Promise<Blob>`

Throws:

- `FEATURE_NOT_LICENSED` when the license lacks `export`.
- `INVALID_ARGUMENT` when `pages` is empty or names a page the document does not have.
- `INVALID_STATE` when `flatten` is asked of a signed document without `invalidateSignatures`; from a viewer's exporter, when pending edits cannot be written or the document was reloaded under it.
- `PAGE_ERROR` when a page cannot be loaded for flattening.
- `EXPORT_FAILED` when the document cannot be written.
- `ABORTED` when `signal` aborts.
- `DOCUMENT_CLOSED` after `doc.close()`.

```ts
const blob = await viewer.createExporter().getBlob();
const form = new FormData();
form.append('file', blob, 'report.pdf');
```

Page: https://kernstra.com/docs/web-sdk/api/pdfexporter/getblob

### `getArrayBuffer`

```ts
getArrayBuffer(options?: ExportOptions): Promise<ArrayBuffer>
```

Get exported PDF as an `ArrayBuffer`. Needs `export`.

- Kind: method
- Since: 1.0.0

| Parameter | Type | Description |
| --- | --- | --- |
| `options?` | `ExportOptions` | `{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }`. |

Returns `Promise<ArrayBuffer>`

Throws:

- `FEATURE_NOT_LICENSED` when the license lacks `export`.
- `INVALID_ARGUMENT` when `pages` is empty or names a page the document does not have.
- `INVALID_STATE` when `flatten` is asked of a signed document without `invalidateSignatures`; from a viewer's exporter, when pending edits cannot be written or the document was reloaded under it.
- `PAGE_ERROR` when a page cannot be loaded for flattening.
- `EXPORT_FAILED` when the document cannot be written.
- `ABORTED` when `signal` aborts.
- `DOCUMENT_CLOSED` after `doc.close()`.

```ts
const buffer = await viewer.createExporter().getArrayBuffer({ pages: [0] });
```

Page: https://kernstra.com/docs/web-sdk/api/pdfexporter/getarraybuffer

### `getBase64`

```ts
getBase64(options?: ExportOptions): Promise<string>
```

Get exported PDF as a base64 string. Needs `export`.

- Kind: method
- Since: 1.0.0

| Parameter | Type | Description |
| --- | --- | --- |
| `options?` | `ExportOptions` | `{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }`. |

Returns `Promise<string>`

Throws:

- `FEATURE_NOT_LICENSED` when the license lacks `export`.
- `INVALID_ARGUMENT` when `pages` is empty or names a page the document does not have.
- `INVALID_STATE` when `flatten` is asked of a signed document without `invalidateSignatures`; from a viewer's exporter, when pending edits cannot be written or the document was reloaded under it.
- `PAGE_ERROR` when a page cannot be loaded for flattening.
- `EXPORT_FAILED` when the document cannot be written.
- `ABORTED` when `signal` aborts.
- `DOCUMENT_CLOSED` after `doc.close()`.

```ts
const base64 = await viewer.createExporter().getBase64();
```

Page: https://kernstra.com/docs/web-sdk/api/pdfexporter/getbase64

### `uploadTo`

```ts
uploadTo(url: string, options?: UploadOptions): Promise<Response>
```

Upload the exported PDF (e.g. to an S3 presigned URL). Sends `Content-Type: application/pdf` unless `headers` overrides it. Needs `export`.

- Kind: method
- Since: 1.0.0

| Parameter | Type | Description |
| --- | --- | --- |
| `url` | `string` | Where to send the file, e.g. an S3 presigned URL. |
| `options?` | `UploadOptions` | Every `ExportOptions` field, and the request's `method` (default `'PUT'`), `headers` (`Content-Type: application/pdf` is sent unless these name one, in any case) and `credentials` (`fetch`'s; `same-origin` by default, `include` for a cookie-authenticated endpoint on another origin). The `signal` cancels the upload too. |

Returns `Promise<Response>`: The `fetch` `Response`. Does **not** reject on an HTTP error status — check `response.ok`.

Throws:

- `NETWORK_ERROR` when the request could not be sent at all (the address, the network, or a CORS refusal: the response must allow your origin).
- `ABORTED` when `signal` aborts, during the export or the upload.
- `FEATURE_NOT_LICENSED` when the license lacks `export`.
- `INVALID_ARGUMENT` when `pages` is empty or names a page the document does not have, or when `headers` holds a name or value a request cannot carry.
- `INVALID_STATE` when `flatten` is asked of a signed document without `invalidateSignatures`; from a viewer's exporter, when pending edits cannot be written or the document was reloaded under it.
- `PAGE_ERROR` when a page cannot be loaded for flattening.
- `EXPORT_FAILED` when the document cannot be written.
- `DOCUMENT_CLOSED` after `doc.close()`.

```ts
const { url } = await (await fetch('/api/presign')).json();
const response = await viewer.createExporter().uploadTo(url, { method: 'PUT' });
if (!response.ok) showUploadFailed(response.status);
```

Page: https://kernstra.com/docs/web-sdk/api/pdfexporter/uploadto

Web page: https://kernstra.com/docs/web-sdk/api/pdfexporter
