35 – Real-World Python Projects – PDF Invoice Generator

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)
  • datetime
  • json

๐Ÿ“ 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


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *