let base64Data: string;
let attachment: Attachment;
let blob: Blob;
docList.forEach(([pdfDoc, title]) => {
blob = pdfDoc.output('blob');
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
base64data = reader.result;
attachment = new Attachment();
attachment.setFilename(title);
attachment.setContent(base64data);
attachment.setType('application/pdf');
attachments.push(attachment);
}
});
pdfDoc is a jsPDF; Attachment is my own class with fields as indicated.
If I run the above code in debug mode and add breakpoints, the attachments array is populated as I expected. Otherwise the array ends up blank. I know there is an issue with synchronizing the looping and the the FileReader. I found the following answer
Looping through files for FileReader, output always contains last value from loop
but I'm not sure how to apply it to my case. Any suggestions? Thanks in advance.