The Palos Publishing Company

Follow Us On The X Platform @PalosPublishing
Categories We Write About

Build a PDF invoice generator

Here’s a complete example of a PDF invoice generator using Python with the reportlab library. This script allows you to generate a clean and customizable invoice in PDF format.


🛠️ Prerequisites

Install the required package:

bash
pip install reportlab

Full Code: PDF Invoice Generator

python
from reportlab.lib.pagesizes import A4 from reportlab.lib import colors from reportlab.lib.units import mm from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer from reportlab.lib.styles import getSampleStyleSheet import datetime def generate_invoice(invoice_data, filename="invoice.pdf"): doc = SimpleDocTemplate(filename, pagesize=A4, rightMargin=30, leftMargin=30, topMargin=30, bottomMargin=18) elements = [] styles = getSampleStyleSheet() # Header elements.append(Paragraph(f"<b>{invoice_data['company_name']}</b>", styles['Title'])) elements.append(Paragraph(invoice_data['company_address'], styles['Normal'])) elements.append(Spacer(1, 12)) # Invoice info invoice_details = [ ['Invoice Number:', invoice_data['invoice_number']], ['Invoice Date:', invoice_data['invoice_date']], ['Due Date:', invoice_data['due_date']], ['Billed To:', invoice_data['customer_name']], ['Customer Address:', invoice_data['customer_address']], ] for item in invoice_details: elements.append(Paragraph(f"<b>{item[0]}</b> {item[1]}", styles['Normal'])) elements.append(Spacer(1, 12)) # Invoice Table data = [['Item', 'Quantity', 'Unit Price', 'Total']] total_amount = 0 for item in invoice_data['items']: line_total = item['quantity'] * item['unit_price'] total_amount += line_total data.append([ item['description'], str(item['quantity']), f"${item['unit_price']:.2f}", f"${line_total:.2f}" ]) # Add total row data.append(['', '', 'Total:', f"${total_amount:.2f}"]) table = Table(data, colWidths=[80*mm, 30*mm, 30*mm, 30*mm]) table.setStyle(TableStyle([ ('BACKGROUND', (0, 0), (-1, 0), colors.HexColor('#d3d3d3')), ('TEXTCOLOR', (0, 0), (-1, 0), colors.black), ('ALIGN', (1, 1), (-1, -1), 'CENTER'), ('FONTNAME', (0, 0), (-1, 0), 'Helvetica-Bold'), ('BOTTOMPADDING', (0, 0), (-1, 0), 10), ('BOX', (0, 0), (-1, -1), 1, colors.gray), ('GRID', (0, 0), (-1, -1), 1, colors.grey) ])) elements.append(table) # Footer elements.append(Spacer(1, 20)) elements.append(Paragraph("Thank you for your business!", styles['Italic'])) # Build PDF doc.build(elements) print(f"Invoice saved as {filename}") # Sample Data invoice = { 'company_name': 'Tech Solutions Inc.', 'company_address': '1234 AI Blvd, Innovation City, 90001', 'invoice_number': 'TS-2025-001', 'invoice_date': str(datetime.date.today()), 'due_date': str(datetime.date.today() + datetime.timedelta(days=30)), 'customer_name': 'John Doe', 'customer_address': '789 Market St, Springfield, IL 62701', 'items': [ {'description': 'AI Development Services', 'quantity': 10, 'unit_price': 120.00}, {'description': 'Cloud Hosting (3 months)', 'quantity': 1, 'unit_price': 300.00}, {'description': 'Maintenance Package', 'quantity': 2, 'unit_price': 75.00} ] } generate_invoice(invoice)

📄 Output

This script generates a cleanly formatted invoice.pdf file with company and customer details, itemized services, and totals.

Would you like a version that runs as a web app (e.g., using Flask or Streamlit)?

Share this Page your favorite way: Click any app below to share.

Enter your email below to join The Palos Publishing Company Email List

We respect your email privacy

Categories We Write About