Skip to content

Commit 20106fa

Browse files
committed
change on fpdf/table.py
1 parent bde714a commit 20106fa

1 file changed

Lines changed: 66 additions & 12 deletions

File tree

fpdf/table.py

Lines changed: 66 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -250,17 +250,18 @@ def render(self):
250250
pagebreak_height = rows_info[i].pagebreak_height
251251
# pylint: disable=protected-access
252252
page_break = self._fpdf._perform_page_break_if_need_be(pagebreak_height)
253-
if (
254-
page_break
255-
and self._fpdf.y + pagebreak_height > self._fpdf.page_break_trigger
256-
):
257-
# Restoring original position on page:
258-
self._fpdf.x = prev_x
259-
self._fpdf.y = prev_y
260-
self._fpdf.l_margin = prev_l_margin
261-
raise ValueError(
262-
f"The row with index {i} is too high and cannot be rendered on a single page"
263-
)
253+
# Modification pour issue #1460 : On autorise le dépassement de page
254+
# if (
255+
# page_break
256+
# and self._fpdf.y + pagebreak_height > self._fpdf.page_break_trigger
257+
# ):
258+
# # Restoring original position on page:
259+
# self._fpdf.x = prev_x
260+
# self._fpdf.y = prev_y
261+
# self._fpdf.l_margin = prev_l_margin
262+
# raise ValueError(
263+
# f"The row with index {i} is too high and cannot be rendered on a single page"
264+
# )
264265
if page_break and repeat_headings and i >= self._num_heading_rows:
265266
# repeat headings on top:
266267
self._fpdf.y += self._outer_border_margin[1]
@@ -278,13 +279,61 @@ def render(self):
278279
self._fpdf.l_margin = prev_l_margin
279280
self._fpdf.x = self._fpdf.l_margin
280281

282+
def _get_span_origin(self, row_idx, col_idx):
283+
"""
284+
Helper to find the origin cell of a rowspan when encountering a None placeholder.
285+
Returns (cell, row_index_of_origin)
286+
"""
287+
# On remonte les lignes vers le haut pour trouver la cellule non-None
288+
for k in range(row_idx - 1, -1, -1):
289+
cell = self.rows[k].cells[col_idx]
290+
if cell is not None:
291+
# On a trouvé la cellule mère.
292+
# On vérifie si son rowspan est assez grand pour arriver jusqu'à nous
293+
if cell.rowspan > (row_idx - k):
294+
return cell, k
295+
return None, None
296+
return None, None
297+
281298
def _render_table_row(self, i, row_layout_info, cell_x_positions, **kwargs):
282299
row = self.rows[i]
283300
y = self._fpdf.y # remember current y position, reset after each cell
284301

285302
for j, cell in enumerate(row.cells):
286303
if cell is None:
304+
# --- START MODIFICATION: Ghost borders management ---
305+
# If the cell is None, it might be the continuation of a rowspan.
306+
origin_cell, origin_idx = self._get_span_origin(i, j)
307+
308+
if origin_cell and origin_cell.border:
309+
# Calculate position and width as if it were a real cell
310+
col_width = self._get_col_width(origin_idx, j, origin_cell.colspan)
311+
x1 = cell_x_positions[j]
312+
y1 = self._fpdf.y
313+
x2 = x1 + col_width
314+
y2 = y1 + row_layout_info.height
315+
316+
# Draw vertical borders (Left and Right)
317+
# Note: We assume a full border (1) or 'LTRB'.
318+
# Ideally we should parse origin_cell.border to check if L or R are required.
319+
self._fpdf.line(x1, y1, x1, y2) # Left
320+
self._fpdf.line(x2, y1, x2, y2) # Right
321+
322+
# --- CALCULATE END OF SPAN ---
323+
current_span_progress = i - origin_idx + 1
324+
total_span_rows = origin_cell.rowspan
325+
is_end_of_span = current_span_progress == total_span_rows
326+
327+
# SECURITY: Is this the very last row of the entire table?
328+
is_last_table_row = i == len(self.rows) - 1
329+
330+
# If we are at the end of the span OR at the end of the table, draw the bottom line
331+
if is_end_of_span or is_last_table_row:
332+
self._fpdf.line(x1, y2, x2, y2) # Bottom
333+
334+
# Continue to the next cell
287335
continue
336+
# --- END MODIFICATION ---
288337
self._render_table_cell(
289338
i,
290339
j,
@@ -414,7 +463,12 @@ def _render_table_cell(
414463
x1 = self._fpdf.l_margin
415464
x2 = x1 + self._width
416465
y1 = y1 - self._outer_border_margin[1]
417-
y2 = y2 + self._outer_border_margin[1]
466+
# If the cell height exceeds the page break threshold, we clip the border
467+
# so that it does not visually extend beyond the page limit.
468+
if y1 + cell_height > self._fpdf.page_break_trigger:
469+
y2 = self._fpdf.page_break_trigger
470+
else:
471+
y2 = y1 + cell_height
418472

419473
if j == 0:
420474
# lhs border

0 commit comments

Comments
 (0)