From 8b89ca7f3a7d7a8cb8b270a6d58170673d2d782b Mon Sep 17 00:00:00 2001 From: Lucas Cimon <925560+Lucas-C@users.noreply.github.com> Date: Fri, 13 Jan 2023 18:19:58 +0100 Subject: [PATCH] Raising an error when trying to add a link with no page number --- fpdf/fpdf.py | 4 ++++ test/image/test_load_image.py | 2 +- test/image/test_oversized.py | 2 +- test/test_links.py | 9 +++++++++ 4 files changed, 15 insertions(+), 2 deletions(-) mode change 100755 => 100644 test/test_links.py diff --git a/fpdf/fpdf.py b/fpdf/fpdf.py index c7711bd49..95d765510 100644 --- a/fpdf/fpdf.py +++ b/fpdf/fpdf.py @@ -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, diff --git a/test/image/test_load_image.py b/test/image/test_load_image.py index 9e159dacf..88604d8ac 100644 --- a/test/image/test_load_image.py +++ b/test/image/test_load_image.py @@ -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 = {} diff --git a/test/image/test_oversized.py b/test/image/test_oversized.py index 5b2e8d197..3a82eac61 100644 --- a/test/image/test_oversized.py +++ b/test/image/test_oversized.py @@ -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): diff --git a/test/test_links.py b/test/test_links.py old mode 100755 new mode 100644 index c401eafe4..6e77a28d7 --- a/test/test_links.py +++ b/test/test_links.py @@ -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)