Univer PDFs API
Create, find, save, enter, and unload PDF units through the PDF Facade API.
The PDF Facade adds PDF units and page entry points to FUniver. Import the side-effect entry before creating the API object.
Enable the PDF Facade
import { FUniver } from '@univerjs/core/facade'import '@univerjs-pro/pdfs/facade'const univerAPI = FUniver.newAPI(univer)Register UniverPdfsPlugin on the Univer instance so the application can host PDF units.
Create, find, and save PDF units
When document is omitted, createPdf() creates a unit with one blank A4 page. To restore a unit, pass a snapshot previously returned by save().
const pdf = univerAPI.createPdf({ id: 'project-brief', name: 'Project brief',})const activePdf = univerAPI.getActivePdf()const samePdf = univerAPI.getPdf(pdf.getId())const snapshot = pdf.save()Use Facade methods or commands to update a running PDF. A saved snapshot is detached persistence data.
Append pages from another PDF
FPdf.appendPdf(source) appends every page of an IPdfUnitData snapshot and records the change in undo and redo history. It returns false when the PDF is no longer open.
The exchange facade adds methods for a PDF that is already open. univerAPI.appendPdfAsync(file, unitId?) imports the file and appends its pages to the given PDF, or to the active PDF when the unit ID is omitted. It resolves to false when there is no target or the pages cannot be appended. univerAPI.exportPdfAsync(unitId?) commits text that is still being edited on the canvas and exports the open PDF. It resolves to undefined when the commit fails or the document is no longer open.
These exchange methods are added to FUniver by @univerjs-pro/pdfs-exchange-client/facade:
const pdf = univerAPI.getActivePdf()pdf?.appendPdf(importedSnapshot)await univerAPI.appendPdfAsync(file) // current PDFawait univerAPI.appendPdfAsync(file, unitId) // specific PDFconst exported = await univerAPI.exportPdfAsync()Enter a page
FPdfPage is the common entry point for a page's editable overlays and imported native text:
const firstPage = pdf.getPageByIndex(0) ?? pdf.insertPage()const elements = firstPage.getElements()const nativeText = firstPage.getTextSpans()For focused operations, continue with Pages and Elements, Text and Lists, Tables, Images and Dividers, or Annotations and Native Text.
Unload a PDF unit
Unload a PDF unit by ID when it is no longer needed. Dispose the root univer instance only when the entire application is destroyed.
const pdf = univerAPI.getActivePdf()if (pdf) { univerAPI.disposeUnit(pdf.getId())}For exact signatures and return types, see the PDF Facade reference.
How is this guide?