question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

background-image renders on top of other content

See original GitHub issue

I use a background-image on the @page. In the past this has worked fine, but as of xhtml2pdf 0.2.7, the image now renders on top of all the other content, and because my image is fully opaque, that means none of the content is visible.

For what it’s worth, I think that a CSS property named “background-image” should render the image in … well, in the background 😃

Reverting to 0.2.6 solves the problem for me.

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
tanveerabidcommented, Jul 24, 2022

Hi

I have achieved a solution by editing default.py from xhtml2pdf.

Background of Problem: Parser.py use’s `def getColor(value, default=None):

if value is None:
    return
if isinstance(value, Color):
    return value
value = str(value).strip().lower()
if value == "transparent" or value == "none":
    return default
if value in COLOR_BY_NAME:
    return COLOR_BY_NAME[value]
if value.startswith("#") and len(value) == 4:
    value = "#" + value[1] + value[1] + \
        value[2] + value[2] + value[3] + value[3]
elif rgb_re.search(value):
    r, g, b = [int(x) for x in rgb_re.search(value).groups()]
    value = "#%02x%02x%02x" % (r, g, b)
else:
    pass

return toColor(value, default)  # Calling the reportlab function

` method in xhtml2pdf which is resided in util.py, has a condition, that if user set background-color: transparent; than simply replace background color with #ffffff(white Background Color). as p element by default doesn’t support background-color property in xhtml2pdf. so while parsing CSS p element inherits default HTML attributes where default background-color property is set to transparent. which invokes if statement in parser.py via util.py & changes it background to white. Screenshot from 2022-07-24 15-00-55 Screenshot from 2022-07-24 15-46-41

To overcome this firstly add background-color attribute to p element in default.py so it can support background color next time.
Screenshot from 2022-07-24 15-12-24 Then remove

background-color: transparent; attribute of HTML element from DEFAULT_CSS variable in default.py file.

Screenshot from 2022-07-24 15-17-12 i have already removed it.

now its all done. now if you want transparent background just simply don’t used background-color: transparent; as it revokes if statement to change background color to white. you can set any other color by passing it value(avoid using transparent).

HTML elements by default have transparent background color. here if statement is setting background color to white we can get transparent background by removing if statement but there 's a catch that you cannot set any other background color as this statement handles the both. then if someone set background color to transparent it throws error.

I added many custom attributes in default.py and these are working fine and also added a card size page of 86mm x 54mm by editing reportlab, so you can do customization just keep track of then to redo changes in case of error.

Have a good day.

0reactions
benedikt-bartschercommented, Jun 26, 2022

I have the same issue with version 0.2.8. The background-image works again but all the other elements appear with a white background. Can you help me? grafik

I recognised when I use table html Elements background becomes transparent again.

related to https://github.com/xhtml2pdf/xhtml2pdf/issues/605

Read more comments on GitHub >

github_iconTop Results From Across the Web

css - Elevate background image over content - Stack Overflow
The background-image property of css is used to put an image at the background of an element. To put an image in the...
Read more >
background-position - CSS: Cascading Style Sheets | MDN
The background-position CSS property sets the initial position for each background image. The position is relative to the position layer set ...
Read more >
Mastering CSS image overlay | A Practical Guide - ImageKit.io
Image overlay effects using CSS. Just like we added text on top of images, we can also add images over other images using...
Read more >
How to Position One Image on Top of Another in HTML/CSS
Sometimes, you may need to place one image over another one. It can be easily done with HTML and CSS. See how to...
Read more >
CSS background-position property - W3Schools
The background-position property sets the starting position of a background image. Tip: By default, a background-image is placed at the top-left corner of ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found