Skip to content

Commit

Permalink
Fixing support for align= in FPDF.table() - close #1306
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Nov 20, 2024
1 parent 096d1c8 commit 98037cb
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
2 changes: 1 addition & 1 deletion fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2533,7 +2533,7 @@ def ink_annotation(
pop-up window when open and active. This entry shall identify the user who added the annotation.
color (tuple): a tuple of numbers in the range 0.0 to 1.0, representing a colour used for
the title bar of the annotation’s pop-up window. Defaults to yellow.
border_width (int): thickness of the path stroke.
border_width (float): thickness of the path stroke.
"""
ink_list = sum(((x * self.k, (self.h - y) * self.k) for (x, y) in coords), ())
x_min = min(ink_list[0::2])
Expand Down
25 changes: 15 additions & 10 deletions fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(
repeat_headings (fpdf.enums.TableHeadingsDisplay): optional, indicates whether to print table headings on every page, default to 1.
"""
self._fpdf = fpdf
self._align = align
self._table_align = Align.coerce(align)
self._v_align = VAlign.coerce(v_align)
self._borders_layout = TableBordersLayout.coerce(borders_layout)
self._outer_border_width = outer_border_width
Expand All @@ -99,7 +99,7 @@ def __init__(
self._line_height = 2 * fpdf.font_size if line_height is None else line_height
self._markdown = markdown
self._text_align = text_align
self._width = fpdf.epw if width is None else width
self._width = width
self._wrapmode = wrapmode
self._num_heading_rows = num_heading_rows
self._repeat_headings = TableHeadingsDisplay.coerce(repeat_headings)
Expand Down Expand Up @@ -163,12 +163,20 @@ def row(self, cells=(), style=None):
def render(self):
"This is an internal method called by `fpdf.FPDF.table()` once the table is finished"
# Starting with some sanity checks:
self._cols_count = max(row.cols_count for row in self.rows) if self.rows else 0
if self._width is None:
if self._col_widths:
if isinstance(self._col_widths, Number):
self._width = self._cols_count * self._col_widths
else:
self._width = sum(self._col_widths)
else:
self._width = self._fpdf.epw
if self._width > self._fpdf.epw:
raise ValueError(
f"Invalid value provided width={self._width}: effective page width is {self._fpdf.epw}"
)
table_align = Align.coerce(self._align)
if table_align == Align.J:
if self._table_align == Align.J:
raise ValueError(
"JUSTIFY is an invalid value for FPDF.table() 'align' parameter"
)
Expand All @@ -191,10 +199,10 @@ def render(self):

# Defining table global horizontal position:
prev_x, prev_y, prev_l_margin = self._fpdf.x, self._fpdf.y, self._fpdf.l_margin
if table_align == Align.C:
if self._table_align == Align.C:
self._fpdf.l_margin = (self._fpdf.w - self._width) / 2
self._fpdf.x = self._fpdf.l_margin
elif table_align == Align.R:
elif self._table_align == Align.R:
self._fpdf.l_margin = self._fpdf.w - self._fpdf.r_margin - self._width
self._fpdf.x = self._fpdf.l_margin
elif self._fpdf.x != self._fpdf.l_margin:
Expand All @@ -204,13 +212,10 @@ def render(self):
xx = self._outer_border_margin[0]
cell_x_positions = [xx]
if self.rows:
self._cols_count = max(row.cols_count for row in self.rows)
for i in range(self._cols_count):
xx += self._get_col_width(0, i)
xx += self._gutter_width
cell_x_positions.append(xx)
else:
self._cols_count = 0

# Process any rowspans
row_info = list(self._process_rowpans_entries())
Expand All @@ -220,7 +225,7 @@ def render(self):
self._repeat_headings is TableHeadingsDisplay.ON_TOP_OF_EVERY_PAGE
)
self._fpdf.y += self._outer_border_margin[1]
for i, row in enumerate(self.rows):
for i in range(len(self.rows)):
pagebreak_height = row_info[i].pagebreak_height
# pylint: disable=protected-access
page_break = self._fpdf._perform_page_break_if_need_be(pagebreak_height)
Expand Down
Binary file not shown.
38 changes: 38 additions & 0 deletions test/table/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ def test_table_with_fixed_col_width(tmp_path):
assert_pdf_equal(pdf, HERE / "table_with_fixed_col_width.pdf", tmp_path)


def test_table_with_fixed_col_width_and_align(tmp_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pdf.table(col_widths=pdf.epw / 5, align="L") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
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)
with pdf.table(col_widths=pdf.epw / 5, align="R") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
pdf.add_page()
with pdf.table(col_widths=40, align="L") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
with pdf.table(col_widths=40, align="C") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
with pdf.table(col_widths=40, align="R") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum)
assert_pdf_equal(pdf, HERE / "table_with_fixed_col_width_and_align.pdf", tmp_path)


def test_table_with_varying_col_widths(tmp_path):
pdf = FPDF()
pdf.add_page()
Expand Down

0 comments on commit 98037cb

Please sign in to comment.