Financial Performance
${financialResultsHTML}
${amortizationTableHTML}
Estimated Environmental Impact
${impactResultsHTML}
`;
}
// --- PDF DOWNLOAD FUNCTIONALITY ---
downloadPdfBtn.addEventListener('click', function() {
if (typeof window.jspdf === 'undefined' || typeof window.html2canvas === 'undefined') {
alert("Error: PDF generation library is not available.");
return;
}
const { jsPDF } = window.jspdf;
const reportElement = document.getElementById('summaryReport');
if(!reportElement) {
alert("Error: Report content element not found.");
return;
}
this.textContent = "Generating...";
this.disabled = true;
html2canvas(reportElement, {
scale: 2,
useCORS: true,
logging: false,
backgroundColor: '#ffffff'
}).then(canvas => {
try {
const imgData = canvas.toDataURL('image/png');
const pdf = new jsPDF({
orientation: 'p',
unit: 'mm',
format: 'a4',
putOnlyUsedFonts:true
});
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = pdf.internal.pageSize.getHeight();
const imgWidth = canvas.width;
const imgHeight = canvas.height;
const ratio = imgWidth / imgHeight;
let pageImgWidth = pdfWidth - 20;
let pageImgHeight = pageImgWidth / ratio;
let heightLeft = pageImgHeight;
let position = 10;
if (pageImgHeight > pdfHeight - 20) {
pageImgHeight = pdfHeight - 20; // max height with margin
pageImgWidth = pageImgHeight * ratio;
}
pdf.addImage(imgData, 'PNG', 10, position, pageImgWidth, pageImgHeight);
heightLeft -= (pdfHeight - 20);
while (heightLeft > 0) {
position = -heightLeft - 10;
pdf.addPage();
pdf.addImage(imgData, 'PNG', 10, position, pageImgWidth, pageImgHeight);
heightLeft -= (pdfHeight);
}
pdf.save(`${(bondNameInput.value || 'Green-Bond').replace(/\s+/g, '-')}-Report.pdf`);
} catch(error) {
console.error("Failed to generate PDF:", error);
alert("An error occurred while generating the PDF.");
} finally {
this.textContent = "Download PDF Report";
this.disabled = false;
}
}).catch(err => {
console.error("html2canvas failed:", err);
alert("Could not render the report for PDF generation.");
this.textContent = "Download PDF Report";
this.disabled = false;
});
});
// Initialize by showing the first tab
showTab(currentTab);
});