Web SDKv1.0.0

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(). 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 / exportCompleted on the viewer, and an export of the whole document clears 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. 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().

classviewer.createExporter(config?) (recommended) | new PDFExporter(document, config?)

Usage

TypeScript
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

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.

ParameterTypeDescription
documentPDFDocumentAn open PDFDocument.
config?ExportConfigThe export defaults — the same ExportConfig a viewer takes.

Export

downloadmethodsince 1.0.0
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: false drops the line. Needs export.
ParameterTypeDescription
filename?stringThe file name; 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.
  • 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().
TypeScript
const result = await viewer.createExporter().download('signed.pdf', {
  flatten: true,
  onProgress: (done, total) => setProgress(done / total),
});
console.log(result.sizeBytes);
getBlobmethodsince 1.0.0
getBlob(options?: ExportOptions): Promise<Blob>
Get exported PDF as a Blob. Needs export.
ParameterTypeDescription
options?ExportOptions{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }.
Returns Promise<Blob>
  • 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().
TypeScript
const blob = await viewer.createExporter().getBlob();
const form = new FormData();
form.append('file', blob, 'report.pdf');
getArrayBuffermethodsince 1.0.0
getArrayBuffer(options?: ExportOptions): Promise<ArrayBuffer>
Get exported PDF as an ArrayBuffer. Needs export.
ParameterTypeDescription
options?ExportOptions{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }.
Returns Promise<ArrayBuffer>
  • 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().
TypeScript
const buffer = await viewer.createExporter().getArrayBuffer({ pages: [0] });
getBase64methodsince 1.0.0
getBase64(options?: ExportOptions): Promise<string>
Get exported PDF as a base64 string. Needs export.
ParameterTypeDescription
options?ExportOptions{ flatten?, invalidateSignatures?, pages?, onProgress?, signal? }.
Returns Promise<string>
  • 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().
TypeScript
const base64 = await viewer.createExporter().getBase64();
uploadTomethodsince 1.0.0
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.
ParameterTypeDescription
urlstringWhere to send the file, e.g. an S3 presigned URL.
options?UploadOptionsEvery 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.
  • 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().
TypeScript
const { url } = await (await fetch('/api/presign')).json();
const response = await viewer.createExporter().uploadTo(url, { method: 'PUT' });
if (!response.ok) showUploadFailed(response.status);