Register Login

Creating PDF Invoices in Python with pText

Portable document format, popularly known as PDF, is a standard ISO file format created by Adobe in 1993. It does not follow the principle of the WYSIWYG (What You See is What You Get) format. Adobe developed a platform-independent agnostic document rendering engine that can run on any underlying operating system and architecture.

In this article, we will create PDF invoices using Python and two popular Python libraries: reportlab and invoice generator.

Understanding Reportlab Library:

Reportlab is a popular Python library that you have to install as a separate package for Python. For this you have to simply type the following to install it:

$pip install reportlab

Or,

py -3 -m pip install reportlab

It will take some time to install depending on the bandwidth.

Creating Invoice using reportlab:

For creating an automated PDF invoice report, you have to go through the following steps –

  • import the reportlab library
  • Pack all the data in the form of a nested list and put it under a list object.
  • Create a simple template of your invoice document with the document name and page size as two different parameters.
  • Create a document style and table style by setting the BOX, GRID, BACKGROUND, TEXTCOLOR, ALIGN, and BACKGROUND values of the TableStyle() method.
  • Then, create a table object using the Table() that will pass the table data and the style object as parameters.
  • Finally, build the document to generate the automated PDF invoice file.

Program:

from reportlab.platypus import SimpleDocTemplate, Table, Paragraph, TableStyle

from reportlab.lib import colors

from reportlab.lib.pagesizes import A4

from reportlab.lib.styles import getSampleStyleSheet



# data which we are going to be displayed in a  tabular format

tableData = [

["Date", "Course Name", "Course Type", "Course Price (Rs.)"],

["4/6/2021","Video Editing using Filmora X","Online Self-paced","1,500.00/-",],

["16/2/2021","Advanced Ethical Hacking","Online Live","8,000.00/-"],

    ["12/1/2021", "Data Science using Python","Offline Course","9,800.00/-"],

    ["02/5/2021","Technical Writing","Online Free","2,439.00/-"],

    ["Signature", "", "", "_________________"],

]



# creating a Document structure with A4 size page

docu = SimpleDocTemplate("invoice.pdf", pagesize=A4)

styles = getSampleStyleSheet()



doc_style = styles["Heading1"]

doc_style.alignment = 1



title = Paragraph("COURSE INVOICE", doc_style)

style = TableStyle([

        ("BOX", (0, 0), (-1, -1), 1, colors.black),

        ("GRID", (0, 0), (4, 4), 1, colors.chocolate),

        ("BACKGROUND", (0, 0), (3, 0), colors.skyblue),

        ("TEXTCOLOR", (0, 0), (-1, 0), colors.whitesmoke),

        ("ALIGN", (0, 0), (-1, -1), "CENTER"),

        ("BACKGROUND", (0, 1), (-1, -1), colors.beige),

    ])

# creates a table object using the Table() to pass the table data and the style object

table = Table(tableData, style=style)

# finally, we have to build the actual pdf merging all objects together

docu.build([title, table])

Output:

Understanding Invoice Generator Library:

Invoice Generator is another popular Python library used for generating complex invoices in formats like PDF and XML. It uses the properties of the reportlab library to generate the PDF file.

Using Invoice Generator:

For creating an automated PDF invoice report using Invoice Generator, you have to go through the following steps:

  • Import Invoice, Item, Client, Provider, Creator from InvoiceGenerator.API. Also import simple invoice from InvoiceGenerator.PDF, Finally import os for performing OS-related activities.
  • Set your document environment language.
  • Set some mandatory details for Client, Provider, and Creator
  • Create an invoice object and add items to it.
  • Finally generate the PDF using SimpleInvoice()

Program:

import os

from InvoiceGenerator.api import Invoice, Item, Client, Provider, Creator

from InvoiceGenerator.pdf import SimpleInvoice

# choosing English as the document language

os.environ["INVOICE_LANG"] = "en"

client = Client('Client company')

provider = Provider('STechies', bank_account='6454-6361-217273', bank_code='2021')

creator = Creator('Karl Iris')

invoice = Invoice(client, provider, creator)

invoice.add_item(Item(26, 780, description="Milk"))

invoice.add_item(Item(14, 460, description="Fruits"))

invoice.add_item(Item(10, 290, description="Nuts"))

invoice.add_item(Item(3, 165, description="Biscuits"))

invoice.currency = "Rs."

invoice.number = "10393069"

docu = SimpleInvoice(invoice)

docu.gen("invoice2.pdf", generate_qr_code=False) #you can put QR code by setting the #qr_code parameter to ‘True’

#docu.gen("invoice.xml") ## We can also generate an XML file of this invoice

Output:

Conclusion:

Among these libraries, Reportlab is the lighter version and easy to execute. Invoice Generator uses a portion of the report lab that makes it complex from inside. But it has different options and specialties that you can include features like setting locale currencies of several countries, adding QR codes, and generating the invoice in other formats apart from PDF.