[Python] Generating an invoice with FPDF

Discussão em 'Python' iniciado por Stack, Maio 12, 2025.

  1. Stack

    Stack Membro Participativo

    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', 'test@gmail.com', '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()

    Continue reading...

Compartilhe esta Página