Skip to content

Fix: Support rowspans breaking over page jumps (#1460)#1694

Open
Neyhlo wants to merge 8 commits intopy-pdf:masterfrom
Neyhlo:master
Open

Fix: Support rowspans breaking over page jumps (#1460)#1694
Neyhlo wants to merge 8 commits intopy-pdf:masterfrom
Neyhlo:master

Conversation

@Neyhlo
Copy link

@Neyhlo Neyhlo commented Dec 8, 2025

Fixes #1460 ---

What changes are being made?

This PR fixes a bug where tables containing large vertically spanned cells (rowspan) would crash when exceeding the page height, and allows them to break across pages cleanly.

Key changes:

  1. The restrictive ValueError check in Table.render() that prevented long rows from rendering is removed.
  2. Introduced a helper function (_get_span_origin) and logic in _render_table_row to draw the vertical borders for spanned cells on continuation pages (the "ghost borders").

Checklist:

  • A unit test is covering the code added / modified by this PR
    (Note: We verified this by running existing rowspan tests which fail without the fix.)

  • In case of a new feature, docstrings have been added, with also some documentation in the docs/ folder (N/A, this is a bug fix/enhancement to existing feature)

  • This PR is ready to be merged


Additional comments:

The change in functionality requires updating several golden PDF files in the test suite. Specifically, tests in test_table_rowspan.py and test_table_padding.py fail because the visual output now includes the vertical borders on continuation pages, which changes the MD5 hash.

The reference files (golden files) need to be updated to match the new, correct behavior.


By submitting this pull request, I confirm that my contribution is made under the terms of the GNU LGPL 3.0 license.

@Neyhlo Neyhlo changed the title change on fpdf/table.py Fix: Support rowspans breaking over page jumps (#1460) Dec 8, 2025
Copy link
Collaborator

@andersonhc andersonhc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add some tests for your feature?
/test/table/test_table.py should be the best place for it - and you can use the current tests there as inspiration

@Lucas-C
Copy link
Member

Lucas-C commented Dec 22, 2025

Hi @Neyhlo

Without any answer from you, we will soon close this PR.

@Neyhlo
Copy link
Author

Neyhlo commented Feb 3, 2026

Hey @Lucas-C

I'm so sorry about my no call no see I was very taken by work.

@ByZay
Copy link

ByZay commented Feb 19, 2026

@Lucas-C or @andersonhc, would u be able to respond and continue this PR ?

Copy link
Collaborator

@andersonhc andersonhc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Neyhlo @ByZay I see what you’re aiming for, but I can’t verify the behavior change yet.

  • The test doesn’t evidence the change: it never calls pdf.table() or uses rowspans, so it isn’t exercising the new table logic.
  • As written, the change doesn’t seem to implement what the PR claims; from the current table code, a rowspan that crosses a page boundary still triggers the same error.

Could you add a focused test that uses pdf.table() with a rowspan that must cross a page brea? That would make it clear whether the new behavior actually works.

Comment on lines +1 to +57
from fpdf import FPDF

class TablePDF(FPDF):
def active_spanning_table(self, data_list, rows_per_page=25):
self.set_font("helvetica", size=11)
col_widths = [60, 60, 60]
self.set_fill_color(200, 200, 200)
headers = ["Header 1", "Header 2", "Header 3"]

def draw_header():
for i, header in enumerate(headers):
self.cell(col_widths[i], 10, header, border=1, fill=True)
self.ln()

draw_header()

for group in data_list:
items = group['items']
label = group['label']
for chunk_idx in range(0, len(items), rows_per_page):
chunk = items[chunk_idx:chunk_idx + rows_per_page]
if self.get_y() > 250:
self.add_page()
draw_header()

display_label = f"{label}\n" if chunk_idx > 0 else label

y_start = self.get_y()
self.multi_cell(col_widths[0], 10, display_label, border=1, align='C')
y_after_label = self.get_y()

self.set_xy(self.l_margin + col_widths[0], y_start)
self.cell(col_widths[1], 10, "", border=1)
self.cell(col_widths[2], 10, chunk[0], border=1)
self.ln()

for item in chunk[1:]:
if self.get_y() > 270:
self.add_page()
draw_header()
self.multi_cell(col_widths[0], 10, f"{label}\n(suite)", border=1, align='C')
self.set_xy(self.l_margin + col_widths[0], self.get_y() - 10)

self.set_x(self.l_margin + col_widths[0])
self.cell(col_widths[1], 10, "", border=1)
self.cell(col_widths[2], 10, item, border=1)
self.ln()

pdf = TablePDF()
pdf.add_page()

data = [
{"label": "Data 1", "items": [f"Ligne {i}" for i in range(100)]}
]

pdf.active_spanning_table(data, rows_per_page=25)
pdf.output("spantest2.pdf")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
from fpdf import FPDF
class TablePDF(FPDF):
def active_spanning_table(self, data_list, rows_per_page=25):
self.set_font("helvetica", size=11)
col_widths = [60, 60, 60]
self.set_fill_color(200, 200, 200)
headers = ["Header 1", "Header 2", "Header 3"]
def draw_header():
for i, header in enumerate(headers):
self.cell(col_widths[i], 10, header, border=1, fill=True)
self.ln()
draw_header()
for group in data_list:
items = group['items']
label = group['label']
for chunk_idx in range(0, len(items), rows_per_page):
chunk = items[chunk_idx:chunk_idx + rows_per_page]
if self.get_y() > 250:
self.add_page()
draw_header()
display_label = f"{label}\n" if chunk_idx > 0 else label
y_start = self.get_y()
self.multi_cell(col_widths[0], 10, display_label, border=1, align='C')
y_after_label = self.get_y()
self.set_xy(self.l_margin + col_widths[0], y_start)
self.cell(col_widths[1], 10, "", border=1)
self.cell(col_widths[2], 10, chunk[0], border=1)
self.ln()
for item in chunk[1:]:
if self.get_y() > 270:
self.add_page()
draw_header()
self.multi_cell(col_widths[0], 10, f"{label}\n(suite)", border=1, align='C')
self.set_xy(self.l_margin + col_widths[0], self.get_y() - 10)
self.set_x(self.l_margin + col_widths[0])
self.cell(col_widths[1], 10, "", border=1)
self.cell(col_widths[2], 10, item, border=1)
self.ln()
pdf = TablePDF()
pdf.add_page()
data = [
{"label": "Data 1", "items": [f"Ligne {i}" for i in range(100)]}
]
pdf.active_spanning_table(data, rows_per_page=25)
pdf.output("spantest2.pdf")
from fpdf import FPDF
from pathlib import Path
from test.conftest import assert_pdf_equal
HERE = Path(__file__).resolve().parent
def test_table_colspan_break(tmp_path):
def active_spanning_table(pdf, data_list, rows_per_page=25):
pdf.set_font("helvetica", size=11)
col_widths = [60, 60, 60]
pdf.set_fill_color(200, 200, 200)
headers = ["Header 1", "Header 2", "Header 3"]
def draw_header():
for i, header in enumerate(headers):
pdf.cell(col_widths[i], 10, header, border=1, fill=True)
pdf.ln()
draw_header()
for group in data_list:
items = group['items']
label = group['label']
for chunk_idx in range(0, len(items), rows_per_page):
chunk = items[chunk_idx:chunk_idx + rows_per_page]
if pdf.get_y() > 250:
pdf.add_page()
draw_header()
display_label = f"{label}\n" if chunk_idx > 0 else label
y_start = pdf.get_y()
pdf.multi_cell(col_widths[0], 10, display_label, border=1, align='C')
y_after_label = pdf.get_y()
pdf.set_xy(pdf.l_margin + col_widths[0], y_start)
pdf.cell(col_widths[1], 10, "", border=1)
pdf.cell(col_widths[2], 10, chunk[0], border=1)
pdf.ln()
for item in chunk[1:]:
if pdf.get_y() > 270:
pdf.add_page()
draw_header()
pdf.multi_cell(col_widths[0], 10, f"{label}\n(suite)", border=1, align='C')
pdf.set_xy(pdf.l_margin + col_widths[0], pdf.get_y() - 10)
pdf.set_x(pdf.l_margin + col_widths[0])
pdf.cell(col_widths[1], 10, "", border=1)
pdf.cell(col_widths[2], 10, item, border=1)
pdf.ln()
pdf = FPDF()
pdf.add_page()
data = [
{"label": "Data 1", "items": [f"Ligne {i}" for i in range(100)]}
]
active_spanning_table(pdf, data, rows_per_page=25)
assert_pdf_equal(pdf, HERE / "table_colspan_break.pdf", tmp_path, generate=True)

Neyhlo and others added 3 commits March 2, 2026 09:40
Co-authored-by: Anderson Herzogenrath da Costa <andersonhc@gmail.com>
Co-authored-by: Anderson Herzogenrath da Costa <andersonhc@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[table] Feature request: support "colspanning-cells" breaking over page jumps

4 participants