Automated Invoice Generator

Automated Invoice Generator

Create, customize, and download professional invoices in minutes.

${client.name}

${client.address.replace(/, /g, '
')}

${client.email}

Invoice

Invoice #: ${invoiceDetails.number}

Issue Date: ${invoiceDetails.issueDate}

Due Date: ${invoiceDetails.dueDate}

${lineItemsHtml}
Description Quantity Rate Amount
Subtotal ${formatCurrency(subtotal)}
Tax (${taxRate}%) ${formatCurrency(taxAmount)}
Total ${formatCurrency(total)}

Notes

${notes}

`; document.getElementById('download-pdf-btn').addEventListener('click', generatePdf); }; const renderDataConfig = () => { const configContent = document.getElementById('content-data-config'); if (!configContent) return; const lineItemsHtml = appData.lineItems.map((item, index) => `
`).join(''); configContent.innerHTML = `
Your Company (Bill From)
Client Info (Bill To)
Invoice Details
Line Items
DescriptionQuantityRate ($)
${lineItemsHtml}
`; attachConfigListeners(); }; const attachConfigListeners = () => { document.getElementById('add-item-btn').addEventListener('click', () => { appData.lineItems.push({ description: '', quantity: 1, rate: 0 }); renderDataConfig(); lucide.createIcons(); }); document.querySelectorAll('.remove-item-btn').forEach(btn => btn.addEventListener('click', e => { appData.lineItems.splice(parseInt(e.currentTarget.dataset.index), 1); renderDataConfig(); lucide.createIcons(); })); document.getElementById('update-data-btn').addEventListener('click', handleConfigUpdate); }; const handleConfigUpdate = () => { // Company appData.company.name = document.getElementById('company-name').value; appData.company.address = document.getElementById('company-address').value; appData.company.email = document.getElementById('company-email').value; appData.company.phone = document.getElementById('company-phone').value; appData.company.logoUrl = document.getElementById('company-logoUrl').value; // Client appData.client.name = document.getElementById('client-name').value; appData.client.address = document.getElementById('client-address').value; appData.client.email = document.getElementById('client-email').value; // Invoice Details appData.invoiceDetails.number = document.getElementById('invoice-number').value; appData.invoiceDetails.issueDate = document.getElementById('invoice-issueDate').value; appData.invoiceDetails.dueDate = document.getElementById('invoice-dueDate').value; // Line Items const newLineItems = []; document.querySelectorAll('.line-item-row').forEach(row => { newLineItems.push({ description: row.querySelector('[data-field="description"]').value, quantity: parseFloat(row.querySelector('[data-field="quantity"]').value) || 0, rate: parseFloat(row.querySelector('[data-field="rate"]').value) || 0, }); }); appData.lineItems = newLineItems; // Tax & Notes appData.taxRate = parseFloat(document.getElementById('tax-rate').value) || 0; appData.notes = document.getElementById('notes').value; renderInvoicePreview(); alert('Invoice updated successfully!'); switchTab(0); }; const generatePdf = () => { loadingOverlay.style.display = 'flex'; const { jsPDF } = window.jspdf; const pdfContent = document.getElementById('pdf-content-area'); html2canvas(pdfContent, { scale: 2, useCORS: true, logging: false }) .then(canvas => { const imgData = canvas.toDataURL('image/png'); const pdf = new jsPDF({ orientation: 'portrait', unit: 'px', format: 'a4' }); const pdfWidth = pdf.internal.pageSize.getWidth(); const pdfHeight = pdf.internal.pageSize.getHeight(); const canvasWidth = canvas.width; const canvasHeight = canvas.height; const ratio = canvasWidth / canvasHeight; const imgWidth = pdfWidth; const imgHeight = imgWidth / ratio; let heightLeft = imgHeight; let position = 0; pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); heightLeft -= pdfHeight; while (heightLeft > 0) { position = heightLeft - imgHeight; pdf.addPage(); pdf.addImage(imgData, 'PNG', 0, position, imgWidth, imgHeight); heightLeft -= pdfHeight; } pdf.save(`Invoice-${appData.invoiceDetails.number}.pdf`); loadingOverlay.style.display = 'none'; }).catch(err => { console.error("PDF generation failed:", err); loadingOverlay.style.display = 'none'; alert('An error occurred while generating the PDF.'); }); }; // --- TAB NAVIGATION & INITIALIZATION --- const switchTab = (tabIndex) => { activeTabIndex = tabIndex; document.querySelectorAll('.tab-btn').forEach((btn, i) => btn.classList.toggle('active', i === tabIndex)); document.querySelectorAll('.tab-content').forEach((content, i) => content.classList.toggle('hidden', i !== tabIndex)); updateNavButtons(); }; const updateNavButtons = () => { prevTabBtn.disabled = activeTabIndex === 0; nextTabBtn.disabled = activeTabIndex === tabIdentifiers.length - 1; }; const initializeUI = () => { const tabs = [ { name: 'Invoice Preview', id: 'invoice-preview' }, { name: 'Configuration', id: 'data-config' } ]; tabIdentifiers = tabs.map(t => t.id); tabsContainer.innerHTML = tabs.map(tab => ``).join(''); mainContent.innerHTML = tabs.map(tab => `
`).join(''); tabs.forEach((tab, index) => { document.getElementById(`tab-${tab.id}`).addEventListener('click', () => switchTab(index)); }); renderInvoicePreview(); renderDataConfig(); switchTab(0); }; initializeUI(); lucide.createIcons(); prevTabBtn.addEventListener('click', () => { if (activeTabIndex > 0) switchTab(activeTabIndex - 1); }); nextTabBtn.addEventListener('click', () => { if (activeTabIndex < tabIdentifiers.length - 1) switchTab(activeTabIndex + 1); }); });
Scroll to Top