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

insert_link() can't insert a link to the last page in a pdf document #2903

Closed
jackb007 opened this issue Dec 17, 2023 · 1 comment
Closed
Labels
not a bug not a bug / user error / unable to reproduce

Comments

@jackb007
Copy link

jackb007 commented Dec 17, 2023

Description of the bug

When an attempt is made to pass a link to the insert_link method pointing to the last page in a document, an error message is returned and the program exits. This problem is caused by an off-by-one error. The fitz.Document numbering of fitz.Page objects is 0-based, while the numbering used by pdf readers is 1-based. The problem is solved by changing the following lines in the getLinkText function in the utils.py file:

  1. The two occurences of the line
    if lnk["page"] >= 0:
    should be changed to
    if lnk["page"] >= 1:

  2. The line
    pno = lnk["page"]
    should be changed to
    pno = lnk["page"] - 1

The fix was tested and is working correctly.

How to reproduce the bug

How to reproduce the bug:
Attempt to pass a valid link to the insert_link() method pointing to the last page in a pdf document. The method will return an error message and the program will terminate.

PyMuPDF version

1.23.7

Operating system

Windows

Python version

3.9

@julian-smith-artifex-com
Copy link
Collaborator

The "page" entry in a link dictionary is defined to be zero-based - see https://pymupdf.readthedocs.io/en/latest/page.html#description-of-get-links-entries.

So you need to pass in zero-based page numbers in lnk["page"].

@julian-smith-artifex-com julian-smith-artifex-com added the not a bug not a bug / user error / unable to reproduce label Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
not a bug not a bug / user error / unable to reproduce
Projects
None yet
Development

No branches or pull requests

2 participants