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

Table cells as Text Regions #1200

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions docs/Tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,13 @@ with pdf.table() as table:
row = table.row()
for j, datum in enumerate(data_row):
if j == 2 and i > 0:
row.cell(img=datum)
img_cell = row.cell()
img_cell.image(
name=datum,
height=pdf.font_size * 2,
keep_aspect_ratio=True,
align="CENTER",
)
else:
row.cell(datum)
pdf.output('table_with_images.pdf')
Expand All @@ -305,9 +311,12 @@ Result:

![](table_with_images.jpg)

By default, images height & width are constrained by the row height (based on text content)
and the column width. To render bigger images, you can set the `line_height` to increase the row height, or
pass `img_fill_width=True` to `.cell()`:
**Incompatible Change in release 2.7.7:**
Up to release 2.7.6, each table cell could only contain either a chunk of text or a single image. and images with `img_fill_width=False` were automatically scaled to match the height of the text cells in the same row.

With release 2.7.7, there is now complete freedom to combine text and images in a cell in any sequence. Because of that, this automatic image scaling doesn't make sense anymore and has been removed. To scale images, you now can use the `TableCell.image()` method and supply an explicit height (and/or width).

To scale any images to the full cell width, you can still use `img_fill_width=True` argument to `.cell()` or the `fill_width=True` argument to `TableCell.image()`.

```python
row.cell(img=datum, img_fill_width=True)
Expand Down
3 changes: 3 additions & 0 deletions fpdf/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ class TableSpan(CoerciveEnum):
COL = intern("COL")
"Mark this cell as a continuation of the previous column"

BOTH = intern("BOTH")
"Mark this cell as an overlap of a row- and columnspan."


class TableHeadingsDisplay(CoerciveIntEnum):
"Defines how the table headings should be displayed"
Expand Down
33 changes: 21 additions & 12 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -4063,6 +4063,7 @@ def text_columns(
text: Optional[str] = None,
img: Optional[str] = None,
img_fill_width: bool = False,
link: Optional[str] = None,
ncols: int = 1,
gutter: float = 10,
balance: bool = False,
Expand All @@ -4074,24 +4075,32 @@ def text_columns(
wrapmode: WrapMode = WrapMode.WORD,
skip_leading_spaces: bool = False,
):
"""Establish a layout with multiple columns to fill with text.
"""Establish a layout with multiple columns to fill with text and/or images.
Args:
text (str, optional): A first piece of text to insert.
ncols (int, optional): the number of columns to create. (Default: 1).
gutter (float, optional): The distance between the columns. (Default: 10).
balance: (bool, optional): Specify whether multiple columns should end at approximately
text (str; optional): A first piece of text to insert. Text with varying formatting can be added
with the `.write()` method of the instance.
img (str, bytes, io.BytesIO, PIL.Image.Image; optional)
a file path or URL, bytes or io.BytesIO representing raw image data, or PIL Image instance
to be inserted into the column. If both `text` and `img` arguments are given, the text will
be inserted first. Images can also be added with the `.image()` method.
link (str, int; optional): Link, either an URL or an integer returned by `FPDF.add_link`, defining
an internal link to a page. This link will apply only to any `text` or `img` arguments supplied,
and not to content added with `.write()` or `.image()`.
ncols (int; optional): the number of columns to create. (Default: 1).
gutter (float; optional): The distance between the columns. (Default: 10).
balance: (bool; optional): Specify whether multiple columns should end at approximately
the same height, if they don't fill the page. (Default: False)
text_align (Align or str, optional): The alignment of the text within the region.
text_align (Align or str; optional): The alignment of the text within the region.
(Default: "LEFT")
line_height (float, optional): A multiplier relative to the font size changing the
line_height (float; optional): A multiplier relative to the font size changing the
vertical space occupied by a line of text. (Default: 1.0).
l_margin (float, optional): Override the current left page margin.
r_margin (float, optional): Override the current right page margin.
print_sh (bool, optional): Treat a soft-hyphen (\\u00ad) as a printable character,
l_margin (float; optional): Override the current left page margin.
r_margin (float; optional): Override the current right page margin.
print_sh (bool; optional): Treat a soft-hyphen (\\u00ad) as a printable character,
instead of a line breaking opportunity. (Default: False)
wrapmode (fpdf.enums.WrapMode, optional): "WORD" for word based line wrapping,
wrapmode (fpdf.enums.WrapMode; optional): "WORD" for word based line wrapping,
"CHAR" for character based line wrapping. (Default: "WORD")
skip_leading_spaces (bool, optional): On each line, any space characters at the
skip_leading_spaces (bool; optional): On each line, any space characters at the
beginning will be skipped if True. (Default: False)
"""
return TextColumns(
Expand Down
Loading
Loading