Ok I partially found the solution. I create a generatePDF custom code with the following function
async function generatePDF(args, ctx) {
const url = "https://myapi";
const headers = {
"Content-Type": "application/json",
Authorization: "MyToken",
};
const body = {
"last_name": args.last_name,
"first_name": args.first_name,
"insured_asset_value": args.insured_asset_value
};
try {
// Make the POST request
const response = await fetch(url, {
method: "POST",
headers: headers,
body: JSON.stringify(body),
});
console.log(response);
// Check if the response is ok
if (!response.ok) {
throw new Error("Network response was not ok " + response.statusText);
}
// Convert the response to ArrayBuffer
const arrayBuffer = await response.arrayBuffer();
// Convert ArrayBuffer to Base64 string
const base64String = arrayBufferToBase64(arrayBuffer);
return ctx.triggerActionEvent("On Success", base64String, Event);
} catch (error) {
console.error("There has been a problem with your fetch operation:", error);
}
}
function arrayBufferToBase64(buffer) {
let binary = "";
const bytes = new Uint8Array(buffer);
const len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
Now, what I don't understand is how to use the ctx.triggerActionEvent to return the base64 value I generated ?