I am trying to generate a pdf report using reportlab in python and I have a large table splitted along a few pages. The problem is that it overwrites my footer and I would like to limit it with a frame within a page template.
How could I use tableTemplate
everytime a new page is generated considering that I don't know the length of the table or the number of rows in a page?
Here is my code:
def report_pdf(self):
buffer = self.buffer
doc = SimpleDocTemplate(buffer,
rightMargin=30,
leftMargin=30,
topMargin=30,
bottomMargin=0,
pagesize=self.pagesize)
elements = []
table_data = gather_table_data()
long_table = Table(table_data)
elements.append(long_table)
frameMain = Frame(x1=doc.leftMargin,
y1=doc.topMargin,
width=doc.width,
height=doc.height)
tableFrame = Frame(x1=doc.leftMargin,
y1=doc.topMargin,
width=doc.width,
height=doc.height-50,
showBoundary=1)
mainTemplate = PageTemplate(id='main', frames=[frameMain])
tableTemplate = PageTemplate(id='table', frames=[tableFrame])
doc.addPageTemplates([mainTemplate, tableTemplate])
doc.build(elements, onFirstPage=footer, onLaterPages=footer)
pdf = buffer.getvalue()
buffer.close()
return pdf
I haven't found a way to repeat the template every time a new page is generated but I solved the problem by increasing document's bottomMargin
and placing the footer outside the margin. Considering that the footer is drawn directly on document's canvas it wasn't affected by the margin, while the table, as a flowable, was limited by document's margins, therefore prevented it from overwriting the footer.