PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
This guide covers installing the package, deploying its files, and embedding the WebViewer into your own page.
ComPDFKitViewer() rejects with UNSUPPORTED_BROWSER when one of them is missing.file://).npm install @compdfkit_pdf_sdk/webviewerThe package contains:
| Path | Purpose |
|---|---|
webviewer.js | ES module entry (import ComPDFKitViewer from "@compdfkit_pdf_sdk/webviewer"). |
webviewer.global.js | UMD 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):
cp ./node_modules/@compdfkit_pdf_sdk/webviewer/webviewer.global.js ./
cp -a ./node_modules/@compdfkit_pdf_sdk/webviewer/dist/. ./public/webviewerimport 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/.
<script> tag (UMD) <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>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.DocumentViewer, and the instance object ({ Core, UI }). The instance is also available as window.instance inside the iframe.config is set, the config script runs inside the iframe before the UI is mounted.ComPDFKitViewer() resolves with instance.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.
| Option | Purpose |
|---|---|
path | Required. URL of the folder that contains webviewer/. |
uiPath | URL of the UI folder. Default: ${path}webviewer/. |
workerPath | URL of the Worker / WASM folder. Default: ${uiPath}lib/. |
licenseKey | License key. |
initialDoc | Initial document: a URL, Blob, File, ArrayBuffer or a data: URL. |
filename | File name of the initial document. |
extension | File extension of the initial document, for sources without one (such as a URL without a file name). |
documentId | Identifier of the initial document, kept with the document. |
config | URL of a config script that runs inside the iframe. See Config Files. |
css | URL of a stylesheet injected into the UI. |
cspNonce | Nonce added to the style elements the UI creates, for pages with a strict Content Security Policy. |
uiConfig | URL of a modular UI JSON file applied with UI.importModularComponents when the UI starts. |
custom | A custom string passed to the UI; read it with UI.getCustomData(). |
theme | 'light' / 'dark'. |
defaultLanguage | Initial language, such as 'en' or 'zh-CN'. |
disabledElements | data-element values to disable when the UI starts. |
annotationUser | Author name of new annotations. |
isAdminUser | Whether the current user can modify all annotations. Default: true. |
isReadOnly | Open the document in read-only mode. |
enableMeasurement / enableRedaction | Show the measurement / redaction features. |
showToolbarControl | Show or hide the header. |
webFontURL | URL of a font folder (with fonts.json) for custom fonts. |
instanceId | Key prefix of the data saved in localStorage. Default: the id of element. |
instance object After initialization, instance exposes two surfaces:
instance.UI // UI APIs (setLanguage, setTheme, disableElements, getModularHeader, …)
instance.Core // The Core namespace plus this viewer's documentViewer and annotationManagerinstance.UI is the public surface documented across these guides. Head to the UI Customization overview or the API reference to continue.
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:
| Package | Location | Description |
|---|---|---|
@compdfkit/webviewer | packages/webviewer | The open-source Vue 3 UI layer and the bootstrap scripts. |
@compdfkit/core | packages/core | The Core engine, used by the UI (provided as built files). |
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:
pnpm --filter @compdfkit/webviewer typecheck # type check
pnpm --filter @compdfkit/webviewer test # unit tests (Vitest)
pnpm lint # ESLint