Skip to content

Links in PDF's created using Story do not work if they use "text=" attribute #3220

@wfsaxton

Description

@wfsaxton

Description of the bug

When creating a link without a text attribute:

with body.add_paragraph() as paragraph:
        paragraph.add_link(myurl)

Links are created and clickable. When using a text attribute however:

with body.add_paragraph() as paragraph:
        paragraph.add_link(myurl, text=mytext)

It looks like a link is created (blue and underlined) but you can't actually interact with it.

How to reproduce the bug

# PDF settings
WIDTH: Final[int] = 360
HEIGHT: Final[int] = 576
MARGIN: Final[int] = 18

# size and position the PDF pages
MEDIABOX: Final[Rect] = fitz.Rect(0, 0, WIDTH, HEIGHT)

# set the margins
WHERE: Final[Rect] = MEDIABOX + (  # noqa: RUF005 - ignoring this rule because its incorrect!
    MARGIN,
    MARGIN,
    -MARGIN,
    -MARGIN,
)

story = fitz.Story()

body = story.body

with body.add_paragraph() as paragraph:
        paragraph.add_link("https://github.com/", text="Github Home Page")

temp_pdf = BytesIO()

    with fitz.DocumentWriter(temp_pdf) as writer:
        more = 1
        pno = 1
        while more:
            device = writer.begin_page(MEDIABOX)
            more, _ = story.place(WHERE)
            story.draw(device)

            writer.end_page()
            pno += 1

    temp_pdf.seek(0)

PyMuPDF version

1.23.25

Operating system

Linux

Python version

3.11

Metadata

Metadata

Assignees

No one assigned

    Labels

    not a bugnot a bug / user error / unable to reproduce

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions