[Python] Build a Python class for FPDF

Discussão em 'Python' iniciado por Stack, Abril 17, 2025.

  1. Stack

    Stack Membro Participativo

    I want to use the Python library fpdf2 and build my own class to generate a pdf document. I tried this:

    from fpdf import FPDF

    class MyPdf(FPDF):
    def __init__(self, *args, **kwargs):
    super().__init__()
    self.pdf = FPDF()
    self.pdf.add_page()
    # customize pdf
    def OtherCustomization(self):
    self.pdf.add_page()
    # other customization pdf


    pdf1 = FPDF()
    pdf1.add_page()

    pdf2 = MyPdf()
    pdf2.add_page()
    pdf2.OtherCustomization()

    pdf1.output("1.pdf")
    pdf2.output("2.pdf")


    I expect a file 1.pdf with one page, another 2.pdf with two pages, but it does not work.

    # pdfinfo 1.pdf | grep Pages
    Pages: 1
    # pdfinfo 2.pdf | grep Pages
    Pages: 1

    Continue reading...

Compartilhe esta Página