`;
pdfPreview.appendChild(reportContent);
downloadPdfBtn.textContent = 'Generating...';
downloadPdfBtn.disabled = true;
container.classList.remove('hidden');
container.style.position = 'absolute';
container.style.left = '-9999px';
container.style.top = '0';
try {
const canvas = await html2canvas(pdfPreview, { scale: 2 });
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({ orientation: 'p', unit: 'pt', format: 'a4' });
const pdfWidth = pdf.internal.pageSize.getWidth();
const margin = 40;
const usableWidth = pdfWidth - margin * 2;
const imgHeight = (canvas.height * usableWidth) / canvas.width;
const pageHeight = pdf.internal.pageSize.getHeight() - margin * 2;
let heightLeft = imgHeight;
let position = margin;
pdf.addImage(imgData, 'PNG', margin, position, usableWidth, imgHeight);
heightLeft -= pageHeight;
while (heightLeft > 0) {
position = heightLeft - imgHeight + margin;
pdf.addPage();
pdf.addImage(imgData, 'PNG', margin, position, usableWidth, imgHeight);
heightLeft -= pageHeight;
}
pdf.save('Vendor_Compliance_Assessment.pdf');
} catch (error) {
console.error("Error generating PDF:", error);
} finally {
container.classList.add('hidden');
container.style.position = '';
downloadPdfBtn.textContent = 'Download Report as PDF';
downloadPdfBtn.disabled = false;
pdfPreview.innerHTML = '';
}
}
downloadPdfBtn.addEventListener('click', downloadPDF);
// --- Initial Load ---
updateUI();
});
