Skip to content

PDF Generation Template Editor

Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.

View on GitHub

State Management ​

The UI state lives in Pinia stores. Each WebViewer instance creates its own Pinia and its own set of stores, so two viewers on one page never share state.

Store setup ​

When an instance is created, three stores are defined on the instance's Pinia, and a few store plugins are installed:

StoreHolds
viewerUI state: open / disabled elements, the modular components (headers, grouped items, flyouts, panels, popups), the active toolbar group, theme, language, custom modals, stamps, saved signatures, zoom list, and more.
documentMirrors of Core state: loaded documents, page counts, permissions, the current user, search query, outlines, bookmarks, layers, signature verification results.
settingsThe normalized options passed to ComPDFKitViewer() (without the license key) and feature flags.

Plugins:

  • persistence — saves selected viewer keys to localStorage (or keeps them in memory when storage is not available).
  • UI events — fires instance.UI.Events such as THEME_CHANGED, LANGUAGE_CHANGED, VISIBILITY_CHANGED and TOOLBAR_GROUP_CHANGED when the related state changes.
  • reset — restores per-document state when a document is closed.

How instance.UI interacts with the stores ​

The stores are internal. Integrators read and change UI state through instance.UI:

javascript
const { UI } = instance;

UI.openElement('tabPanel');                 // viewer store: open elements
console.log(UI.isElementOpen('tabPanel'));  // true

UI.setToolbarGroup('toolbarGroup-Annotate'); // viewer store: active toolbar group
UI.setTheme('dark');                        // viewer store: theme

UI.addEventListener(UI.Events.VISIBILITY_CHANGED, (element, isVisible) => {
  console.log(element, isVisible);
});

The document store only mirrors the Core. To change documents or annotations, call the Core (instance.Core.documentViewer, annotationManager); the event listeners update the store.

Persistence ​

The viewer store saves these keys: the current language, custom stamps, and saved signatures / initials / digital signatures. They are stored under the key prefix cpdf:<instanceId>:, where instanceId is the instanceId option, or the id of the container element, or default.

User settings such as language, theme and keyboard shortcuts can also be exported and imported:

javascript
const settings = instance.UI.exportUserSettings();
console.log(settings.language, settings.theme);
instance.UI.importUserSettings({ theme: 'light' });