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

Creating Links in Table Cells #1031

Closed
demiurgepy opened this issue Nov 20, 2023 · 10 comments · Fixed by #1061
Closed

Creating Links in Table Cells #1031

demiurgepy opened this issue Nov 20, 2023 · 10 comments · Fixed by #1061
Assignees

Comments

@demiurgepy
Copy link

demiurgepy commented Nov 20, 2023

Hello FPDF Team,

First and foremost, I'd like to express my immense gratitude for the effort put into developing the FPDF library. It's an incredible tool that has been immensely helpful.

I am currently facing an issue with creating clickable hyperlinks in a table. Specifically, I have a table with three columns, where the second column consists of links. When these links extend over a line break, PDF readers only recognize and make the part of the link before the line break clickable. I need a solution that allows the entire link to be clickable.

I have followed the tutorial in the FPDF Tables Documentation, particularly the section about adding links to cells in a table:

row.cell(..., link="https://py-pdf.github.io/fpdf2/") row.cell(..., link=pdf.add_link(page=1))

However, the links in the table cells are not becoming clickable. They work fine outside the table using pdf.cell(text=text, link=link), but inside the table, they seem to be unresponsive, only the text is updated. Here is the snippet of my code:

def add_table(self, table_data):
    headings_style = FontFace(color=0, fill_color=(173, 216, 230))
    with self.pdf.table(
            headings_style=headings_style,
            text_align='LEFT',
            col_widths=(10, 10, 30)
    ) as table:
        for data_row in table_data:
            row = table.row()
            for index, item in enumerate(data_row):
                if index == 1:
                    row.cell(text=item, link=item)
                else:
                    row.cell(item)

I've also tried to replicate the example provided in the documentation, but I'm encountering the same issue – the text in the table cells is not clickable.

Could you please provide some guidance or suggestions on how to resolve this issue? Any help would be greatly appreciated!

Thank you in advance for your time and assistance.

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

Hi and welcome @demiurgepy 🙂

First and foremost, I'd like to express my immense gratitude for the effort put into developing the FPDF library. It's an incredible tool that has been immensely helpful.

Thank you! It makes me happy to know that this library helped you 😊

However, the links in the table cells are not becoming clickable. [...] Here is the snippet of my code:

You did not provide a code snippet that I can easily test 😢
Please provide a minimal reproducible example, that does not require to guess what is your surrounding code and the data you are using: https://stackoverflow.com/help/minimal-reproducible-example

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

I was able to craft a minimal script reproducing your issue, and this is indeed a bug:

#!/usr/bin/env python3
from fpdf import FPDF

TABLE_DATA = (
    ("First name", "Last name", "Age", "City"),
    ("Jules", "Smith", "34", "San Juan"),
    ("Mary", "Ramos", "45", "Orlando"),
    ("Carlson", "Banks", "19", "Los Angeles"),
    ("Lucas", "Cimon", "31", "Angers"),
)

pdf = FPDF()
pdf.set_font("helvetica", size=8)
pdf.add_page()
with pdf.table() as table:
    for data_row in TABLE_DATA:
        row = table.row()
        for item in data_row:
            row.cell(text=item, link="https://py-pdf.github.io/fpdf2/")
pdf.output("issue_1031.pdf")

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

This was a regression in fpdf2 release 2.7.6, brought by this PR: #797

I'm creating a PR to fix this now.

Thank you for the report @demiurgepy!

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

@allcontributors please add @demiurgepy for bug

Copy link

@Lucas-C

I've put up a pull request to add @demiurgepy! 🎉

Lucas-C added a commit that referenced this issue Nov 20, 2023
Lucas-C added a commit that referenced this issue Nov 20, 2023
@Lucas-C Lucas-C self-assigned this Nov 20, 2023
@demiurgepy
Copy link
Author

Sorry for not getting that minimal code over to you in time. Thanks a bunch for your quick response though – you guys are amazing!
It's really cool to see such active and helpful support in the community 🤜🤛

@demiurgepy
Copy link
Author

demiurgepy commented Dec 5, 2023

Hi again FPDF Team,

I'm back with an update and further inquiry about the table hyperlink issue I mentioned earlier. This time, I've noticed that when I place a hyperlink in the third column of a table, it strangely extends to the first column as well. This is happening despite my attempts to limit the hyperlink to just the third column.

A minimal script:

from fpdf import FPDF

TABLE_DATA = (
    ("First name", "Last name", "Age", "City"),
    ("Jules", "Smith", "34", "San Juan"),
    ("Mary", "Ramos", "45", "Orlando"),
    ("Carlson", "Banks", "19", "Los Angeles"),
    ("Lucas", "Cimon", "31", "Saint-Mahturin-sur-Loire"),
)
pdf = FPDF()
pdf.add_page()
pdf.set_font("Times", size=16)
with pdf.table() as table:
    for i, data_row in enumerate(TABLE_DATA):
        row = table.row()
        for j, datum in enumerate(data_row):
            if j == 2 and i > 0:
                row.cell(text=datum, link='https://py-pdf.github.io/fpdf2/')
            else:
                row.cell(datum)
pdf.output('table.pdf')

In this script, the hyperlink is intended only for the "Age" column, but it also activates in the "First name" column. I'm hoping to get some insight or guidance on how to address this issue.

Thanks again for your time and assistance.

@Lucas-C
Copy link
Member

Lucas-C commented Dec 5, 2023

Hi @demiurgepy

In this script, the hyperlink is intended only for the "Age" column, but it also activates in the "First name" column. I'm hoping to get some insight or guidance on how to address this issue.

I tried your code, and I do not reproduce the problem you are describing, sorry!
It correctly produces a PDF with an hyperlink set only on the numeric values of the 3rd column.

Are you using the latest version of fpdf2 from the master branch?
You can install it this way:

pip install git+https://github.com/py-pdf/fpdf2.git@master

@demiurgepy
Copy link
Author

Thanks Lucas for your prompt response and for taking the time to test the code. I appreciate your efforts to help diagnose the issue.

After receiving your message, I made sure to install the latest version of fpdf2 from the master branch as you suggested. However, I'm still encountering the same issue where a portion of the first column becomes clickable, despite the hyperlink being intended only for the third column. This behavior persists in the generated PDF, with the first few letters in the first column responding as part of the hyperlink.

Interestingly, a few other developers have also tested this on their systems and have reported experiencing the same issue. It seems to be a consistent behavior across different environments. This is a visual example, to give you a clearer idea:
image

Could there be a specific environment or PDF viewer factor at play here? Or might there be a subtle aspect of the library's behavior that we're overlooking?

@Lucas-C
Copy link
Member

Lucas-C commented Dec 10, 2023

Could there be a specific environment or PDF viewer factor at play here? Or might there be a subtle aspect of the library's behavior that we're overlooking?

No, you were right, there was still a bug related to this in fpdf2 😅
I fixed that in #1061

Sadly, it was not included as part of today's release: https://github.com/py-pdf/fpdf2/releases/tag/2.7.7
But if you install fpdf2 from this repo master branch, you should be able to test if that solves your problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants