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

Can i change the vertical space between lines inside a table? #1024

Closed
viniciustozzi opened this issue Nov 15, 2023 · 8 comments
Closed

Can i change the vertical space between lines inside a table? #1024

viniciustozzi opened this issue Nov 15, 2023 · 8 comments

Comments

@viniciustozzi
Copy link

viniciustozzi commented Nov 15, 2023

Hi, is it possible to modify the vertical space in between the lines of a text in a table? For example with the following snippet:

from fpdf import FPDF
from fpdf.fonts import FontFace

pdf = FPDF()
pdf.set_font("helvetica")
pdf.add_page()

table_data = [
    ("Title", "Value"),
    (
        "Test",
        "A really long description in this table where I can show that the vertical spacing is too big",
    ),
    (
        "Test",
        "    Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.",
    ),
]

with pdf.table(headings_style=FontFace(fill_color=235)) as table:
    for data_row in table_data:
        table.row(data_row)

pdf.output("test.pdf")

test.pdf

I think the vertical space between lines in the second column are a little too big, I would like to make it smaller. Thanks in advance for the help.

@Lucas-C
Copy link
Member

Lucas-C commented Nov 15, 2023

Have you tried passing line_height=10 to FPDF.table()?

cf. https://py-pdf.github.io/fpdf2/fpdf/fpdf.html#fpdf.fpdf.FPDF.table

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

Hi @viniciustozzi

Feedbacks and extra questions are welcome regarding this 😊

But without any answer from you we will close this issue at some point.

@viniciustozzi
Copy link
Author

viniciustozzi commented Nov 20, 2023

Hi @Lucas-C, sorry for the delay in my response. I tried what you suggested:

with self.table(
    headings_style=FontFace(fill_color=235), line_height=10
) as table:
    for data_row in table_data:
        table.row(data_row)

but it does not really solve the issue, I noticed that this is enough if the text has only one line, then the spacing between the text and the table border is smaller, but when I have a multiple line text as in this example, the vertical distance between the lines of text is still quite big.

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

but it does not really solve the issue, I noticed that this is enough if the text has only one line, then the spacing between the text and the table border is smaller, but when I have a multiple line text as in this example, the vertical distance between the lines of text is still quite big.

OK.
If what matters is "the spacing between the text and the table border", then you should consider setting some cell padding:
https://py-pdf.github.io/fpdf2/Tables.html#setting-cell-padding

However, if you want control over the line height on a per-row or per-cell basis, this is currently not supported.
But it's a feature that could potentially be added to fpdf2.

@viniciustozzi
Copy link
Author

I think it would be a nice feature to have. In the pdf file I uploaded as an example, I think it's weird that there is so much space between each line of text, but when I look at the padding documentation images in the link you provided there seems to be not much space between the lines inside a call in the table, I wonder why. Maybe it's the font I am using?

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

In the pdf file I uploaded as an example, I think it's weird that there is so much space between each line of text, but when I look at the padding documentation images in the link you provided there seems to be not much space between the lines inside a call in the table, I wonder why. Maybe it's the font I am using?

In fpdf2, the line spacing in tables is only controlled by one setting: line_height.
Fonts do not have any impact on this spacing.

Have you tried to lower the value of line_height below 10, to see how that reduce the line spacing?
Does that solve your problem?

@viniciustozzi
Copy link
Author

Oh I misunderstood, I assumed line_height was about the cell height but it makes more sense that it is about the text line. I tried now with different and smaller values and it solves my problem, thank you!

@Lucas-C
Copy link
Member

Lucas-C commented Nov 20, 2023

Oh I misunderstood, I assumed line_height was about the cell height but it makes more sense that it is about the text line. I tried now with different and smaller values and it solves my problem, thank you!

Great, glad that helped you 😊

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