vgalin / html2image

A package acting as a wrapper around the headless mode of existing web browsers to generate images from URLs and from HTML+CSS strings or files.
MIT License
344 stars 44 forks source link

Background is not transparent anymore #113

Open LukasReindlGit opened 1 year ago

LukasReindlGit commented 1 year ago

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')
SmartWashingMachine commented 1 year ago

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 1 year ago

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 1 year ago

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?