developer
Generate GST-Compliant Invoices Automatically on Instant UPI Confirmation
Learn how to build an automated GST tax invoice generator using Node.js and PDFKit. Trigger official tax invoices upon instant UPI payment confirmation webhooks.
In modern Indian digital commerce, customer expectations are instant: when a buyer scans a QR code or taps a UPI intent link, they expect their official GST tax invoice and order confirmation receipt within seconds.
However, many early-stage businesses still generate invoices manually at the end of the day or use clunky accounting software that requires human export. Not only does this delay order fulfillment, but invoices often lack mandatory statutory fields required under Rule 46 of the Central Goods and Services Tax (CGST) Rules.
Here is the engineering tutorial on building an automated GST-compliant invoice generation engine in Node.js that creates and delivers a branded PDF tax invoice immediately upon UPI webhook confirmation.
Statutory Elements of a Valid GST Tax Invoice (Rule 46)
To ensure that your digital invoices are legally compliant and eligible for Input Tax Credit (ITC) by business buyers, your automated template must contain these mandatory elements:
Rule 46 Mandatory Invoice Checklist:
├── 1. Supplier Name, Registered Office Address & GSTIN
├── 2. Sequential Serial Number (unique for the financial year)
├── 3. Date of Issue
├── 4. Buyer Name, Address, State Code & Buyer GSTIN (for B2B)
├── 5. Harmonized System of Nomenclature (HSN) or SAC Code
├── 6. Item Description, Quantity, Unit Price & Discount
├── 7. Taxable Value & Tax Rate Breakdown:
│ • Intra-State: CGST (e.g., 9%) + SGST (e.g., 9%)
│ • Inter-State: IGST (e.g., 18%)
├── 8. Bank Reference / 12-Digit UPI UTR Number
└── 9. Digital Signature or "Computer-Generated Invoice" clause
The Real-Time Invoicing Architecture
[Customer Pays via VyaparGateway Dynamic UPI]
│
▼ (Sub-500ms Push Webhook)
[Node.js Invoicing Microservice]
├── 1. Cryptographic HMAC Signature Verification
├── 2. Sequential Invoice Number Generator (Atomic DB Counter)
├── 3. PDFKit Render Engine builds Vector Tax Invoice PDF
└── 4. Uploads PDF to AWS S3 (Expires in 30 days)
│
▼
[Automated Dispatch within 3 Seconds of Payment]
├── WhatsApp API sends PDF Document directly into buyer's chat
└── SendGrid / Postmark emails PDF attachment to buyer
Automated GST Invoice Generator Code (Node.js & PDFKit)
Here is a production-grade TypeScript service that generates a PDF tax invoice using PDFKit:
import PDFDocument from "pdfkit";
import fs from "fs";
interface InvoiceData {
invoiceNumber: string;
dateStr: string;
customerName: string;
customerPhone: string;
customerGstin?: string;
items: Array<{ description: string; hsn: string; qty: number; rate: number }>;
utr: string;
isInterState: boolean;
}
export function generateGstInvoicePdf(data: InvoiceData, outputPath: string): Promise<string> {
return new Promise((resolve, reject) => {
const doc = new PDFDocument({ size: "A4", margin: 40 });
const writeStream = fs.createWriteStream(outputPath);
doc.pipe(writeStream);
// 1. Header & Merchant Details
doc.fontSize(18).text("TAX INVOICE", { align: "right" });
doc.fontSize(12).font("Helvetica-Bold").text("VYAPAR VENTURES PVT LTD");
doc.font("Helvetica").fontSize(9).text("GSTIN: 07AABCV1234F1Z8");
doc.text("Registered Office: Connaught Place, New Delhi - 110001");
doc.moveDown();
// 2. Invoice Metadata & Buyer Details
doc.text(`Invoice No: ${data.invoiceNumber}`);
doc.text(`Invoice Date: ${data.dateStr}`);
doc.text(`Bank Reference (UPI UTR): ${data.utr}`);
doc.moveDown();
doc.text(`Billed To: ${data.customerName} (${data.customerPhone})`);
if (data.customerGstin) doc.text(`Buyer GSTIN: ${data.customerGstin}`);
doc.moveDown();
// 3. Line Items Table Header
let y = doc.y;
doc.font("Helvetica-Bold");
doc.text("Description", 40, y);
doc.text("HSN", 240, y);
doc.text("Qty", 300, y);
doc.text("Rate", 340, y);
doc.text("Total", 420, y, { align: "right" });
doc.moveDown();
doc.font("Helvetica");
let subtotal = 0;
data.items.forEach((item) => {
const lineTotal = item.qty * item.rate;
subtotal += lineTotal;
y = doc.y;
doc.text(item.description, 40, y);
doc.text(item.hsn, 240, y);
doc.text(item.qty.toString(), 300, y);
doc.text(`₹${item.rate.toFixed(2)}`, 340, y);
doc.text(`₹${lineTotal.toFixed(2)}`, 420, y, { align: "right" });
});
doc.moveDown();
y = doc.y + 10;
// 4. Tax Calculation Breakdown (18% GST example)
const taxRate = 0.18;
const totalTax = subtotal * taxRate;
const grandTotal = subtotal + totalTax;
if (data.isInterState) {
doc.text(`Integrated GST (IGST 18%): ₹${totalTax.toFixed(2)}`, 300, y, { align: "right" });
} else {
const cgst = totalTax / 2;
const sgst = totalTax / 2;
doc.text(`Central GST (CGST 9%): ₹${cgst.toFixed(2)}`, 300, y, { align: "right" });
doc.text(`State GST (SGST 9%): ₹${sgst.toFixed(2)}`, 300, doc.y, { align: "right" });
}
doc.font("Helvetica-Bold").fontSize(11);
doc.text(`Grand Total: ₹${grandTotal.toFixed(2)}`, 300, doc.y + 5, { align: "right" });
doc.moveDown();
// 5. Footer & Legal Disclaimer
doc.font("Helvetica-Oblique").fontSize(8);
doc.text(
"This is a computer-generated tax invoice verified by bank webhook. No physical signature is required.",
40,
760,
{ align: "center", width: 515 }
);
doc.end();
writeStream.on("finish", () => resolve(outputPath));
writeStream.on("error", (err) => reject(err));
});
}
E-Invoicing (IRN) & B2B Compliance for Turnover > ₹5 Crores
If your annual aggregate turnover exceeds ₹5 Crores, the Central Board of Indirect Taxes and Customs (CBIC) mandates E-Invoicing:
- Before sending the PDF to a B2B customer, your server must transmit the invoice JSON to the government’s Invoice Registration Portal (IRP - NIC/Einvoice1).
- The portal returns a 64-character Invoice Reference Number (IRN) and a cryptographically signed QR code.
- Your automated engine renders the government’s official signed QR code onto the top right corner of the PDF.
Instant Customer Delivery via WhatsApp & Email
By hooking this engine into VyaparGateway’s sub-500ms payment confirmation webhooks:
- The buyer completes their payment in Google Pay or PhonePe.
- Before they put their phone back into their pocket, an automated WhatsApp ping delivers their official PDF tax invoice with the bank UTR reference.
- Your business maintains 100% GST compliance with zero manual paperwork.
Direct answers
Frequently asked questions
- What are the mandatory fields on a GST Tax Invoice under Rule 46 of CGST Rules?
- A valid tax invoice must include: (1) Consecutive invoice serial number, (2) Date of issue, (3) Supplier legal name, address, and GSTIN, (4) Recipient name and address, (5) HSN/SAC code, (6) Description of goods/services, (7) Total value and taxable value, (8) Tax rate and split amount (CGST/SGST or IGST), and (9) Place of supply.
- Can a digital PDF invoice generated via a webhook be legally valid without a physical signature?
- Yes. Under the Information Technology Act, 2000 and Rule 46 of the CGST Rules, a digital invoice containing an electronic signature or clearly marked 'This is a computer-generated invoice and does not require a physical signature' is legally valid.
- How does linking the 12-digit UPI UTR to the tax invoice protect merchants during audits?
- Printing the bank's unique 12-digit UTR directly on the customer's PDF tax invoice establishes an unshakeable one-to-one audit trail linking the commercial sale to the specific bank statement credit, preventing duplicate order disputes.
Build your payment flow
Explore the API and browser-only merchant tools.
Create UPI checkout orders, verify signed events, or test the free calculators and generators without exposing credentials.