mexitaw
mexitaw

Reputation: 119

Generating an invoice with FPDF

I tried using the reportLab library with no luck. For some reason, it wouldn't write the file. I am not sure if it was permission issues but w.e. . Now I am happily using fpdf but I am lost in this little error.

Traceback (most recent call last):
File "/Users/User/Desktop/Invoice Maker/invoice_maker.py", line 40, in <module>
pdf_inv.generate_pdf()
AttributeError: 'PdfInvoice' object has no attribute 'generate_pdf'

Where am I going wrong?

import fpdf
from datetime import datetime

from datetime import datetime
from collections import namedtuple

Creator = namedtuple("Creator", ["first_name", "last_name", "email", "phone_num","address", "city", "country"])
Organization = namedtuple("Organination", ["name", "address", "city", "country"])
Project = namedtuple("Project", ["name", "description", "amount"])
File = namedtuple("File", ["filename", "font_size", "line_height", "orientation"])
PdfInvoice = namedtuple("PdfInvoice", ["invoice_num", "creator", "organization", "project", "file"])

def generate_pdf(self):
    dt = datetime.now()
    date = dt.date()
    pdf = fpdf.FPDF(format=self.file.orientation)
    pdf.add_page()
    pdf.set_font("Arial", size=self.file.font_size)

    pdf_content = [
        f"Invoice Number #{self.pdf_inv}", 
        f"Date Invoiced #{date}",
        # and so on and so forth
    ]

    for line in pdf_content:
        pdf.write(self.file.line_height, line)
        pdf.ln()

    pdf.output(self.file.filename)

if __name__ == "__main__":
    creator = Creator('Test', 'User', '[email protected]', 'Testtest','Testtest Testtest, 123 Test road', 'Testtest', 'Testtest')
    organization = Organization('Test Org', 'Testtest', 'Testtest','Testtest')
    file = File("Invoice.pdf", 12, 5, "letter")
    project = Project('Ecommerce site', 'Worked on the ecommerce site', 10.900)
    pdf_inv = PdfInvoice('1393939', creator, organization, project,file)
    pdf_inv.generate_pdf()

Upvotes: 0

Views: 1647

Answers (1)

velociraptor11
velociraptor11

Reputation: 604

Your problem is from the usage of namedtuple.

  1. Did you mean to use this statement instead
PdfInvoice = namedtuple("PdfInvoice", ["filename", "font_size", "line_height", "orientation"])
  1. You are passing too many argument here, you have to remove one:
pdf_inv = PdfInvoice('1393939', creator, organization, project,file)

EDIT

Please use this function definition instead:

def generate_pdf(pdf_invoice_obj):
    dt = datetime.now()
    date = dt.date()
    pdf = fpdf.FPDF(format=pdf_invoice_obj.file.orientation)
    pdf.add_page()
    pdf.set_font("Arial", size=pdf_invoice_obj.file.font_size)

    pdf_content = [
        f"Invoice Number #{pdf_invoice_obj.pdf_inv}", 
        f"Date Invoiced #{date}",
        # and so on and so forth
    ]

    for line in pdf_content:
        pdf.write(pdf_invoice_obj.file.line_height, line)
        pdf.ln()

    pdf.output(pdf_invoice_obj.file.filename)

Then call it like so:

generate_pdf(pdf_inv)

Upvotes: 1

Related Questions