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

Backward compatibility for write_html regarding <table> header row style #740

Closed
Duspy99 opened this issue Mar 29, 2023 · 9 comments
Closed

Comments

@Duspy99
Copy link

Duspy99 commented Mar 29, 2023

FPDF.write_html() replaced with FPDF.table() ..
I got error on my live server that's reason why I open this issue.

@Duspy99 Duspy99 added the bug label Mar 29, 2023
@Lucas-C
Copy link
Member

Lucas-C commented Mar 29, 2023

Hi @Mario12335.

We did not rename the write_html() method into table().

I'd be happy to investigate a bit more if you could provide some more detail on the error you faced.
What was the error message? The stracktrace?
What version of fpdf2 do you use? What does your code looks like?

Without those crucial elements, your message is a bit incomprehensible / sounds like a baseless rant 😢

@Duspy99
Copy link
Author

Duspy99 commented Mar 29, 2023

FPDF.write_html() now uses the new FPDF.table() method to render <table> tags.

This is describe in newest release am I wrong?
Sry for late answer I didn't have time today.
I "fixed" it to lock version in poetry to 2.6.0 (poetry updated to newest one 2.7.1) btw this is the error:

I found warnings: ignoring width="15" specified on a td that is not in the first tr. That's problem.
@Lucas-C

@Lucas-C
Copy link
Member

Lucas-C commented Mar 30, 2023

I found warnings: ignoring width="15" specified on a td that is not in the first tr. That's problem.

OK, so this is a warning introduced in v2.7.0.
It comes from here: https://github.com/PyFPDF/fpdf2/blob/2.7.1/fpdf/html.py#L472

It is quite self-explanatory: it means that you used a width attribute in <td> tag that is not in the first row.

To fix this, you must simply define your table columns sizes inside the first <tr>.
There is an example of doing so in this unit test file: https://github.com/PyFPDF/fpdf2/blob/2.7.1/test/html/test_html.py#L152

I'd be happy to try to help you further, but you will have to provide some Python minimal code in order for me to understand what your use case is exactly 😊

By the way, this is what was recommend in the template you must have seen when you created this issue.
For reference, it is here: https://github.com/PyFPDF/fpdf2/blob/master/.github/ISSUE_TEMPLATE/bug_report.md

Please include some minimal Python code reproducing your issue:
If you don't know how to build a minimal reproducible example, please check this tutorial: https://stackoverflow.com/help/minimal-reproducible-example

@Lucas-C
Copy link
Member

Lucas-C commented Apr 12, 2023

Hi @Duspy99

Unless you provide more information, I'm planning on closing this issue.

@Duspy99
Copy link
Author

Duspy99 commented Apr 12, 2023

@Lucas-C
It works on the older version, but not on the new one, I mentioned the problem/warning above.
I don't know if you changed anything behind the scenes, but this is my output and it works on fpdf 2.6.0.
This is not original code there is some loops and logic for multiple/dynamic tables I cut it for cleaner code.

table_str = f"""
     <table>
     <tbody>
     <tr>
         <td width=15><b>Client</b></td>
         <td width=100><b>{reservation.client.name}</b></td>
         <td width=30><b>Status</b></td>
         <td width=45 align=right>{reservation.reservation_status}</td>
     </tr>
     <tr>
         <td width=15></td>
         <td width=100></td>
         <td width=30><b>Reservation #</b></td>
         <td width=45 align=right>{reservation.code}</td>
     </tr>
     <tr>
         <td width=15></td>
         <td width=100></td>
         <td width=30><b>Type</b></td>
         <td width=45 align=right>{reservation.reservation_type.name}</td>
     </tr>
 
     </tbody>
     </table>
     """
self.write_html(
   table_str
)

I printed output in console:

<table>
  <tbody>
    <tr>
      <td width="15"><b>Client</b></td>
      <td width="100"><b>some name</b></td>
      <td width="30"><b>Status</b></td>
      <td width="45" align="right">some status</td>
    </tr>
    <tr>
      <td width="15"></td>
      <td width="100"></td>
      <td width="30"><b>Reservation </b></td>
      <td width="45" align="right">some code</td>
    </tr>
    <tr>
      <td width="15"></td>
      <td width="100"></td>
      <td width="30"><b>Type</b></td>
      <td width="45" align="right">some type</td>
    </tr>
  </tbody>
</table>

@Lucas-C
Copy link
Member

Lucas-C commented Apr 12, 2023

Thank you for providing some example HTML code 😊

A self-sufficient autonomous Python script would have been ideal,
but I was able to build one easily based on the HTML <table> you provided:

from fpdf import FPDF, FPDF_VERSION

pdf = FPDF()
pdf.add_page()
pdf.set_font("Helvetica", size=12)
pdf.write_html("""<table>
  <tbody>
    <tr>
      <td width="15"><b>Client</b></td>
      <td width="100"><b>some name</b></td>
      <td width="30"><b>Status</b></td>
      <td width="45" align="right">some status</td>
    </tr>
    <tr>
      <td width="15"></td>
      <td width="100"></td>
      <td width="30"><b>Reservation </b></td>
      <td width="45" align="right">some code</td>
    </tr>
    <tr>
      <td width="15"></td>
      <td width="100"></td>
      <td width="30"><b>Type</b></td>
      <td width="45" align="right">some type</td>
    </tr>
  </tbody>
</table>""")
pdf.output(f"issue_740-{FPDF_VERSION}.pdf")

This is the output I get with fpdf2 2.6.0:
issue_740-2 6 0

And the output I get with fpdf2 2.7.3:
issue_740-2 7 3

I see 2 differences:

  • the header row line is now all bold
  • there is an horizontal line below the header row

This is because you used <td> tags in the top row, and not <th> tags.
Prior to version 2.7 of fpdf2, this used to make a difference on the header row style,
but currently we always render it in bold with an horizontal line, even if there are only <td> cells and <th> is not explicitely used.

Based on some quick tests I made using both Chrome & Firefox,
the default style applied by those browsers is:

  • never display an horizontal line under the top header row
  • if <th> is used, center-align text in the header row cells and make them bold

Based on those observations, I am willing to slightly change fpdf2 behaviour regarding <table> header row styling.

@Duspy99: do you only care about backward-compatibility in terms of visual aspect,
or is there some specific behaviour that you care about regarding bold and/or the horitontal line on the <table> header row?

@Lucas-C Lucas-C self-assigned this Apr 12, 2023
@Lucas-C Lucas-C removed their assignment Apr 12, 2023
@Lucas-C Lucas-C changed the title Backward compatibility for write_html Backward compatibility for write_html regarding <table> header row style Apr 12, 2023
@gmischler
Copy link
Collaborator

This is because you used <td> tags in the top row, and not <th> tags.

It looks like a good idea to allow cells anywhere in the table to be a "header cell", and to use <th> to trigger its use.
People do weird things with tables, placing "headers" wherever they want, or even none at all... 😉

@Lucas-C
Copy link
Member

Lucas-C commented Apr 12, 2023

@Duspy99: I have just merged a PR that should improve backward compatibility.

Could you please test it and tell us if that solves you issue?

pip install git+https://github.com/PyFPDF/fpdf2.git@master

@Duspy99
Copy link
Author

Duspy99 commented Apr 13, 2023

@Duspy99: I have just merged a PR that should improve backward compatibility.

Could you please test it and tell us if that solves you issue?

pip install git+https://github.com/PyFPDF/fpdf2.git@master

Check discord @Lucas-C

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

3 participants