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[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 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)?
Explanation of the error
The problem starts at the end of the first page when the value of
yis high (may beyvalue is180mm). At this point your code executed the instructions:and inserts the last row in the first page. After that the library creates a new page and it is ready to place the next row at the start of this new page.
At this point are executed following 2 instruction:
The instruction
self.set_y(y)places the code at the start of the second page.Now is inserted a new row, but
yis equal to195and it is yet increased by15. So when is executed the instructionself.set_y(210)the library creates the third 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 ofy, you have to avoid the y increase and accept the current value ofself.get_y(): you have reached the end of the page and the library have add a new page.Modification to the
content()methodOne possible solution is modify your
content()function as followed: