Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multi_cell table is broken #349

Closed
mcerveny opened this issue Mar 7, 2022 · 3 comments
Closed

multi_cell table is broken #349

mcerveny opened this issue Mar 7, 2022 · 3 comments

Comments

@mcerveny
Copy link

mcerveny commented Mar 7, 2022

New features breaks table rendering with multi_cell().
multi_cell() is not rendered if txt="". It breaks table design (cell() is ok).

test.pdf

import fpdf

pdf = fpdf.FPDF(orientation='portrait', unit='mm', format='A4')
pdf.add_page()
pdf.set_font('helvetica',size=10)
for i in range(1,5):
  pdf.multi_cell(20, ln=3, txt=str(i))
pdf.ln(10)
for i in range(1,5):
  pdf.multi_cell(20, ln=3, txt=str(i) if i>2 else "")
pdf.ln(10)
for i in range(1,5):
  pdf.cell(20, txt=str(i))
pdf.ln(10)
for i in range(1,5):
  pdf.cell(20, txt=str(i) if i>2 else "")
pdf.output('test.pdf')
@mcerveny mcerveny added the bug label Mar 7, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Mar 7, 2022

Hi @mcerveny

Thanks for reporting this.
The behaviour of multi_cell() with empty text input was never tested before.

Here is a unit test we should add, based on your code @mcerveny:

def test_multi_cell_with_empty_contents(tmp_path):  # issue 349
    pdf = fpdf.FPDF()
    pdf.add_page()
    pdf.set_font('helvetica', size=10)
    for i in range(1,5):
        pdf.multi_cell(20, ln=3, txt=str(i))
    pdf.ln(10)
    for i in range(1,5):
        pdf.multi_cell(20, ln=3, txt=str(i) if i>2 else "")
    assert_pdf_equal(pdf, HERE / "multi_cell_with_empty_contents.pdf", tmp_path)

Ping @gmischler: what do you think would be the best approach here, to keep the same behaviour as before?
In the new code, _render_styled_cell_text() is responsible for incrementing FPDF.x but is never called by multi_cell() in that case, because text_lines is empty.

@gmischler
Copy link
Collaborator

Ping @gmischler: what do you think would be the best approach here, to keep the same behaviour as before? In the new code, _render_styled_cell_text() is responsible for incrementing FPDF.x but is never called by multi_cell() in that case, because text_lines is empty.

The simplest solution might be to check if text_lines is None right before the while loop, and invoke _render_styled_cell_text() with a dummy line, so it can do its positioning magic.

Lucas-C added a commit that referenced this issue Mar 7, 2022
Lucas-C added a commit that referenced this issue Mar 7, 2022
@Lucas-C Lucas-C closed this as completed in a67479d Mar 7, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Mar 7, 2022

Fixed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants