My main goal is to save pandas dataframe with html and scc styles as image. I'm having problems imgkit so I tried html2image. And it worked for me. But a few days later the same code doesn't do anything. No error either. I even created a new venv with html2imageonly and ran simple code from other examples
from html2image import Html2Image
hti = Html2Image()
html = '<h1> A title </h1> Some text.'
css = 'body {background: red;}'
# screenshot an HTML string (css is optional)
hti.screenshot(html_str=html, css_str=css, save_as='page.png')
However, it doesn't save anything, and just finishes programm with "Process finished with exit code 0". I also tried looking in Temp\html2image and only found file from when the pachage was working. Tried downgrading to 2.0.4, still no results.
First version of code looked like this
html = s.set_td_classes(cell_color).to_html()
root = get_project_root()
image_path = pathlib.Path(root, 'resources')
hti = Html2Image(output_path=image_path)
image_name = 'top_5_table.png'
hti.screenshot(html_str=html, css_str=CSS, save_as=image_name)
And it stopped working. I've tried removing css_str, changing output_path, still no result
I'm trying all of the versions, 1.1.3 and 2.0.3 at least save html files, not png, and to Temp\html2image
There has recently been updates to Chromium which are causing this problem. As of today (September 7th, 2024) the issue appears to affect both Chrome and Edge browser based on Chromium version 128.0.x.x and higher. There is no resolution at this point other than downgrading the browser version or embedding Chromium or a portable version of Chrome directly into your app. This GitHub issue has been created to track the problem.
Update (September 9th 2024):
Version 2.0.5 of html2image has been released and it addresses the problem identified by the OP.
Update (January 26th, 2025):
Recent updates to Chromium headless mode are causing the problem again. Version 132 has completely removed "old headless mode" (which was essentially a separate executable) in favor of "new headless mode" which uses the same executable for headless and non-headless modes. The current version of Html2Image (2.0.5) does not work with new headless mode, but you can use the browser_executable argument to point it to an executable that does support old headless mode. You have several options there:
Google's solution is "chrome-headless-shell" which supports old headless mode. It can be downloaded via NXP with the command npx @puppeteer/browsers install chrome-headless-shell@stable.
If you don't want to use NXP, but still want to use Google's "chrome-headless-shell", you can use the Python script found here to download the binaries.
You can ditch Chrome and instead use Chromium. Google searches break it down like this:
Here are links to the binaries that most people will be interested in. These are both version 131.0.6778.0; the last build of version 131. I've confirmed both of these builds work with Html2Image 2.0.5. Additionally, the permissive Chromium license allows these binaries to be legally distributed with your app, unlike Chrome.
Version 2.0.5 of html2image can be made to work with Chrome 1.333.0.x by setting the internal use_new_headless flag to None after construction:
hti = Html2Image()
hti.browser.use_new_headless = None
hti.browser.print_command = True # prints the command line used to perform the screenshot
hti.screenshot(html_file='input.html', save_as='output.png')
This causes the --headless flag to be passed to Chrome, instead of the default --headless=old. The latter causes Chrome to exit with a status code of 1.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With