Auto Saving PDF Changes in Our JavaScript Viewer

PSPDFKit for Web allows modification of multiple domain objects. These objects include annotations, bookmarks, comments, form fields, and form field values.

Standalone

In standalone mode of Nutrient Web SDK, changes will be stored in memory until they’re exported. The same persisting steps are traversed, which means we still differentiate between saved and unsaved changes, and exported Instant JSON will only contain changes that were saved. See the guide on importing and exporting for more information.

Server-backed

When using the Nutrient Web SDK server-backed operational mode, local changes are always synced to the Document Engine backend by default. This means you can always use the backend API to interact with these objects. This auto-save behavior can be set by the Configuration#autoSaveMode configuration option.

Before the change is sent to Document Engine, we assign a stable ID (ULID). This ID is also used by Document Engine, and it allows you to track updates that happen before Document Engine responds. If you’d like to ensure a local change has been saved by Document Engine, you can use the Instance#ensureChangesSaved method:

PSPDFKit.load(configuration).then(async (instance) => {
	const createdAnnotations = await instance.create(newAnnotation);
	const [savedAnnotation] = await instance.ensureChangesSaved(
		createdAnnotations
	);
	console.log(savedAnnotation.id);
});

Configuration#autoSaveMode

If nothing else is configured, Nutrient Web SDK will have auto save enabled. This means that local changes are automatically synced. You can use Configuration#autoSaveMode to configure exactly when saving occurs.

Save mode Use case
default If Configuration#instant is true, autoSaveMode defaults to PSPDFKit.AutoSaveMode.IMMEDIATE. Otherwise, it defaults to PSPDFKit.AutoSaveMode.INTELLIGENT.
PSPDFKit.AutoSaveMode.IMMEDIATE Changes are saved whenever something changes, as long as they’re in a valid state. This is useful for real-time updates but increases server load.
PSPDFKit.AutoSaveMode.INTELLIGENT Changes are saved whenever we detect a complete operation. This merges multiple operations into one server request and saves, for example, on deselect.
PSPDFKit.AutoSaveMode.DISABLED Changes aren’t saved by default. You can use Instance#save to trigger a save manually whenever you want.

Here’s an example of how to set the autoSaveMode in the configuration object passed to PSPDFKit.load():

PSPDFKit.load({ autoSaveMode: PSPDFKit.AutoSaveMode.INTELLIGENT });

Reacting to Unsaved Changes

You can check to see if there are any unsaved changes by calling Instance#hasUnsavedChanges.

If you wish to react to save state changes, you can register listeners for the document.saveStateChange event. The emitted event contains the hasUnsavedChanges property with the current value returned by the Instance#hasUnsavedChanges method:

instance.addEventListener("document.saveStateChange", (event) => {
  console.log(`Save state changed: ${event.hasUnsavedChanges}`);
});