python-3.xfpdf2

Can't set X axis for pdf.table position FPDF2


I can't set X axis position for display pdf.table, only Y axis works using set_y(). When i use set_x() nothing happens or set_xy() only Y axis parameter works.

Theres a way to position a table in any page place?

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-Mathurin-sur-Loire"),
)

pdf = FPDF()

pdf.set_font("helvetica", size=8)
pdf.add_page()

pdf.set_xy(230, 80) <------------------------ HERE, ONLY Y AXIS WORKS

with pdf.table(
    line_height=6,
    width=60
) as table:
    for data_row in data:
        row = table.row()
        for datum in data_row:
            row.cell(datum)

pdf.output("tuto5.pdf")

Solution

  • FPDF.table() default behavior is to center the table on the page. You can specify align left and it will be positioned starting on the x coord.

    pdf.set_xy(80, 80)
    
    with pdf.table(
        line_height=6,
        align="l",
        width=60
    ) as table:
        for data_row in data:
            row = table.row()
            for datum in data_row:
                row.cell(datum)
    

    To have 2 tables side by side:

    pdf.set_xy(10, 80)
    
    with pdf.table(
        line_height=6,
        align="l",
        width=60
    ) as table:
        for data_row in data:
            row = table.row()
            for datum in data_row:
                row.cell(datum)
    
    pdf.set_xy(80, 80)
    
    with pdf.table(
        line_height=6,
        align="l",
        width=60
    ) as table:
        for data_row in data:
            row = table.row()
            for datum in data_row:
                row.cell(datum)