Skip to content

Commit

Permalink
Raising an error when trying to add a link with no page number
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas-C committed Jan 13, 2023
1 parent 479afb8 commit 8b89ca7
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 2 deletions.
4 changes: 4 additions & 0 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2058,6 +2058,10 @@ def link(self, x, y, w, h, link, alt_text=None, border_width=0):
link in self.links
), f"Link with an invalid index: {link} (doc #links={len(self.links)})"
dest = self.links[link]
if not dest.page_number:
raise ValueError(
f"Cannot insert link {link} with no page number assigned"
)
link_annot = AnnotationDict(
"Link",
x=x * self.k,
Expand Down
2 changes: 1 addition & 1 deletion test/image/test_load_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_load_invalid_base64_data():


# ensure memory usage does not get too high - this value depends on Python version:
@memunit.assert_lt_mb(141)
@memunit.assert_lt_mb(145)
def test_share_images_cache(tmp_path):
images_cache = {}

Expand Down
2 changes: 1 addition & 1 deletion test/image/test_oversized.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

HERE = Path(__file__).resolve().parent
IMAGE_PATH = HERE / "png_images/6c853ed9dacd5716bc54eb59cec30889.png"
MAX_MEMORY_MB = 143 # memory usage depends on Python version
MAX_MEMORY_MB = 145 # memory usage depends on Python version


def test_oversized_images_warn(caplog):
Expand Down
9 changes: 9 additions & 0 deletions test/test_links.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,12 @@ def test_inserting_link_to_non_exising_page():
)
with pytest.raises(ValueError):
pdf.output()


def test_inserting_link_with_no_page_number():
pdf = FPDF()
link = pdf.add_link()
pdf.add_page()
pdf.set_font("helvetica", size=12)
with pytest.raises(ValueError):
pdf.cell(txt="Page 1", link=link)

0 comments on commit 8b89ca7

Please sign in to comment.