The Palos Publishing Company

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

Build a JSON-to-PDF invoice converter

Here’s a complete implementation of a JSON-to-PDF invoice converter using Python. It reads invoice data from a JSON file or object and generates a styled PDF using the reportlab library.


🔧 Requirements

Install reportlab before running:

bash
pip install reportlab

🧾 Python Code: JSON to PDF Invoice Converter

python
import json from reportlab.lib.pagesizes import A4 from reportlab.lib import colors from reportlab.pdfgen import canvas from reportlab.platypus import Table, TableStyle, Paragraph, SimpleDocTemplate from reportlab.lib.styles import getSampleStyleSheet def generate_invoice(json_data, output_filename="invoice.pdf"): data = json.loads(json_data) if isinstance(json_data, str) else json_data doc = SimpleDocTemplate(output_filename, pagesize=A4) elements = [] styles = getSampleStyleSheet() styleN = styles["Normal"] styleH = styles["Heading1"] # Invoice Header elements.append(Paragraph(f"Invoice #{data['invoice_no']}", styleH)) elements.append(Paragraph(f"Date: {data['date']}", styleN)) elements.append(Paragraph(f"Due Date: {data['due_date']}", styleN)) elements.append(Paragraph(f"Bill To: {data['customer']['name']}", styleN)) elements.append(Paragraph(f"{data['customer']['address']}", styleN)) elements.append(Paragraph(f"Email: {data['customer']['email']}", styleN)) elements.append(Paragraph("<br/>", styleN)) # spacing # Table of items table_data = [["Item", "Description", "Qty", "Unit Price", "Total"]] for item in data["items"]: total_price = item["quantity"] * item["unit_price"] table_data.append([ item["name"], item.get("description", ""), str(item["quantity"]), f"${item['unit_price']:.2f}", f"${total_price:.2f}" ]) # Table Style table = Table(table_data, colWidths=[80, 180, 50, 80, 80]) table.setStyle(TableStyle([ ("BACKGROUND", (0, 0), (-1, 0), colors.grey), ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke), ("ALIGN", (2, 1), (-1, -1), "CENTER"), ("GRID", (0, 0), (-1, -1), 0.5, colors.black), ("FONTNAME", (0, 0), (-1, 0), "Helvetica-Bold"), ("BOTTOMPADDING", (0, 0), (-1, 0), 10), ])) elements.append(table) # Total Section elements.append(Paragraph("<br/>", styleN)) elements.append(Paragraph(f"Subtotal: ${data['subtotal']:.2f}", styleN)) elements.append(Paragraph(f"Tax ({data['tax_percent']}%): ${data['tax']:.2f}", styleN)) elements.append(Paragraph(f"Total: <b>${data['total']:.2f}</b>", styleN)) # Footer elements.append(Paragraph("<br/><br/>Thank you for your business!", styleN)) doc.build(elements) print(f"Invoice saved as {output_filename}") # Sample JSON sample_invoice = { "invoice_no": "INV-1001", "date": "2025-05-17", "due_date": "2025-06-01", "customer": { "name": "John Doe", "address": "123 Elm Street, Springfield, IL 62704", "email": "john.doe@example.com" }, "items": [ {"name": "Web Design", "description": "Homepage redesign", "quantity": 1, "unit_price": 500.00}, {"name": "Hosting", "description": "Annual hosting plan", "quantity": 1, "unit_price": 120.00}, {"name": "Maintenance", "description": "Monthly support", "quantity": 6, "unit_price": 50.00} ], "subtotal": 920.00, "tax_percent": 10, "tax": 92.00, "total": 1012.00 } # Generate the PDF generate_invoice(sample_invoice)

✅ Features

  • Supports multiline descriptions and dynamic item list

  • Customizable tax, subtotal, and totals

  • Basic styling with a header and table format


Let me know if you want:

  • HTML-to-PDF version

  • Flask web interface

  • Command-line version

  • Multi-language currency formatting

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