【问题标题】:Generating an invoice with FPDF使用 FPDF 生成发票
【发布时间】:2020-01-04 02:18:45
【问题描述】:

我尝试使用 reportLab 库但没有成功。由于某种原因,它不会写入文件。我不确定这是否是权限问题,但 w.e. .现在我很高兴使用fpdf,但我迷失在这个小错误中。

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'

我哪里错了?

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()

【问题讨论】:

  • 嗨,您可以添加完整的错误堆栈跟踪吗?
  • @velociraptor11 抱歉,问题已更新

标签: python python-3.x pdf fpdf


【解决方案1】:

您的问题来自于命名元组的使用。

  1. 你是不是要改用这个语句
PdfInvoice = namedtuple("PdfInvoice", ["filename", "font_size", "line_height", "orientation"])
  1. 您在这里传递了太多参数,您必须删除一个:
pdf_inv = PdfInvoice('1393939', creator, organization, project,file)

编辑

请改用这个函数定义:

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)

然后这样称呼它:

generate_pdf(pdf_inv)

【讨论】:

  • 我把它改成了PdfInvoice = namedtuple("PdfInvoice", ["invoice_num", "creator", "organization", "project", "file"])。现在我得到:pdf_inv.generate_pdf() AttributeError: 'PdfInvoice' object has no attribute 'generate_pdf'
  • 如果有帮助,我更新了这个问题。感谢您的帮助
  • 我已经添加了我的答案,请尝试一下
  • 另外,如果有帮助,请给我点赞,谢谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-01
相关资源
最近更新 更多