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

Fix: when adding a link on a table cell, an extra link was added erroneously on the left #1061

Merged
merged 1 commit into from
Dec 10, 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)