Implement WebViewer in Salesforce as a Lightning Web Component

This sample is demonstrating how to integrate the Apryse WebViewer into Salesforce as a Lightning Web Component. For more information, see this guide.

WebViewer provides a slick out-of-the-box responsive UI that enables you to view, annotate and manipulate PDFs and other document types inside any web project.

Click the button below to view the full project in GitHub.

1import { LightningElement, track, wire } from 'lwc';
2// import { ShowToastEvent } from 'lightning/platformShowToastEvent';
3import { CurrentPageReference } from 'lightning/navigation';
4import { loadScript } from 'lightning/platformResourceLoader';
5import libUrl from '@salesforce/resourceUrl/lib';
6import myfilesUrl from '@salesforce/resourceUrl/myfiles';
7import { registerListener, unregisterAllListeners } from 'c/pubsub';
8
9export default class PdftronWebViewer extends LightningElement {
10 fullAPI = true;
11 @wire(CurrentPageReference) pageRef;
12
13 connectedCallback() {
14 registerListener('fileSelected', this.handleFileSelected, this);
15 }
16
17 disconnectedCallback() {
18 unregisterAllListeners(this);
19 }
20
21 handleFileSelected(file) {
22 this.iframeWindow.postMessage({type: 'OPEN_DOCUMENT', file: file}, '*')
23 }
24
25 divHeight = 600;
26 uiInitialized = false;
27 renderedCallback() {
28 if (this.uiInitialized) {
29 return;
30 }
31 this.uiInitialized = true;
32
33 Promise.all([
34 loadScript(this, libUrl + '/webviewer.min.js')
35 ])
36 .then(() => {
37 this.initUI();
38 })
39 .catch(console.error);
40 }
41 initUI() {
42 const myObj = {
43 libUrl: libUrl,
44 fullAPI: this.fullAPI,
45 namespacePrefix: '',
46 };
47
48 const url = myfilesUrl + '/webviewer-demo-annotated.pdf';
49 // var url = myfilesUrl + '/webviewer-demo-annotated.xod';
50 // var url = myfilesUrl + '/word.docx';
51
52 const viewerElement = this.template.querySelector('div')
53 const viewer = new WebViewer.Iframe({
54 path: libUrl, // path to the PDFTron 'lib' folder on your server
55 custom: JSON.stringify(myObj),
56 backendType: 'ems',
57 config: myfilesUrl + '/config_apex.js',
58 fullAPI: this.fullAPI,
59 enableFilePicker: this.enableFilePicker,
60 enableRedaction: this.enableRedaction,
61 enableMeasurement: this.enableMeasurement,
62 enableOptimizedWorkers: true,
63 // l: 'YOUR_LICENSE_KEY_HERE',
64 }, viewerElement);
65
66 viewerElement.addEventListener('ready', () => {
67 this.iframeWindow = viewerElement.querySelector('iframe').contentWindow
68 })
69
70 }
71}
72

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales