Invoice Generator

 pip install reportlab


from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas

def generate_invoice(client_name, items, tax_rate=0.05, filename="invoice.pdf"):
    c = canvas.Canvas(filename, pagesize=letter)
    width, height = letter

    # Title
    c.setFont("Helvetica-Bold", 16)
    c.drawString(50, height - 50, "INVOICE")

    # Client Details
    c.setFont("Helvetica", 12)
    c.drawString(50, height - 80, f"Client: {client_name}")

    # Table Headers
    c.setFont("Helvetica-Bold", 12)
    c.drawString(50, height - 120, "Item")
    c.drawString(250, height - 120, "Quantity")
    c.drawString(350, height - 120, "Price")
    c.drawString(450, height - 120, "Total")

    # Invoice Items
    c.setFont("Helvetica", 12)
    y_position = height - 140
    total_cost = 0

    for item, details in items.items():
        quantity, price = details
        total = quantity * price
        total_cost += total

        c.drawString(50, y_position, item)
        c.drawString(250, y_position, str(quantity))
        c.drawString(350, y_position, f"${price:.2f}")
        c.drawString(450, y_position, f"${total:.2f}")
        y_position -= 20

    # Tax and Total Calculation
    tax_amount = total_cost * tax_rate
    grand_total = total_cost + tax_amount

    c.setFont("Helvetica-Bold", 12)
    c.drawString(350, y_position - 20, "Subtotal:")
    c.drawString(450, y_position - 20, f"${total_cost:.2f}")

    c.drawString(350, y_position - 40, f"Tax ({tax_rate * 100}%):")
    c.drawString(450, y_position - 40, f"${tax_amount:.2f}")

    c.drawString(350, y_position - 60, "Grand Total:")
    c.drawString(450, y_position - 60, f"${grand_total:.2f}")

    # Save PDF
    c.save()
    print(f"✅ Invoice saved as {filename}")

No comments: