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

In a HTML table, bgcolor is affecting text colour #512

Closed
dmail00 opened this issue Aug 29, 2022 · 4 comments
Closed

In a HTML table, bgcolor is affecting text colour #512

dmail00 opened this issue Aug 29, 2022 · 4 comments

Comments

@dmail00
Copy link

dmail00 commented Aug 29, 2022

Describe the bug
In a table bgcolor is affecting text colour.

Error details
The background is being set at [1] using box_shadow, and box_shadow [2] is getting the current fill colour changing it and then resetting it. However, set_fill_colour does more [3] as shown below, so just caching and reseting the colour is not enough

        if self.page > 0:
            self._out(self.fill_color.pdf_repr().lower())

As a hack I changed shadow_box to the following and it is rendered correctly:

    def box_shadow(self, w, h, bgcolor):
        LOGGER.debug("box_shadow w=%d h=%d bgcolor=%s", w, h, bgcolor)
        if bgcolor:
            fill_color = self.pdf.fill_color
            self.pdf.set_fill_color(*bgcolor)
            self.pdf.rect(self.pdf.x, self.pdf.y, w, h, "F")
            self.pdf.fill_color = fill_color
            self.pdf._out(self.pdf.fill_color.pdf_repr().lower())  # added

[1] https://github.com/PyFPDF/fpdf2/blob/master/fpdf/html.py#L347
[2] https://github.com/PyFPDF/fpdf2/blob/d840cf16ad0199a53d507f905d6d19d139a2147b/fpdf/html.py#L396
[3] https://github.com/PyFPDF/fpdf2/blob/d840cf16ad0199a53d507f905d6d19d139a2147b/fpdf/fpdf.py#L961

Minimal code
Please include some minimal Python code reproducing your issue:

    <thead>
        <tr>
            <th width="25%">Career</th>
            <th width="75%">Quote</th>
        </tr>
    </thead>
    <tbody>
        <tr bgcolor="grey"><td>Engineer</td><td>The engineer has been, and is, a maker of history.</td></tr>
        <tr bgcolor="white"><td>Developer</td><td>Logical thinking, passion and perseverance is the paint on your palette.</td></tr>
        <tr bgcolor="grey"><td>Analyst</td><td>Seeing what other people can't see gives you great vision.</td></tr>
        <tr bgcolor="white"><td><i>None of the above</i></td><td>I'm sorry. We could not find a quote for your job.</td></tr>
    </tbody>
</table>
...

If you don't know how to build a minimal reproducible example, please check this tutorial: https://stackoverflow.com/help/minimal-reproducible-example

Environment

  • OS (Windows, Mac OSX, Linux flavour...) Windows
  • Python version 3.9
  • fpdf2 version used 2.5.6

Side note: During looking at this, I noticed that I can not set a border for cells that is respected, but that is a seperate issue.

@dmail00 dmail00 added the bug label Aug 29, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Aug 29, 2022

Hi @dmail00!

Thank you for reporting this bug, and for the detailed error report!
I was able to reproduce the problem easily with your example.

Your fix was functional, but I think that instead of a call to the private method .out(),
a "cleaner" fix could be to replace the line self.pdf.fill_color = fill_color in HTML2FPDF.box_shadow by this:

self.pdf.set_fill_color(*fill_color.colors)

Would you like to submit a PR fixing this issue, including a short unit test?
If not, I'll perform the fix. As you wish.

@Lucas-C Lucas-C added the html label Aug 29, 2022
@Lucas-C
Copy link
Member

Lucas-C commented Aug 29, 2022

@all-contributors please add @dmail00 for bug

@allcontributors
Copy link

@Lucas-C

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

@Lucas-C
Copy link
Member

Lucas-C commented Sep 8, 2022

This has been released in v2.5.7

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