ComPDFKit Web SDK shows, annotates, fills, signs and edits PDF documents in the browser.
This reference covers everything the WebViewer function and the instance it returns expose.
npm install @compdfkit_pdf_sdk/webviewer
Copy the package's dist/ folder (the viewer UI) to a webviewer/ folder that your site serves as static files, for example:
cp -a ./node_modules/@compdfkit_pdf_sdk/webviewer/dist/. ./public/webviewer
The path option is the URL of the folder that contains webviewer/. With path: '/', the viewer loads /webviewer/index.html and its worker from /webviewer/lib/.
import WebViewer from '@compdfkit_pdf_sdk/webviewer'
const instance = await WebViewer({
path: '/',
licenseKey: 'YOUR_LICENSE_KEY',
initialDoc: '/files/sample.pdf',
}, document.getElementById('viewer')!)
WebViewer(options, element) mounts the viewer inside element and resolves the instance once the UI is ready.
See WebViewerOptions for every option, and WebViewerError for the errors it rejects with.
The instance ( WebViewerInstance) has two parts:
| Part | What it is for |
|---|---|
instance.UI |
The viewer's interface: loading documents, showing and hiding elements, panels, headers, tools, popups, search, printing, themes and languages. Members are grouped by topic. |
instance.Core |
The document model under the interface: the document viewer, documents and pages, annotations, form fields, tools and content editing. |
instance.Core.documentViewer and instance.Core.annotationManager are the main Core.DocumentViewer and Core.AnnotationManager of the instance.
Load another document and wait for it:
const { UI, Core } = instance
Core.documentViewer.addEventListener('documentLoaded', () => {
console.log('Pages:', Core.documentViewer.getPageCount())
})
await UI.loadDocument('/files/contract.pdf', { filename: 'contract.pdf' })
Hide parts of the interface, switch theme and language:
UI.disableElements(['printButton', 'downloadButton'])
UI.setTheme('dark')
await UI.setLanguage('en')
Pick a tool:
UI.setToolMode(Core.Tools.ToolNames.RECTANGLE)
React to annotation changes and export them as XFDF:
Core.annotationManager.addEventListener('annotationChanged', (annotations, action) => {
console.log(action, annotations.length)
})
const xfdf = await Core.annotationManager.exportAnnotations()
Download the document with its annotations:
await UI.downloadPdf({ filename: 'annotated.pdf' })