py-pdf / fpdf2

Simple PDF generation for Python
https://py-pdf.github.io/fpdf2/
GNU Lesser General Public License v3.0
1.13k stars 253 forks source link

FPDF.unbreakable() drops table instead of adding page break beforehand #1230

Open nocalla opened 4 months ago

nocalla commented 4 months ago

Error details In the below example, a series of 10 tables of two rows each is created using the unbreakable context manager based on the logic described here.

Instead of the page break triggering before the unbreakable context manager as described, the table that triggers the page break (which should have cells labelled Test 31 to Test 37) does not get displayed.

This is perhaps down to me not fully understanding the syntax around using unbreakable, but I don't think so! I'm open to correction, of course!

Minimal code

from itertools import batched

import fpdf

PDF = fpdf.FPDF()
PDF.set_font("Helvetica", size=10)
PDF.add_page()

# create some test data
CELL_COUNT = 50
labels = [f"Test {i+1}" for i in range(CELL_COUNT)]

# group the data in batches
col_count = 5
label_groups = list(batched(labels, col_count))

# generate a separate table for each group with the labels as table headers
for group in label_groups:
    with PDF.unbreakable() as doc:
        doc.ln()
        with doc.table(
            text_align="CENTER",
            gutter_width=10,
        ) as table:
            table.row(group)
            row = table.row()
            for item in group:
                row.cell("DON'T PANIC - CELL LEFT INTENTIONALLY BLANK")

PDF.output("unbreakable_bug_test.pdf")

Environment Please provide the following information:

gmischler commented 4 months ago

Thanks for reporting this, @nocalla !

As you can see in #1200, the table code is currently under a fundamental overhaul. I don't know if there is a quick fix possible for the current code, but otherwise you'll have to be patient until that PR lands. It will be more flexible about page breaks in general, and hopefully solve your problem as well.