[Python] Problems creating PDF with tables and multicells in FPDF Python

Discussão em 'Python' iniciado por Stack, Junho 16, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to create a table with the FPDF library that is capable of expanding the height of the cells when the text is very large.

    I am going to show the code that I have, which is the same that appears on the official documentation page:

    from fpdf import FPDF

    data = (
    ("First name", "Last name", "Age", "City"),
    ("Jules", "Smith", "34", "San Juan"),
    ("Mary", "Ramos", "45", "Orlando"),
    ("Carlson", "Banks", "19", "Los Angeles"),
    ("Lucas", "Cimon", "31", "Saint-Mahturin-sur-Loire udfhisudhf fughdiufhg fduihgsdiufg dfghsdifugh fdiguhdsfiug fdughdifugh dfhgsdiufhg"),
    )

    pdf = FPDF()
    pdf.add_page()
    pdf.set_font("Times", size=10)
    line_height = pdf.font_size * 2.5
    col_width = pdf.epw / 4
    for row in data:
    for datum in row:
    pdf.multi_cell(col_width, line_height, datum, border=1, ln=3)
    pdf.ln(line_height)
    pdf.output('table_with_cells.pdf')


    this is the result:

    [​IMG]

    The last column of the last row is taller because it has more content, that's fine, but I would like the height of all columns in that row to be the same dynamically. Something like this:

    [​IMG]

    How can I get it?

    Note: The data that will be in the table comes from the database, so I can't know exactly the length of each string

    Continue reading...

Compartilhe esta Página