Web SDKv1.0.0

Helpers.createPasswordPrompt()

"This document is protected · Enter the password to open it." — shown in place when a load rejects with PASSWORD_ERROR. A wrong password (resolve false from onUnlock) shakes the field and says so.
static methodDocument statessince 1.0.0

Signature

createPasswordPrompt(container: HTMLElement, options: PasswordPromptOptions): PasswordPrompt

Parameters

ParameterTypeDescription
containerHTMLElementWhere the prompt is mounted.
optionsPasswordPromptOptions{ onUnlock, onCancel?, name?, locale?, icons? }. onUnlock(password) returns true / false directly or as a promise — false shakes the field and says the password was wrong; onCancel() when the user gives up; name is the file's name to show; locale and icons as for createDropZone.

Returns

PasswordPrompt { element, destroy() }.

Example

TypeScript
import { createPasswordPrompt, createLoadError, PDFDocument, SDKError } from '@kernstra/pdf-web-sdk';

try {
  doc = await PDFDocument.fromFile(file);
} catch (err) {
  if (err instanceof SDKError && err.code === 'PASSWORD_ERROR') {
    const prompt = createPasswordPrompt(container, {
      name: file.name,
      onUnlock: async (password) => {
        try {
          doc = await PDFDocument.fromFile(file, { password });
          prompt.destroy();
          return true;
        } catch {
          return false; // shakes the field
        }
      },
    });
  } else {
    createLoadError(container, { error: err, onRetry: () => openPdf(file) });
  }
}

More in Helpers