Hi Jean,
Below is one example of how you can achieve it:
1 - Generate the PDF via pdf-lib. In my example, I already have a PDF Base64 (raw).
2 - Call the callback function and pass the Base64 to it.
function GeneratePDFClient(five, context, result) {
//Base64-encoded PDF that includes metadata indicating it was produced by pdf-lib.
const parms = {
Base64PDF: 'JVBERi0xLjQKJYGBgYEKMSAwIG9iago8PCAvVHlwZSAvQ2F0YWxvZyAvUGFnZXMgMiAwIFIgL01ldGFkYXRhIDYgMCBSID4+CmVuZG9iagoKMiAwIG9iago8PCAvVHlwZSAvUGFnZXMgL0tpZHMgWzMgMCBSXSAvQ291bnQgMSA+PgplbmRvYmoKCjMgMCBvYmoKPDwgL1R5cGUgL1BhZ2UgCi9QYXJlbnQgMiAwIFIgCi9NZWRpYUJveCBbMCAwIDU5NSA4NDJdIAovQ29udGVudHMgNCAwIFIgCi9SZXNvdXJjZXMgPDwgL0ZvbnQgPDwgL0YxIDUgMCBSID4+ID4+ID4+CmVuZG9iagoKNCAwIG9iago8PCAvTGVuZ3RoIDEyMCA+PgpzdHJlYW0KQlQKL0YxIDI0IFRmCjcyIDc2MCBUZAooUHJvZHVjdCBSZXBvcnQpIFRqCjAgLTQwIFRkCihQcm9kdWN0ICAgICAgUHJpY2UpIFRqCjAgLTIwIFRkCihMYXB0b3AgICAgICAgJDEyMDApIFRqCjAgLTIwIFRkCihNb3VzZSAgICAgICAgJDI1KSBUagowIC0yMCBUZAooS2V5Ym9hcmQgICAgICQ1MCkgVGoKRVQKZW5kc3RyZWFtCmVuZG9iagoKNSAwIG9iago8PCAvVHlwZSAvRm9udCAvU3VidHlwZSAvVHlwZTEgL0Jhc2VGb250IC9IZWx2ZXRpY2EgPj4KZW5kb2JqCgo2IDAgb2JqCjw8IC9Qcm9kdWNlciAocGRmLWxpYikgL0NyZWF0b3IgKHBkZi1saWIpID4+CmVuZG9iagoKeHJlZgowIDcKMDAwMDAwMDAwMCA2NTUzNSBmIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAwNzQgMDAwMDAgbiAKMDAwMDAwMDE0MCAwMDAwMCBuIAowMDAwMDAwMzAwIDAwMDAwIG4gCjAwMDAwMDA0NzAgMDAwMDAgbiAKMDAwMDAwMDU2MCAwMDAwMCBuIAp0cmFpbGVyCjw8IC9TaXplIDcgL1Jvb3QgMSAwIFIgPj4Kc3RhcnR4cmVmCjY2MAolJUVPRg=='
}
five.executeFunction('GeneratePDFServer', parms, null, '', '', function (serverResult) {
if (!serverResult) {
five.showMessage('Error generating emails.');
}
});
return five.success(result);
}
3 - In the server function, I am running a report, retrieving the Base64 string and appending the string to it ‘data:application/pdf;base64,‘.
4 - Then add both reports to an array and send them to the execution function.
When I received the email, both PDFs opened without issue.
function GeneratePDFServer(five, context, result) {
////////////////////////////////////////////////////////////////////////////////////////////////
// Execute the report action
////////////////////////////////////////////////////////////////////////////////////////////////
const reportResult = five.executeAction('StaffExtensionTestV2', {StaffExtension:"2122"});
five.log("Test....:")
five.log(JSON.stringify(reportResult))
if (reportResult.isOk() === false) {
return five.createError(reportResult);
}
const files = \[\]
let base64PDF = context.Base64PDF;
//prepend (add to the beginning) a string using simple string concatenation.
base64PDF = \`data:application/pdf;base64,${base64PDF}\`;
files.push(base64PDF)
files.push(reportResult.report)
////////////////////////////////////////////////////////////////////////////////////////////////
// The reportResult contains a parameter called report, which is the report result in the format
// of a pdf (encoded as data url application/pdf mimetype)
////////////////////////////////////////////////////////////////////////////////////////////////
const mailMergeContext = {SMTPToEmail: 'youremail@yourcompany.co', SMTPToName: "Staff", SMTPAttachments: \[...files\]};
const mailResult = five.executeAction('ExtensionReportEmail', mailMergeContext);
if (mailResult.isOk() === false) {
return five.createError(mailResult);
}
return five.success('Report has been sent');
}
This is the default function that attaches the files before the email is sent; this function is on the event ‘Do Merge Record‘, in the Mail Merge action, tab events.
Observation: In this example, both files will have the same name, but you can change it.
function DoAddAttachments(five, context, result) {
if (context.Attachments && context.Attachments.length > 0) {
for (let i = 0; i < context.Attachments.length; i++) {
const attachment = context.Attachments\[i\];
five.addAttachment(five.toDataURL('application/pdf', attachment), 'StaffExtension');
}
}
return five.success(result);
}
If you use a five.variable for the Base64 (raw), make sure you set this variable in the back, and retrieve it using five.getVariable(‘variableName‘).
Please let me know if that helps you.
Regards,
Elton S