global class CallPdfButlerWithPreviewController {
@AuraEnabled
global static String convert(Id recordId) {
//see https://www.pdfbutler.com/files/api/cadmuscore/ConvertController.html#ConvertController.convertWithWrapper
cadmus_core.ConvertController.ConvertDataModel cdm = new cadmus_core.ConvertController.ConvertDataModel();
cdm.objectId = recordId; //this must be the Id of the record you want to generate from eg an Opportunity Id
//Select the DocConfig via the "cadmus_core__CustomerDocumentConfigId__c", this is unique over all sandboxes and PROD. So we can hardcode it
cadmus_core__Doc_Config__c dc = [SELECT Id FROM cadmus_core__Doc_Config__c
WHERE cadmus_core__CustomerDocumentConfigId__c = 'a6a612df-b30a-41aa-aa09-38119d42e904'];
cdm.docConfigId = dc.Id;
cadmus_core.DocGenerationWrapper wrapper = cadmus_core.ConvertController.convertWithWrapper(cdm);
//Here we put the title and the base64 into a JSON. For demo purposes, we string-concatenate the JSON together.
//Do not forget to use "EncodingUtil.base64Encode" on the base64!
return '{"title":"' + wrapper.response.metadata.targetName + '","base64":"' + EncodingUtil.base64Encode(wrapper.response.base64) + '"}';
}
@AuraEnabled
global static String pack(Id recordId, String longPackId) {
//see https://www.pdfbutler.com/files/api/cadmuscore/ConvertController.html#ConvertController.convertWithWrapper
cadmus_core.ConvertController.ConvertDataModel cdm = new cadmus_core.ConvertController.ConvertDataModel();
cdm.objectId = recordId; //this must be the Id of the record you want to generate from eg an Opportunity Id
//Select the Pack via the "cadmus_core__Customer_Pack_Id__c", this is unique over all sandboxes and PROD. So we can hardcode it
cadmus_core__Pack__c pack = [SELECT Id FROM cadmus_core__Pack__c
WHERE cadmus_core__Customer_Pack_Id__c = :longPackId];
cdm.packId = pack.Id;
cadmus_core.ConvertController.convertWithWrapper(cdm);
//For demo purposes, we do not care about the response
return 'OK';
}
}
|
LWC - JavaScript Controller:
import { api,track,LightningElement } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import iframeResource from '@salesforce/resourceUrl/cadmus_core__PdfButlerPdfPreviewer';
import logoImageResource from '@salesforce/resourceUrl/cadmus_core__logo32x32';
import convert from '@salesforce/apex/CallPdfButlerWithPreviewController.convert';
import pack from '@salesforce/apex/CallPdfButlerWithPreviewController.pack';
export default class GenerateDocument extends LightningElement {
@api recordId;
@api logoImageURL = logoImageResource + '?';
@api iframeURL = iframeResource + '/web/viewer.html#zoom=page-fit';
@api modalClass = "slds-modal slds-fade-in-open slds-modal_large";
// Title is set document title and base64 is set document data generated by Apex class
@track title;
@track base64;
@track isModalOpen = false;
openModal(event) {
// to open modal set isModalOpen tarck value as true
this.isModalOpen = true;
// extecute Apex method when the modal is opened
convert({recordId:this.recordId}).then((result)=>{
this.title = result.title;
this.base64 = result.base64;
this.template.querySelector('iframe').contentWindow.postMessage(this.base64, '*');
});
}
closeModal() {
// to close modal set isModalOpen tarck value as false
this.isModalOpen = false;
}
download(){
// extecute download pdf file when click download button
const linkSource = `data:application/pdf;base64,` + this.base64;
const downloadLink = document.createElement("a");
downloadLink.href = linkSource;
downloadLink.download = this.title;
downloadLink.click();
downloadLink.remove();
}
runPack(){
// extecute pack process when click Send an email button
pack({recordId:this.recordId}).then(()=>{
// show the notification about the process result
this.showNotification('SUCCESS', 'The process was successfully completed!', 'success')
})
.catch(e => {
this.showNotification('ERROR', 'The process ended abnormally. Contact your system administrator.', 'error')
console.error(e)
});
}
// the method to show the notification
showNotification(title, message, variant) {
const event = new ShowToastEvent({
title: title,
message: message,
variant: variant,
});
this.dispatchEvent(event);
}
}
|