Purpose
Automatically generate professional PDF invoices for clients using Python.
Supports multiple items, tax calculation, totals, and auto-saving.
Used By (Real Life)
- Freelancers
- Small businesses
- Agencies
- E-commerce
- Billing & Accounts teams
๐ง What This Project Will Do
โ Accept invoice details (client, items, cost, tax)
โ Auto-calculate subtotals, tax, grand total
โ Generate a professional PDF invoice
โ Auto-save with invoice number + date
โ Optional email delivery
โ Optional Excel log of all invoices
๐งฐ Tech Stack
reportlab(PDF generation)pandas(optional for logging)datetimejson
๐ Folder Structure
PDF_Invoice_Generator/
โโโ invoice_generator.py
โโโ invoice_template.json
โโโ output/
๐งพ invoice_template.json (Example)
{
"invoice_number": "INV-2025-001",
"date": "2025-11-20",
"client_name": "ABC Technologies Pvt Ltd",
"client_address": "Bangalore, India",
"items": [
{"description": "Website Development", "quantity": 1, "price": 50000},
{"description": "SEO Optimization", "quantity": 1, "price": 15000},
{"description": "Hosting (1 year)", "quantity": 1, "price": 4000}
],
"tax_percent": 18
}
๐ Full Python Code: invoice_generator.py
โ Clean
โ Professional
โ Real-world ready
import json
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib import colors
from reportlab.lib.pagesizes import A4
from reportlab.lib.styles import getSampleStyleSheet
from datetime import datetime
def generate_invoice(json_file):
with open(json_file) as f:
data = json.load(f)
invoice_no = data["invoice_number"]
date = data["date"]
client = data["client_name"]
address = data["client_address"]
items = data["items"]
tax_percent = data["tax_percent"]
# Output file name
filename = f"output/{invoice_no}.pdf"
doc = SimpleDocTemplate(filename, pagesize=A4)
styles = getSampleStyleSheet()
content = []
# Title
content.append(Paragraph("<b>INVOICE</b>", styles["Title"]))
content.append(Spacer(1, 20))
# Invoice Details
content.append(Paragraph(f"Invoice No: {invoice_no}", styles["BodyText"]))
content.append(Paragraph(f"Date: {date}", styles["BodyText"]))
content.append(Spacer(1, 10))
# Client Info
content.append(Paragraph("<b>Bill To:</b>", styles["Heading3"]))
content.append(Paragraph(client, styles["BodyText"]))
content.append(Paragraph(address, styles["BodyText"]))
content.append(Spacer(1, 20))
# Table Header
table_data = [["Description", "Qty", "Price", "Total"]]
subtotal = 0
for item in items:
total = item["quantity"] * item["price"]
subtotal += total
table_data.append([
item["description"],
item["quantity"],
f"โน{item['price']}",
f"โน{total}"
])
tax_amount = round(subtotal * (tax_percent / 100), 2)
grand_total = subtotal + tax_amount
# Summary rows
table_data.append(["", "", "Subtotal", f"โน{subtotal}"])
table_data.append(["", "", f"Tax ({tax_percent}%)", f"โน{tax_amount}"])
table_data.append(["", "", "Grand Total", f"โน{grand_total}"])
# Table
table = Table(table_data, colWidths=[250, 50, 80, 80])
table.setStyle(TableStyle([
("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey),
("GRID", (0, 0), (-1, -1), 1, colors.black),
("FONTNAME", (0, 0), (-1, -1), "Helvetica"),
("ALIGN", (1, 1), (-1, -1), "CENTER"),
("VALIGN", (0, 0), (-1, -1), "MIDDLE")
]))
content.append(table)
content.append(Spacer(1, 30))
content.append(Paragraph("<b>Thank you for your business!</b>", styles["BodyText"]))
doc.build(content)
print(f"Invoice generated: {filename}")
if __name__ == "__main__":
generate_invoice("invoice_template.json")
๐งพ Sample Output (PDF)
โ Invoice Number
โ Date
โ Client Info
โ Item Table
โ Subtotal + Tax + Grand Total
โ Professional Layout
๐ Advanced Version (Optional Enhancements)
๐น 1. Email Invoice Automatically
Use Gmail SMTP โ Send invoice PDF to the client.
๐น 2. Automatic Invoice Number Generation
Format: INV-2025-002
๐น 3. Company Logo
Insert an image at the top of PDF.
๐น 4. Excel Log of All Invoices
Store every invoice in invoices.xlsx.
๐น 5. Multi-Currency Support
INR, USD, EUR, AED, SGD.
๐น 6. Online API Version
Create a FastAPI endpoint:
POST /generate-invoice

Leave a Reply