Closed Lucas-C closed 6 days ago
Hi @DerekRobin
Thank you for the report.
Despite being mentioned in our documentation: https://py-pdf.github.io/fpdf2/Tables.html#setting-table-column-widths
align
can be passed to table() to set the table horizontal position relative to the page, when it's not using the full page width. It's centered by default.
...it seems that this never worked properly... 😅
I tested the following code with fpdf2
version 2.7.0, where tables were originally introduced in this documentation was redacted:
from fpdf import FPDF
TABLE_DATA = (
("First name", "Last name", "Age", "City"),
("Jules", "Smith", "34", "San Juan"),
("Mary", "Ramos", "45", "Orlando"),
("Carlson", "Banks", "19", "Los Angeles"),
("Lucas", "Cimon", "31", "Angers"),
)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pdf.table(col_widths=pdf.epw / 5, align="C") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
pdf.output("./test/table/table_with_fixed_col_width_and_align.pdf")
...and the table is NOT horizontally centered on the page!
Wooops.
I will look into fix it.
@allcontributors please add @DerekRobin for bug report
@Lucas-C
I've put up a pull request to add @DerekRobin! :tada:
Fixed in PR https://github.com/py-pdf/fpdf2/pull/1308
@DerekRobin: while the fix has not been released in a new version published on Pypi, you can test that it works for you using:
pip install git+https://github.com/py-pdf/fpdf2.git@master
Also, note that you will have to either specify a fixed column width (single number col_widths
in the document unit) or a table width=
in order for the table to not fill the page width.
When col_widths
is provided as an array, those values are not considered to be widths in the document unit
but as "fractions" (meaning that col_widths=(1, 1, 2)
is strictly equivalent to col_widths=(25, 25, 50)
)
What wonderful news to wake up to! Thank you so much :)
Discussed in https://github.com/py-pdf/fpdf2/discussions/1304