vgalin/html2image

Background is not transparent anymore

LukasReindlGit opened this issue · 3 comments

When I ran this code I was used to get a transparent image with only the red text.
Now the background is white.
Is it a known issue based on some chrome version?
Since I am running my code in a docker container, I am quite flexible.

def renderImage(html, css, size, outputPath):
    hti = Html2Image(custom_flags=['--no-sandbox'])
    img = hti.screenshot(html_str=html, css_str=css, save_as='htmlToImage.png', size=size)
    os.chmod(img[0], 0o0777)
    shutil.move(img[0], outputPath)
    print('image created', outputPath)
    return outputPath

if __name__ == '__main__':
    html = '<html><body><h1>Hello, World!</h1></body></html>'
    css = 'h1 { color: red; }'
    renderImage(html, css, (720,1280), 'test.png')

I had the same issue. Not sure what version of Chrome is causing this but Adi's solution for another issue also ended up fixing this issue for me. (Windows)

Fix:

I've managed to bypass this on Windows by installing an older portable version of Chrome v109 - googlechromeportable64_109.0.5414.120_online.paf

(https://sourceforge.net/projects/portableapps/files/Google%20Chrome%20Portable/) and using in my program. I installed in the same folder.

hti=Html2Image(browser_executable='GoogleChromePortable64/App/Chrome-bin/chrome.exe')
hti.size=(2046,2046)
...
hti.screenshot(......)

Tested on latest html2image (2.0.3)

vgalin commented

For now, you can use the --default-background-color flag to force transparent background color, like so:

hti = Html2Image(custom_flags=['--no-sandbox', '--default-background-color=00000000'])
kzu commented

Would be nice to be able to pass those custom flags when using the CLI too. Right now, there doesn't seem to be a way to do so?