Skip to Content
API Referencepdf-export-client

@stesura/pdf-export-client

Browser side of PDF export: the toolbar button, and the shell for the export page the server loads. The server side is @stesura/pdf-export-server; the PDF export guide covers setup.

Toolbar

pdfUiExtension adds the PDF export group to the toolbar’s Export tab:

import { pdfUiExtension } from '@stesura/pdf-export-client' <StesuraEditor uiExtensions={[pdfUiExtension]} />

For a custom endpoint or file name, mount PdfExportGroup (the button in a labelled toolbar group, which is what pdfUiExtension mounts) or the bare PdfExportButton instead. The button posts { doc, fileName } to the endpoint and downloads the PDF; failures, including a non-PDF response, show a toast unless you pass onError. Both take the same props:

PdfExportButtonProps

NameTypeDefault
endpointstring

Export endpoint. Defaults to ${NEXT_PUBLIC_BASE_PATH}/api/export/pdf; an absolute URL points at a standalone export service.

fileNamestring

Download file name. Defaults to document.pdf.

onError(error: Error) => void

Called instead of the default toast when an export fails.

Export page (/shell)

ExportEditorShell ({ children: ReactNode }) wraps the read-only editor on the export route. It provides the clipping viewport, publishes window.__stesuraPdfExport (ExportApi) and flags <body> with data-export-ready or data-export-error once the layout has settled. The editor stays in your app, so it keeps your plugins and extensions.

import { ExportEditorShell } from '@stesura/pdf-export-client/shell' <ExportEditorShell> <StesuraEditor printMode editable={false} toolbar={false} /> </ExportEditorShell>

It is on its own subpath because it loads the print stylesheet, whose @page rule can’t be scoped and must only load on the export route. Don’t import @stesura/pdf-export-client/print.css yourself.

The server injects the document as window.__STESURA_EXPORT_DOC__ (an ExportPayload) before any page script runs; importing the shell types that global. If the payload is missing, set document.body.dataset.exportError rather than rendering nothing, or the server waits until its timeout. The guide lists the rest of the export page’s requirements.

Protocol types (/types)

The contract between the export page and the server, from @stesura/pdf-export-client/types. This entry point has no React or browser dependencies, so the server can import it.

ExportApi

NameTypeDefault
status"pending" | "ready" | "error"
errorstring
manifestExportPageInfo[]
outlineExportOutlineNode[]

Heading tree for the PDF outline/bookmarks panel.

insertionsExportInsertion[]

Embedded PDF files to splice in, in document order.

languagestring

BCP-47 tag of the document’s language, stamped as the PDF’s /Lang.

warningsstring[]

Non-fatal losses (e.g. an image that never loaded), logged by the server.

timingsRecord<string, number>

Client-side phase durations in ms (fonts, images, settle, links).

showPage(index: number) => { width: number; height: number; }

Positions page index in the clipping viewport; returns the size to print at.

showRun(start: number, count: number) => { width: number; height: number; advance: number; } | null

Positions count consecutive same-size pages of one section to print as one tall sheet, page tops advance apart. Null when the pages are not uniformly spaced; the server then falls back to showPage.

ExportPayload

NameTypeDefault
docJSONContent
fileNamestring

ExportPageInfo

NameTypeDefault
sectionIndexnumber

Index of the owning .pm_section in DOM order.

pageIndexnumber

0-based page index within the section (matches data-page-index).

widthnumber

Page width in CSS px (section’s --section-width).

heightnumber

Page height in CSS px (section’s --section-page-height).

NameTypeDefault
kind"url" | "internal"
rectExportLinkRect

ExportLinkRect

NameTypeDefault
xnumber
ynumber
widthnumber
heightnumber

ExportOutlineNode

NameTypeDefault
titlestring
pageIndexnumber

Manifest / merged-PDF page index of the heading.

ynumber

Heading top in page-local CSS px from the page top.

childrenExportOutlineNode[]
Last updated on