0

I am in struggle with FPDF - when I am looping over a dataframe and I want to put in the data in the page, the FPDF adds a new page, when the first page is full, and this is good - but then on the other pages, there are only one row per page, like:

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 PDFClass:

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[i],1,ln=0,align='C')
            self.cell(40, 15, df['LGP'].iloc[i], 1, ln=0, align='C')
            self.cell(40, 15, df['ZEIT'].iloc[i], 1, ln=1, align='C')
            y=y+15

What is the problem here?

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

Thanks

frankfalse
  • 1,553
  • 1
  • 4
  • 17
Lukas Fürst
  • 81
  • 11

1 Answers1

2

Explanation of the error

The problem is that when you arrive at the end of the page the value of y is high and you continue to add 15 to it by the instruction y=y+15. After that you execute the instruction: self.set_y(y).
So when the y value is near the bottom of the page and you add a new cell the library chooses to add a new page.

How to avoid the error

To avoid this you have to check the value of self.get_y() and when it becomes <= of the value of y, you have to avoid the y increase and accept the current value of self.get_y(): you have reached the end of the page and the library have add a new page.

Modification to the content() method

One possible solution is modify your content() function as followed:

def content(self,df):
    y=30
    for i in range(len(df)):
        self.set_y(y)
        self.cell(40,15,df['LG'].iloc[i],1,ln=0,align='C')
        self.cell(40, 15, df['LGP'].iloc[i], 1, ln=0, align='C')
        self.cell(40, 15, df['ZEIT'].iloc[i], 1, ln=1, align='C')
        curr_y = self.get_y()
        if curr_y > y:
            y = y + 15
        else:
            # here the library has added a new page
            y = curr_y
frankfalse
  • 1,553
  • 1
  • 4
  • 17
  • Now, sometimes I get the error, that on page 2,3,....., there were the first two rows overlaying each other.. – Lukas Fürst Mar 29 '23 at 07:12
  • 1
    @Lukas Fürst I have changed the program try with the last presented in this answer please. The answer of yesterday wasn't perfect. Check my new `if` statement: is it different from yesterday. I have substituted the instruction `y=30` with `y=curr_y`. – frankfalse Mar 29 '23 at 07:16