Skip to content

Commit

Permalink
Fix: when adding a link on a table cell, an extra link was added erro…
Browse files Browse the repository at this point in the history
…neously on the left (#1061)
  • Loading branch information
Lucas-C committed Dec 10, 2023
1 parent b8b92f7 commit 381c393
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 4 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Expand Up @@ -17,7 +17,8 @@ in order to get warned about deprecated features used in your code.
This can also be enabled programmatically with `warnings.simplefilter('default', DeprecationWarning)`.

## [2.7.8] - Not released yet

### Fixed
* When adding a link on a table cell, an extra link was added erroneously on the left. Moreover, now `FPDF._disable_writing()` properly disable link writing.

## [2.7.7] - 2023-12-10
### Added
Expand Down
4 changes: 3 additions & 1 deletion fpdf/fpdf.py
Expand Up @@ -111,7 +111,7 @@ class Image:
from .sign import Signature
from .structure_tree import StructureTreeBuilder
from .svg import Percent, SVGObject
from .syntax import DestinationXYZ, PDFDate
from .syntax import DestinationXYZ, PDFArray, PDFDate
from .table import Table
from .text_region import TextRegionMixin, TextColumns
from .util import get_scale_factor, Padding
Expand Down Expand Up @@ -3381,6 +3381,7 @@ def _disable_writing(self):
return
self._out = lambda *args, **kwargs: None
prev_page, prev_x, prev_y = self.page, self.x, self.y
annots = PDFArray(self.pages[self.page].annots)
self._push_local_stack()
try:
yield
Expand All @@ -3390,6 +3391,7 @@ def _disable_writing(self):
for p in range(prev_page + 1, self.page + 1):
del self.pages[p]
self.page = prev_page
self.pages[self.page].annots = annots
self.set_xy(prev_x, prev_y)
# restore writing function:
del self._out
Expand Down
Binary file added test/table/table_with_images_and_links.pdf
Binary file not shown.
Binary file modified test/table/table_with_links.pdf
Binary file not shown.
15 changes: 15 additions & 0 deletions test/table/test_table.py
Expand Up @@ -698,3 +698,18 @@ def test_table_with_fill_color_set_beforehand(tmp_path): # issue 932
style = None
row.cell(datum, style=style)
assert_pdf_equal(pdf, HERE / "table_with_fill_color_set_beforehand.pdf", tmp_path)


def test_table_with_links(tmp_path): # issue 1031
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
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(text=datum, link="https://py-pdf.github.io/fpdf2/")
else:
row.cell(datum)
assert_pdf_equal(pdf, HERE / "table_with_links.pdf", tmp_path)
4 changes: 2 additions & 2 deletions test/table/test_table_with_image.py
Expand Up @@ -164,7 +164,7 @@ def test_table_with_page_break_over_image(tmp_path):
assert_pdf_equal(pdf, HERE / "table_with_page_break_over_image.pdf", tmp_path)


def test_table_with_links(tmp_path):
def test_table_with_images_and_links(tmp_path):
pdf = FPDF()
pdf.set_font("Times", size=16)
pdf.add_page()
Expand All @@ -183,4 +183,4 @@ def test_table_with_links(tmp_path):
)
else:
row.cell(datum, link=pdf.add_link(page=1))
assert_pdf_equal(pdf, HERE / "table_with_links.pdf", tmp_path)
assert_pdf_equal(pdf, HERE / "table_with_images_and_links.pdf", tmp_path)

0 comments on commit 381c393

Please sign in to comment.