I am trying to use the tables in FPDF2 with a formatted PDF. My PDF Class inherits the header method that takes up the first 15 or so mm on the top of the page. The problem I am running into is when the page breaks the table continues to the next page and overlaps with the header. Is there a way to make it so that when the page breaks the table continues with the same top margin?
def header(self):
if self.page_no() != 1:
self.set_font("Helvetica", "B", 15)
self.set_fill_color(255, 255, 255)
self.set_xy(4, 4)
self.cell(0, 0, f"{self.title}")
self.set_line_width(.25)
self.set_draw_color(r=0, g=31, b=46)
self.line(x1=5, y1=self.header_height, x2=self.w - 5, y2=self.header_height)
def table_page_with_png(self, df: pd.DataFrame, columns: list,
text_align=("CENTER", "CENTER", "CENTER", "LEFT"),
col_widths=(20, 20, 25, 50),
png_padding=(2, 20, 2, 15)):
self.add_page()
self.set_draw_color(50)
self.set_line_width(.5)
self.set_font("Helvetica", '', 10)
working_df = df[columns]
df_to_list = working_df.values.tolist()
df_to_list.insert(0, working_df.columns.values.tolist())
with self.table(borders_layout="SINGLE_TOP_LINE",
text_align=text_align,
col_widths=col_widths,
line_height=2.5) as table:
for i, data_row in enumerate(df_to_list):
row = table.row()
for j, data_cell in enumerate(data_row):
self.set_font("Helvetica", '', 8)
if ".png" in str(data_cell) and i > 0:
row.cell(img=data_cell, img_fill_width=True, padding=png_padding)
else:
data_cell = re.sub("<br>", " ", str(data_cell).strip())
row.cell(data_cell, padding=(1, .5, 1, 1))
Thanks!
Bit of a hack, but I had the same problem and this is how I solved it.
Just call self.set_y()
at the end of header()
and pass in where you want your next page to start.
CONTENT_START = 108 # or set to whatever you want your content to start at
def header(self):
... # header code
self.set_y(CONTENT_START)
This works so long as your header height is predictable.