Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML to image html2image doesn't save files

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

like image 701
Maria Luchkova Avatar asked Oct 26 '25 13:10

Maria Luchkova


2 Answers

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:

  1. 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.

  2. 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.

  3. You can ditch Chrome and instead use Chromium. Google searches break it down like this:

    • Version 112 is the last version that had old headless mode and did not have new headless mode.
    • Version 131 is the last version to have old headless mode and new headless mode.
    • Version 132 is the first version to have new headless mode without old headless mode.

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.

  • Win_x64
  • Mac Arm
like image 122
ubiquibacon Avatar answered Oct 29 '25 02:10

ubiquibacon


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.

like image 38
tanderson Avatar answered Oct 29 '25 04:10

tanderson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!