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

Browser executable check fails on RHEL for chromium-headless #56

Open TytoCapensis opened 2 years ago

TytoCapensis commented 2 years ago

I am using a Red Hat Enterprise Linux machine (version 8.3) with Python 3.6.8 and the current version of html2image.

On this distribution, several choices are presented when we want to install Chromium on it :

chromium.x86_64 96.0.4664.110-2.el8 epel
chromium-common.x86_64 96.0.4664.110-2.el8 epel
chromium-headless.x86_64 96.0.4664.110-2.el8 epel

I went for the last option since it looked the most convenient for our use case.
Instead of the usual Chromium package which install the full browser with its standard executable, this package installs only a Chromium "headless shell", located in /usr/lib64/chromium-browser/headless_shell

The issue I have is that, unlike the classic Chromium versions, this executable fails the executable check in html2image:

>>> from html2image import Html2Image
>>> hti = Html2Image(browser_executable='/usr/lib64/chromium-browser/headless_shell', custom_flags=['--headless', '--disable-gpu', '--disable-software-rasterizer', '--default-background-color=ffffffff'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/html2image/html2image.py", line 86, in __init__
    flags=custom_flags,
  File "/usr/local/lib/python3.6/site-packages/html2image/browsers/chrome.py", line 187, in __init__
    self.executable = executable
  File "/usr/local/lib/python3.6/site-packages/html2image/browsers/chrome.py", line 204, in executable
    self._executable = _find_chrome(value)
  File "/usr/local/lib/python3.6/site-packages/html2image/browsers/chrome.py", line 94, in _find_chrome
    'Failed to find a seemingly valid chrome executable '
FileNotFoundError: Failed to find a seemingly valid chrome executable in the given path.

This is because /usr/lib64/chromium-browser/headless_shell --version apparently does not give the Chromium version as expected. It should be noted that, if I bypass the check by commenting it, everything works fine except that (I am able to make "screenshots" of pages normally).

I am unsure how this issue could be correctly fixed, maybe a special flag or condition could be implemented.

vgalin commented 2 years ago

Hello, thank you for opening an issue.


Edit : I don't know why I didn't thought of it right away, but the more practical way would be to change the value of the browser's _executable attribute :

>>> from html2image import Html2Image
>>> hti = Html2Image()
>>> hti.browser
<html2image.browsers.chrome.ChromeHeadless object at 0x000001896B3A9490>
>>> hti.browser._executable
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
>>> hti.browser._executable = YOUR_NEW_EXECUTABLE_PATH

Other method that should still work:


A way to temporarily solve your problem would be to set the HTML2IMAGE_TOGGLE_ENV_VAR_LOOKUP environment variable to any value (example : true), and set an environment variable with any of the following names :

Although this feature has yet to be documented and tested, it should do the trick, as the path provided using this method are not subject to any tests.


Regarding a fix, an argument could indeed be added the Html2Image class' __init__(), for instance.