Dev.to WebDev ๐Ÿ›  Dev ๐Ÿ‘ 0 ๐Ÿ“– 5 min read

Building a Tiny Export Invoice Generator in Vanilla JS (With GST LUT Support)

Most invoice generators are a form, a total and a "Download PDF" button. That's roughly what we'll build here: one file, no dependencies, no framework. Three details are easy to get wrong, though, and they matter once r

Most invoice generators are a form, a total and a "Download PDF" button. That's roughly what we'll build here: one file, no dependencies, no framework.

Three details are easy to get wrong, though, and they matter once real money and a real accountant are involved: floating-point totals, invoice numbering, and, if you're an Indian freelancer billing clients abroad, the export endorsement your GST rules require on every invoice.

1. Keep money in integers

JavaScript numbers are floating point, so this is false:

0.1 + 0.2 === 0.3; // false

On a single line item you'll never notice. Across a year of invoices, a total that's off by a paisa is exactly the kind of thing that makes reconciliation miserable. The fix is old and boring: store amounts as integer minor units (paise, cents), and only convert back when you display them.

// Store money as integer minor units (paise, cents) so totals never drift.
const toMinor = (amount) => Math.round(Number(amount) * 100);

function formatMoney(minor, currency) {
  const locale = currency === 'INR' ? 'en-IN' : 'en-US';
  return new Intl.NumberFormat(locale, { style: 'currency', currency }).format(minor / 100);
}

toMinor(0.1) + toMinor(0.2) === toMinor(0.3) is true, because you're adding 10 + 20 and comparing against 30.

2. Format for the reader, not the database

Intl.NumberFormat handles currency symbols and digit grouping for you. Using the en-IN locale for rupees gives Indian lakh grouping:

formatMoney(12345678, 'INR'); // "โ‚น1,23,456.78"
formatMoney(12345678, 'USD'); // "$123,456.78"

3. Invoice numbers are a sequence, not a label

Your client's accounts team, and your own CA, expect invoice numbers to run in order without gaps or repeats.

function nextInvoiceNumber(lastNumber, prefix = 'INV') {
  const year = new Date().getFullYear();
  const n = lastNumber ? Number(lastNumber.split('-').pop()) + 1 : 1;
  return `${prefix}-${year}-${String(n).padStart(4, '0')}`;
}

Two rules follow from this:

  • Store the last number somewhere durable. Even localStorage beats retyping it from memory.
  • Never edit a sent invoice and resend it under the same number. If the amount was wrong, issue a credit note against it and send a new invoice with the next number. Otherwise two different documents share one number, and the client has probably already booked the first one.

One more thing if you're in India: the financial year runs April to March, so you may want to key the series to the financial year rather than the calendar year. That's a small change to nextInvoiceNumber.

4. The export endorsement (India)

If you're registered for GST and export services under a Letter of Undertaking (LUT, Form GST RFD-11), you can invoice without IGST, but each of those invoices has to carry a fixed endorsement. Hard-code it rather than letting anyone type it:

const LUT_ENDORSEMENT =
  'SUPPLY MEANT FOR EXPORT UNDER BOND OR LETTER OF UNDERTAKING WITHOUT PAYMENT OF INTEGRATED TAX';

function buildInvoice({ number, seller, client, currency, lines, issueDate, dueDate, underLut }) {
  const items = lines.map(l => ({ description: l.description, minor: toMinor(l.amount) }));
  const totalMinor = items.reduce((sum, i) => sum + i.minor, 0);
  return {
    number, seller, client, currency, issueDate, dueDate, items, totalMinor,
    endorsement: underLut ? LUT_ENDORSEMENT : null,
  };
}

The invoice also needs the client's name and address and the destination country, which is why client.country is a separate field rather than part of the address string.

5. Render it, safely

Everything in the invoice came from a form, so escape it before it goes anywhere near innerHTML:

// Escape anything a user typed before it goes into HTML.
const esc = (s) => String(s).replace(/[&<>"']/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;' }[c]));

function renderInvoice(inv) {
  const rows = inv.items
    .map(i => `<tr><td>${esc(i.description)}</td><td class="amt">${formatMoney(i.minor, inv.currency)}</td></tr>`)
    .join('');
  return `
    <h1>Invoice ${esc(inv.number)}</h1>
    <p><strong>From:</strong> ${esc(inv.seller.name)}, ${esc(inv.seller.address)} (GSTIN ${esc(inv.seller.gstin)})</p>
    <p><strong>Bill to:</strong> ${esc(inv.client.name)}, ${esc(inv.client.address)}, ${esc(inv.client.country)}</p>
    <p>Issued ${esc(inv.issueDate)} ยท Due ${esc(inv.dueDate)}</p>
    <table>${rows}<tr><th>Total due</th><th class="amt">${formatMoney(inv.totalMinor, inv.currency)}</th></tr></table>
    ${inv.endorsement ? `<p class="endorsement">${inv.endorsement}</p>` : ''}`;
}

6. PDF without a library

The browser already has a PDF engine: the print dialog. Hide the form when printing and let "Save as PDF" do the rest.

@media print {
  form, button { display: none; }
  .endorsement { font-weight: 700; margin-top: 2rem; }
}
const inv = buildInvoice({
  number: nextInvoiceNumber(localStorage.getItem('lastInvoice')),
  seller: { name: 'Asha Rao', address: 'Pune, India', gstin: '27ABCDE1234F1Z5' },
  client: { name: 'Acme Inc.', address: '1 Market St, San Francisco', country: 'United States' },
  currency: 'USD',
  lines: [
    { description: 'Design sprint, week 1', amount: 1200.10 },
    { description: 'Design sprint, week 2', amount: 1200.20 },
  ],
  issueDate: '2026-12-07',
  dueDate: '2027-01-06',
  underLut: true,
});

document.querySelector('#preview').innerHTML = renderInvoice(inv);
document.querySelector('#download').addEventListener('click', () => {
  localStorage.setItem('lastInvoice', inv.number);
  window.print();
});

That example totals exactly $2,400.30, with no stray floating-point digits.

What this doesn't do

It's deliberately small. Before you rely on it for real clients, know what's missing:

  • Tax lines. There's no GST, VAT or sales tax calculation for domestic invoices.
  • Payment details. Bank details or a payment link still have to go somewhere: on the invoice or in the email.
  • Country rules. Mandatory fields, numbering and e-invoicing formats vary by country, and some places require a specific format and channel.
  • Tracking. Under an LUT, the payment clock runs from each invoice date, so you need to know when each invoice was actually paid. A PDF can't tell you that.

Or skip the build

If you just need a clean invoice today, Vobero's free invoice generator gives you guided fields and a PDF export in the browser, with no sign-up. Building your own is still a good way to learn what belongs on the page and why.

Written by the team at Vobero, which helps freelancers invoice clients abroad and follow each payment through to settlement. This is general information, not tax advice.

๐Ÿ“ฐ Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ€” full credit and traffic to the original publisher.