Skip to content

PDF Generation Template Editor

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

View on GitHub

Getting Started ​

This guide covers installing the package, deploying its files, and embedding the WebViewer into your own page.

Prerequisites ​

  • A browser with WebAssembly, Web Worker and Canvas support. ComPDFKitViewer() rejects with UNSUPPORTED_BROWSER when one of them is missing.
  • A web server that serves the WebViewer files (the UI page and the Worker / WASM files cannot be loaded from file://).
  • A license key. See Apply the License Key.

Installation ​

bash
npm install @compdfkit_pdf_sdk/webviewer

The package contains:

PathPurpose
webviewer.jsES module entry (import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer").
webviewer.global.jsUMD build for a <script> tag; defines the global ComPDFKitViewer.
dist/The UI (index.html, scripts, styles, locales/, images).
dist/lib/The Worker and WASM files (PDFWorker.js, ComPDFKit.js, ComPDFKit.wasm, fonts).

Copy dist to the static assets of your app, for example public/webviewer (and webviewer.global.js when you load it with a <script> tag):

bash
cp ./node_modules/@compdfkit_pdf_sdk/webviewer/webviewer.global.js ./
cp -a ./node_modules/@compdfkit_pdf_sdk/webviewer/dist/. ./public/webviewer

Embedding the WebViewer ​

As an ES module ​

javascript
import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer";

const viewer = document.getElementById('webviewer');

ComPDFKitViewer({
  path: '/',
  initialDoc: 'Your PDF Url',
  licenseKey: 'Input your license here'
}, viewer).then((instance) => {
  const { documentViewer } = instance.Core;

  documentViewer.addEventListener('documentLoaded', () => {
    console.log('document loaded');
  });
});

path is the URL of the folder that contains webviewer/. In the example above, the UI is loaded from /webviewer/index.html and the Worker files from /webviewer/lib/.

Via a <script> tag (UMD) ​

html
<div id="webviewer" style="width: 100%; height: 100vh;"></div>
<script src="/webviewer.global.js"></script>
<script>
  ComPDFKitViewer({
    path: '/',
    initialDoc: 'Your PDF Url',
    licenseKey: 'Input your license here'
  }, document.getElementById('webviewer')).then((instance) => {
    console.log(instance.UI, instance.Core);
  });
</script>

How embedding works ​

  1. ComPDFKitViewer(options, element) checks the options and the browser, then creates an <iframe> inside element that loads {uiPath}index.html (by default {path}webviewer/index.html). See Architecture for the query and hash parameters of the iframe URL.
  2. Inside the iframe the UI creates one instance: a Pinia store set, an i18next instance, hotkeys, the Core DocumentViewer, and the instance object ({ Core, UI }). The instance is also available as window.instance inside the iframe.
  3. If config is set, the config script runs inside the iframe before the UI is mounted.
  4. When the UI is mounted, the promise returned by ComPDFKitViewer() resolves with instance.
  5. Then initialDoc is loaded, and documentViewer fires documentLoaded. Listeners added in .then() therefore receive the first documentLoaded event.

If the UI cannot be loaded (for example a wrong path), the promise rejects with an error whose code is UI_LOAD_FAILED.

When uiPath is on another origin, the options are passed to the iframe with postMessage: options that are functions cannot be passed, and the promise resolves with undefined instead of instance.

Options reference ​

OptionPurpose
pathRequired. URL of the folder that contains webviewer/.
uiPathURL of the UI folder. Default: ${path}webviewer/.
workerPathURL of the Worker / WASM folder. Default: ${uiPath}lib/.
licenseKeyLicense key.
initialDocInitial document: a URL, Blob, File, ArrayBuffer or a data: URL.
filenameFile name of the initial document.
extensionFile extension of the initial document, for sources without one (such as a URL without a file name).
documentIdIdentifier of the initial document, kept with the document.
configURL of a config script that runs inside the iframe. See Config Files.
cssURL of a stylesheet injected into the UI.
cspNonceNonce added to the style elements the UI creates, for pages with a strict Content Security Policy.
uiConfigURL of a modular UI JSON file applied with UI.importModularComponents when the UI starts.
customA custom string passed to the UI; read it with UI.getCustomData().
theme'light' / 'dark'.
defaultLanguageInitial language, such as 'en' or 'zh-CN'.
disabledElementsdata-element values to disable when the UI starts.
annotationUserAuthor name of new annotations.
isAdminUserWhether the current user can modify all annotations. Default: true.
isReadOnlyOpen the document in read-only mode.
enableMeasurement / enableRedactionShow the measurement / redaction features.
showToolbarControlShow or hide the header.
webFontURLURL of a font folder (with fonts.json) for custom fonts.
instanceIdKey prefix of the data saved in localStorage. Default: the id of element.

The instance object ​

After initialization, instance exposes two surfaces:

javascript
instance.UI    // UI APIs (setLanguage, setTheme, disableElements, getModularHeader, …)
instance.Core  // The Core namespace plus this viewer's documentViewer and annotationManager

instance.UI is the public surface documented across these guides. Head to the UI Customization overview or the API reference to continue.

Building from source ​

The UI layer is open source. To modify it and build your own WebViewer files, you need Node.js and pnpm. The source repository is a pnpm workspace:

PackageLocationDescription
@compdfkit/webviewerpackages/webviewerThe open-source Vue 3 UI layer and the bootstrap scripts.
@compdfkit/corepackages/coreThe Core engine, used by the UI (provided as built files).
bash
pnpm install
pnpm --filter @compdfkit/webviewer dev       # dev server on http://localhost:3040
pnpm --filter @compdfkit/webviewer build     # build the UI and the bootstrap scripts into packages/webviewer/dist/
pnpm --filter @compdfkit/webviewer pack:npm  # arrange the build as the npm package (webviewer.js, webviewer.global.js, dist/) in packages/webviewer/npm/

Run the checks:

bash
pnpm --filter @compdfkit/webviewer typecheck  # type check
pnpm --filter @compdfkit/webviewer test       # unit tests (Vitest)
pnpm lint                                     # ESLint

Next steps ​