Section:

Opening a document from the Salesforce Database

If you have a document uploaded into your instance of Salesforce, it will have a record in the database in the ContentVersion table. The ContentVersion object allows for versioning of the ContentDocument which is linked to a Salesforce record via the ContentDocumentLink.

You can use the following Apex code snippet to pass the relevant ContentVersion record to your WebViewer instance:

ContentVersionController.cls

Apex

1// force-app\main\default\classes\PDFTron_ContentVersionController.cls
2
3//snipped for brevity
4@AuraEnabled(Cacheable=true)
5public static List<ContentVersionWrapper> getAttachments(String recordId){
6 try {
7 List<String> cdIdList = new List<String> ();
8 List<ContentVersionWrapper> cvwList = new List<ContentVersionWrapper> ();
9
10 //Find links between record & document
11 for(ContentDocumentLink cdl :
12 [ SELECT id, ContentDocumentId, ContentDocument.LatestPublishedVersionId
13 FROM ContentDocumentLink
14 WHERE LinkedEntityId = :recordId ]) {
15 cdIdList.add(cdl.ContentDocumentId);
16 }
17 //Use links to get attachments
18 for(ContentVersion cv :
19 [ SELECT Id, Title,FileExtension, VersionData
20 FROM ContentVersion
21 WHERE ContentDocumentId IN :cdIdList
22 AND IsLatest = true ]) {
23 cvwList.add(new ContentVersionWrapper(cv));
24 }
25
26 return cvwList;
27 } catch (Exception e) {
28 throw new AuraHandledException(e.getMessage());
29 }
30}
31
32//sample wrapper class
33public class ContentVersionWrapper {
34 @AuraEnabled
35 public String name {get; set;}
36 @AuraEnabled
37 public String body {get; set;}
38
39 public ContentVersionWrapper(ContentVersion contentVer) {
40 this.name = contentVer.Title + '.' + contentVer.FileExtension;
41 this.body = EncodingUtil.base64Encode(contentVer.VersionData);
42 }
43}

Converting a base64 string to blob inside your WebViewer LWC

JavaScript

1function _base64ToArrayBuffer(base64) {
2 var binary_string = window.atob(base64);
3 var len = binary_string.length;
4 var bytes = new Uint8Array( len );
5 for (var i = 0; i < len; i++) {
6 bytes[i] = binary_string.charCodeAt(i);
7 }
8 return bytes.buffer;
9}

Then you can use the function to build your blob and pass it to WebViewer like so:

JavaScript

1//snipped for brevity
2import { registerListener, unregisterAllListeners } from 'c/pubsub';
3
4connectedCallback() {
5 registerListener('blobSelected', this.handleBlobSelected, this);
6 //register other listeners here
7}
8
9disconnectedCallback() {
10 //unregister all listeners here
11}
12
13handleBlobSelected(record) {
14 record = JSON.parse(record);
15
16 let blobby = new Blob([_base64ToArrayBuffer(record.body)], {
17 type: mimeTypes[record.FileExtension]
18 });
19
20 const payload = {
21 blob: blobby,
22 extension: record.cv.FileExtension,
23 filename: record.cv.Title + "." + record.cv.FileExtension,
24 documentId: record.cv.Id
25 };
26 this.iframeWindow.postMessage({type: 'OPEN_DOCUMENT_BLOB', payload} , '*');
27 }

config_apex.js

You can use

JavaScript (v8.0+)

1//snipped for brevity
2window.addEventListener("message", receiveMessage, false);
3
4function receiveMessage(event) {
5 if (event.isTrusted && typeof event.data === 'object') {
6 switch (event.data.type) {
7 case 'OPEN_DOCUMENT_BLOB':
8 const { blob, extension, filename, documentId } = event.data.payload;
9 instance.loadDocument(blob, { extension, filename, documentId })
10 break;
11 default:
12 break;
13 }
14 }
15}

JavaScript (v7.0+)

1//snipped for brevity
2window.addEventListener("message", receiveMessage, false);
3
4function receiveMessage(event) {
5 if (event.isTrusted && typeof event.data === 'object') {
6 switch (event.data.type) {
7 case 'OPEN_DOCUMENT_BLOB':
8 const { blob, extension, filename, documentId } = event.data.payload;
9 event.target.readerControl.loadDocument(blob, { extension, filename, documentId })
10 break;
11 default:
12 break;
13 }
14 }
15}

Code sample

You can find a full code sample that implements opening documents in Salesforce on our Github repository.

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales