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

Allowing to add links to table cells #825

Merged
merged 1 commit into from
Jun 19, 2023
Merged
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
## [2.7.5] - Not released yet
### Added
- [`FPDF.mirror()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.mirror) - New method: [documentation page](https://pyfpdf.github.io/fpdf2/Transformations.html) - Contributed by @sebastiantia
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): new optional parameters `gutter_height`, `gutter_width` and `wrapmode`
- [`FPDF.table()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table): new optional parameters `gutter_height`, `gutter_width` and `wrapmode`. Links can also be added to cells by passing a `link` parameter to [`Row.cell()`](https://pyfpdf.github.io/fpdf2/fpdf/table.html#fpdf.table.Row.cell)
- [`FPDF.multi_cell()`](https://pyfpdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.multi_cell): has a new optional `center` parameter to position the cell horizontally at the center of the page
- Added Tutorial in Khmer language - thanks to @kuth-chi
### Fixed
Expand Down
7 changes: 6 additions & 1 deletion docs/Tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ Result:

![](table_with_images_and_img_fill_width.jpg)

## Adding links to cells
```python
row.cell(..., link="https://pyfpdf.github.io/fpdf2/")
row.cell(..., link=pdf.add_link(page=1))
```

## Syntactic sugar
To simplify `table()` usage, shorter, alternative usage forms are allowed.

Expand Down Expand Up @@ -222,7 +228,6 @@ Result:
![](table_with_gutter.jpg)

## Column span

Cells spanning multiple columns can be defined by passing a `colspan` argument to `.cell()`.
The cells that are overwritten by the spanned cell are not rendered but must be added to the table.

Expand Down
17 changes: 15 additions & 2 deletions fpdf/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ def _render_table_cell(
w=col_width,
h=0 if cell.img_fill_width else row_height,
keep_aspect_ratio=True,
link=cell.link,
).rendered_height
self._fpdf.set_xy(x, y)
text_align = cell.align or self._text_align
Expand Down Expand Up @@ -278,6 +279,7 @@ def _render_table_cell(
txt=cell.text,
max_line_height=self._line_height,
border=self.get_cell_border(i, j),
link=cell.link,
align=text_align,
new_x="RIGHT",
new_y="TOP",
Expand Down Expand Up @@ -348,7 +350,14 @@ def cols_count(self):
return sum(cell.colspan for cell in self.cells)

def cell(
self, text="", align=None, style=None, img=None, img_fill_width=False, colspan=1
self,
text="",
align=None,
style=None,
img=None,
img_fill_width=False,
colspan=1,
link=None,
):
"""
Adds a cell to the row.
Expand All @@ -363,6 +372,8 @@ def cell(
img_fill_width (bool): optional, defaults to False. Indicates to render the image
using the full width of the current table column.
colspan (int): optional number of columns this cell should span.
link (str, int): optional link, either an URL or an integer returned by `FPDF.add_link`, defining an internal link to a page

"""
if text and img:
raise NotImplementedError(
Expand All @@ -375,7 +386,7 @@ def cell(
font_face = self._fpdf.font_face()
if font_face != self.style:
style = font_face
cell = Cell(text, align, style, img, img_fill_width, colspan)
cell = Cell(text, align, style, img, img_fill_width, colspan, link)
self.cells.append(cell)
return cell

Expand All @@ -390,13 +401,15 @@ class Cell:
"img",
"img_fill_width",
"colspan",
"link",
)
text: str
align: Optional[Union[str, Align]]
style: Optional[FontFace]
img: Optional[str]
img_fill_width: bool
colspan: int
link: Optional[Union[str, int]]

def write(self, text, align=None):
raise NotImplementedError("Not implemented yet")
Binary file added test/table/table_with_links.pdf
Binary file not shown.
22 changes: 22 additions & 0 deletions test/table/test_table_with_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,25 @@ def test_table_with_page_break_over_image(tmp_path):
else:
row.cell(datum)
assert_pdf_equal(pdf, HERE / "table_with_page_break_over_image.pdf", tmp_path)


def test_table_with_links(tmp_path):
pdf = FPDF()
pdf.set_font("Times", size=16)
pdf.add_page()
pdf.y = 100
pdf.cell(txt="Page 1", center=True)
pdf.add_page()
with pdf.table() as table:
for i, data_row in enumerate(TABLE_DATA):
row = table.row()
for j, datum in enumerate(data_row):
if j == 2 and i > 0:
row.cell(
img=datum,
img_fill_width=True,
link="https://pyfpdf.github.io/fpdf2/",
)
else:
row.cell(datum, link=pdf.add_link(page=1))
assert_pdf_equal(pdf, HERE / "table_with_links.pdf", tmp_path)