skip to Main Content

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 Temphtml2image 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 Temphtml2image

2

Answers


  1. The Chrome Driver may need to be properly configured.

    1. Based on this document, install the Chrome Driver that matches your environment.

      • Mac users with Homebrew installed: brew install --cask chromedriver
      • Debian based Linux distros: sudo apt-get install chromium-driver
      • Windows users with Chocolatey installed: choco install chromedriver
    2. Also based on the document, get the Chrome Driver executable_path according to your operating system.

      • Linux: /usr/bin/google-chrome
      • Mac: /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
      • Windows: Vista and newer: C:Users%USERNAME%AppDataLocalGoogleChromeApplicationchrome.exe

      However, it’s might be a little different in practical situation. Dig into the similar folder and find the actual chromedriver.
      In my case, it’s named as /usr/bin/chromedriver.

    3. Enter the executable_path path found above or specify the browser type.

      from html2image import Html2Image
      hti = Html2Image(
          browser_executable=<executable-path-found-above>, 
          custom_flags=[
              '--no-sandbox', 
              '--headless', 
              '--disable-gpu', 
              '--disable-software-rasterizer', 
              '--disable-dev-shm-usage'
          ])
      
      # OR
      
      hti = Html2Image(
          browser="chrome", 
          custom_flags=[
              '--no-sandbox', 
              '--headless', 
              '--disable-gpu', 
              '--disable-software-rasterizer', 
              '--disable-dev-shm-usage'
          ])
      
      html = "<h1> An interesting title </h1> This page will be red"
      css = """body {background: red;}"""
      
      hti.screenshot(html_str=html, css_str=css, save_as='red_page.png')
      
    Login or Signup to reply.
  2. There has recently been updates to Chromium which are causing this problem. It 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. This GitHub issue has been created to track the problem.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search