Cost Breakdown
${breakdownHtml}
Total Estimated Cost
$${results.total.toFixed(2)}
`;
downloadBtn.classList.remove('hidden');
// Render Chart
const ctx = document.getElementById('costChart');
if(ctx) {
if (costChart) {
costChart.destroy();
}
costChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: chartLabels,
datasets: [{
label: 'Cost Breakdown',
data: chartData,
backgroundColor: ['#4338ca', '#10b981', '#f59e0b', '#ef4444', '#6b7280'],
borderColor: '#ffffff',
borderWidth: 2
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
plugins: {
legend: {
position: 'bottom',
labels: {
padding: 15,
boxWidth: 12
}
}
}
}
});
}
}
function downloadPDF() {
const { jsPDF } = window.jspdf;
const pdf = new jsPDF({ orientation: 'p', unit: 'mm', format: 'a4' });
const contentToPrint = document.getElementById('dashboard-content');
if (!contentToPrint) return;
html2canvas(contentToPrint, {
scale: 2,
useCORS: true,
onclone: (doc) => {
const chartCanvas = doc.getElementById('costChart');
if (chartCanvas && costChart) {
const img = new Image();
img.src = costChart.toBase64Image();
img.style.maxWidth = '100%';
img.style.height = 'auto';
chartCanvas.parentNode.replaceChild(img, chartCanvas);
}
}
}).then(canvas => {
const imgData = canvas.toDataURL('image/png');
const pdfWidth = pdf.internal.pageSize.getWidth();
const pdfHeight = (canvas.height * pdfWidth) / canvas.width;
pdf.addImage(imgData, 'PNG', 10, 10, pdfWidth - 20, pdfHeight > 277 ? 277 : pdfHeight - 20);
pdf.save('Car-Rental-Insurance-Estimate.pdf');
}).catch(err => {
console.error("Error generating PDF:", err);
});
}