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

Table: row: link #826

Closed
Sil68 opened this issue Jun 23, 2023 · 2 comments
Closed

Table: row: link #826

Sil68 opened this issue Jun 23, 2023 · 2 comments

Comments

@Sil68
Copy link

Sil68 commented Jun 23, 2023

Hi,

First of all, you guys rock! Amazing package!

Currently I am trying to create a boilerplate template for some recurring reports, featuring amongst other a toc with links to each corresponding section.

For laying out the toc I'm facilitating the fpdf2 table class. According to the documentation found on the project home page, adding a link to a table row should be rather straight forward

row.cell(..., link=...)

Unfortunately this renders an error upon execution, stating the 'link' is an unexpected keyword.

Turning to the documentation for help, having a look at the tutorial, I tried to code shown there

`# https://pyfpdf.github.io/fpdf2/Tables.html
from fpdf import FPDF
from fpdf.fonts import FontFace

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=10)

blue = (0, 0, 255)
lightblue = (173, 216, 230)
grey = (128, 128, 128)
greyscale = 200
headings_style = FontFace(emphasis="ITALICS", color=blue, fill_color=grey)

pdf.set_draw_color(50) # very dark grey
pdf.set_line_width(.5)

with pdf.table(width=150, col_widths=(30, 30, 10, 30),
text_align=("CENTER", "CENTER", "RIGHT", "LEFT"),
line_height=2.5 * pdf.font_size,
headings_style=headings_style,
cell_fill_color=greyscale, cell_fill_mode="ROWS",
borders_layout="MINIMAL") as table:
for data_row in TABLE_DATA:
row = table.row()
for datum in data_row:
row.cell(datum, link="https://pyfpdf.github.io/fpdf2/Tables.html")

pdf.output('table.pdf')`

When running this sequence the following output is getting generated

`Python 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:17) [GCC 12.2.0]
Type 'copyright', 'credits' or 'license' for more information
IPython 8.14.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 8.14.0
Python 3.11.4 | packaged by conda-forge | (main, Jun 10 2023, 18:08:17) [GCC 12.2.0] on linux

import os
os.environ['KEYRING_CRYPTFILE_PASSWORD'] = 'usrpwd'
os.environ['USRENV'] = 'usrenv'
runfile('/home/user/projects/pdf/test.py', wdir='/home/user/projects/pdf')

Traceback (most recent call last):
File "/usr/local/miniconda3/envs/user/lib/python3.11/site-packages/IPython/core/interactiveshell.py", line 3508, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "", line 4, in
runfile('/home/user/projects/pdf/test.py', wdir='/home/user/projects/pdf')
File "/opt/pycharm-community/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/opt/pycharm-community/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "/home/user/projects/pdf/test.py", line 34, in
row.cell(datum, link="https://pyfpdf.github.io/fpdf2/Tables.html")
TypeError: Row.cell() got an unexpected keyword argument 'link'`

I am using FPDF2 v2.7.4.

Having the possibility of linking adding links to table rows would be kind of an nice to important thing to have working.

Cheers!

Sil

@Sil68 Sil68 added the question label Jun 23, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Jun 23, 2023

Hi @Sil68 and welcome!

Currently I am trying to create a boilerplate template for some recurring reports, featuring amongst other a toc with links to each corresponding section.

Have you considered using FPDF.insert_toc_placeholder()?
cf. https://pyfpdf.github.io/fpdf2/DocumentOutlineAndTableOfContents.html

Unfortunately this renders an error upon execution, stating the 'link' is an unexpected keyword.

This is because our documention is slightly in advance compared to the latest release... Sorry about this.
The feature you want to use has been added 4 days ago: #825

It is not released yet but you can test it before the next release using this command:
pip install git+https://github.com/PyFPDF/fpdf2.git@master

You can track fpdf2 releases there:

Do you have other questions?

@Sil68
Copy link
Author

Sil68 commented Jun 23, 2023

Hi Lucas,

Thanks for the quick response, much appreciated!

Yes, indeed, I'm using the placeholder method, along with the 'render toc' function.

Migrating to the most recent (development) version seems to have solved the issue; now it's working like a charm!

:)

Cheers!
Sil

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

No branches or pull requests

2 participants