I wrote the following code to create several pdf files with html2pdf.js. Later I want to save them to an archive and download said archive. I am using jsZip to create and fill the archive and fileSaver to save it:
window.zip = new JSZip();
let masterDiv = document.createElement('div');
masterDiv.id = 'masterDiv';
document.body.appendChild(masterDiv);
// Stundenplan für jeden Kandidaten erstellen
for (let i = 0; i < window.allCandidates.length; i++) {
let candidate = window.allCandidates[i].split(',');
window.lastName = candidate[0];
window.firstName = candidate[1];
window.filter = 'candidate';
setScheduleView('masterDiv');
let worker = html2pdf();
let opt = {
margin: 1,
image: {
type: 'jpeg',
quality: 1
},
html2canvas: {
scale: 2
},
jsPDF: {
unit: 'mm',
format: 'A3',
orientation: 'portrait'
}
};
// PDF erstellen
worker.set(opt).from(masterDiv).outputPdf().then((pdf) => {
window.zip.file(window.lastName + '.' + window.firstName + '.pdf', pdf, {
binary: true
});
});
setTimeout(() => {
clearDiv(masterDiv);
worker = null;
}, 50);
}
setTimeout(() => {
window.zip.generateAsync({
type: "blob"
}).then(function(content) {
saveAs(content, "allPDFs.zip");
});
}, 2000);
My problem is now: instead of one pdf per loop I get one single pdf that contains the complete content. What do I have to change to make html2pdf.js understand that it should create a new pdf file for each loop?