Expense Categorization Tool Expense Categorization Tool Automatically sort your expenses and generate a professional report. 1. Input Expenses 2. Review & Categorize 3. Report & Export Add Your Expenses Paste your expenses below (one per line, e.g., "Starbucks Coffee, 5.75") or add them manually. Review Your Expenses Expense Report Spending by Category Download Expense Report Previous Next Generated on: ${new Date("2025-09-11").toLocaleDateString()} CategoryTotal% ${summaryTableRows}Total$${total.toFixed(2)}100% ${detailsHtml} `; const pdfTemplate = document.getElementById('pdf-template'); pdfTemplate.innerHTML = reportHtml; pdfTemplate.classList.remove('invisible'); // Render chart on the hidden canvas const pdfCtx = document.getElementById('pdf-chart').getContext('2d'); new Chart(pdfCtx, { type: 'doughnut', data: pdfChartData, options: { animation: { duration: 0 }, plugins: { legend: { display: false } } } }); // A small delay to ensure chart is rendered before html2canvas runs setTimeout(async () => { try { const { jsPDF } = window.jspdf; const canvas = await html2canvas(pdfTemplate.querySelector('.pdf-report-container'), { 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 pdfHeight = (canvas.height * pdfWidth) / canvas.width; pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight); pdf.save('Expense_Report.pdf'); } catch(e) { console.error('PDF Generation Error:', e); } finally { downloadPdfBtn.disabled = false; downloadPdfBtn.textContent = 'Download Expense Report'; pdfTemplate.classList.add('invisible'); pdfTemplate.innerHTML = ''; } }, 500); } downloadPdfBtn.addEventListener('click', generatePdfReport); // --- INITIALIZATION --- switchTab(0); });