[Python] pyFPDF: autopagebreak causes, that only one row is per page

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

  1. Stack

    Stack Membro Participativo

    I am in struggle with FPDF. When I am looping over a dataframe and I want to put in the data in the page of a PDF file; but when FPDF adds a new page would be happen:

    • the first page is correctly filled by many rows (and this is good)
    • in the other pages, there are only one row per page

    I shows what happens below:

    PAGE 1: val 1, val 2, val 3, val 4, val 5, val 6
    PAGE 2: val 7,
    PAGE 3: val 8,
    PAGE 4: val 9,
    [...]


    Here is my code:

    pdf = PDF('L', 'mm', 'A4')
    pdf.alias_nb_pages()
    pdf.add_page()
    pdf.set_font("Arial","",12)
    pdf.content(df)
    pdf.output('tuto2.pdf', 'F')


    and this is my PDF Class which is a child of FPDF:

    class PDF(FPDF):

    def header(self):
    self.set_font('Arial', 'B', 30)
    self.cell(0,30,"HALLO PDF",0,0,"C")
    self.ln(30)

    def footer(self):
    self.set_y(-20)
    self.set_font('Arial', 'I', 8)
    self.cell(0,10,"Seite " + str(self.page_no()) + "/{nb}",0,0,"C",0,"")
    self.set_y(-10)
    self.cell(0,10,"Dokument erzeugt am XXXX",0,0,"C",0,"")

    def content(self,df):
    y=30
    for i in range(len(df)):
    self.set_y(y)
    self.cell(40,15,df['LG'].iloc,1,ln=0,align='C')
    self.cell(40, 15, df['LGP'].iloc, 1, ln=0, align='C')
    self.cell(40, 15, df['ZEIT'].iloc, 1, ln=1, align='C')
    y=y+15


    What is the problem in my code?

    And maybe another one can answer me a quick question: Can I get the size of the body (like: pagesize - header - footer)?

    Continue reading...

Compartilhe esta Página