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

Allow add_link() to be called before add_page(). #1337

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ This can also be enabled programmatically with `warnings.simplefilter('default',
## [2.8.3] - Not released yet
### Fixed
* [`FPDF.write_html()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.write_html): Fixed rendering of content following `<a>` tags; now correctly resets emphasis style post `</a>` tag: hyperlink styling contained within the tag authority. - [Issue #1311](https://github.com/py-pdf/fpdf2/issues/1311)
* [`FPDF.add_link()`](https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.add_link): It is now safe to create links before adding pages with `FPDF.add_page()`.

## [2.8.2] - 2024-12-16
### Added
Expand Down
7 changes: 5 additions & 2 deletions fpdf/fpdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -2219,12 +2219,15 @@ def add_link(self, y=0, x=0, page=-1, zoom="null"):
x (float): optional abscissa of target position.
The default value is 0 (top of page).
page (int): optional number of target page.
-1 indicates the current page, which is the default value.
-1 indicates the current page (or the first page if there are no pages yet),
which is the default value.
zoom (float): optional new zoom level after following the link.
Currently ignored by Sumatra PDF Reader, but observed by Adobe Acrobat reader.
"""
if page < 0:
page = self.page if self.page != 0 else 1
link = DestinationXYZ(
self.page if page == -1 else page,
page,
top=self.h_pt - y * self.k,
left=x * self.k,
zoom=zoom,
Expand Down
18 changes: 12 additions & 6 deletions test/test_links.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,20 @@ def test_inserting_link_to_non_exising_page():
pdf.output()


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

link_to_section1 = pdf.add_link()

pdf.add_page() # page 1
pdf.cell(text="Section 1", link=link_to_section1)

pdf.add_page() # page 2
pdf.set_link(link_to_section1, page=pdf.page)
pdf.cell(text="Section 1: Bla bla bla")

assert_pdf_equal(pdf, HERE / "later_call_to_set_link.pdf", tmp_path)
Copy link
Member

@Lucas-C Lucas-C Jan 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please provide the reference file later_call_to_set_link.pdf as part of this PR, please?

You just need to pass generate=True to this function call in order to generate it:
https://py-pdf.github.io/fpdf2/Development.html#generating-pdf-files-for-testing

You will also need to run black test/test_links.py in order to pretty-format this file.


def test_later_call_to_set_link(tmp_path): # v2.6.1 bug spotted in discussion 729
pdf = FPDF()
Expand Down
Loading