Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Not Getting Image #39

Closed
1Danish-00 opened this issue Aug 1, 2021 · 12 comments
Closed

Not Getting Image #39

1Danish-00 opened this issue Aug 1, 2021 · 12 comments

Comments

@1Danish-00
Copy link

1Danish-00 commented Aug 1, 2021

Python 3.9.5
html2image 2.0.1

I'm using Chrome heroku buildpack

from html2image import Html2Image
hti = Html2Image()
# hti.chrome_path = "/opt/google/chrome/chrome"
hti.chrome_path = "/app/.apt/usr/bin/google-chrome"
html = """<h1> An interesting title </h1> This page will be red"""
css = "body {background: red;}"

file = hti.screenshot(html_str=html, css_str=css, save_as='red_page.png')

Its Not saving Image + No error logs

@vgalin Thanks

@vgalin
Copy link
Owner

vgalin commented Aug 1, 2021

Hello, thanks for opening an issue. This problem is not directly related to this package but the fact that there is not any error message is indeed troublesome.

I checked myself using heroku, and it seems that adding a buildpack is not enough to "install" it, you will have to deploy your app for the buildpack to be applied.

I'm not really a heroku user, so it may not be the right way to do it, but you can check if chrome is correctly installed by "connecting" via bash to you app using the Heroky CLI:

heroku run bash -a the-name-of-your-app

Then you can try running chrome:

/app/.apt/usr/bin/google-chrome

If you get a "file not found", it may be because the buildpack has not been applied. To apply it, you need to deploy your app (you can find different ways to do so via your heroku dashboard > your app > deploy tab).

After deploying the app, the Chrome executable was here I was able to take a screenshot.

I hope this can help you. In all cases do not close the issue, I'll take a look at why there isn't any error message.

@1Danish-00
Copy link
Author

Hello, thanks for opening an issue. This problem is not directly related to this package but the fact that there is not any error message is indeed troublesome.

I checked myself using heroku, and it seems that adding a buildpack is not enough to "install" it, you will have to deploy your app for the buildpack to be applied.

I'm not really a heroku user, so it may not be the right way to do it, but you can check if chrome is correctly installed by "connecting" via bash to you app using the Heroky CLI:

heroku run bash -a the-name-of-your-app

Then you can try running chrome:

/app/.apt/usr/bin/google-chrome

If you get a "file not found", it may be because the buildpack has not been applied. To apply it, you need to deploy your app (you can find different ways to do so via your heroku dashboard > your app > deploy tab).

After deploying the app, the Chrome executable was here I was able to take a screenshot.

I hope this can help you. In all cases do not close the issue, I'll take a look at why there isn't any error message.

yes ik, I deployed correctly and path is also there, but same issue image not saving.

@1Danish-00
Copy link
Author

1Danish-00 commented Aug 2, 2021

Hello, thanks for opening an issue. This problem is not directly related to this package but the fact that there is not any error message is indeed troublesome.
I checked myself using heroku, and it seems that adding a buildpack is not enough to "install" it, you will have to deploy your app for the buildpack to be applied.
I'm not really a heroku user, so it may not be the right way to do it, but you can check if chrome is correctly installed by "connecting" via bash to you app using the Heroky CLI:

heroku run bash -a the-name-of-your-app

Then you can try running chrome:

/app/.apt/usr/bin/google-chrome

If you get a "file not found", it may be because the buildpack has not been applied. To apply it, you need to deploy your app (you can find different ways to do so via your heroku dashboard > your app > deploy tab).
After deploying the app, the Chrome executable was here I was able to take a screenshot.
I hope this can help you. In all cases do not close the issue, I'll take a look at why there isn't any error message.

yes ik, I deployed correctly and path is also there, but same issue image not saving.

Now I changed heroku stack to container (root docker)

used custom flag and path

from html2image import Html2Image
hti = Html2Image(custom_flags=['--no-sandbox'])
hti.browser_executable = "/usr/bin/google-chrome"
html = """<h1> An interesting title </h1> This page will be red"""
css = "body {background: red;}"

file = hti.screenshot(html_str=html, css_str=css, save_as='red_page.png')

It worked , 👍👍

btw how can we get a good quality image?

@aurelmegn
Copy link

Capture screenshot is disabled when remote debugging is enabled

Hey,
in case someone is having this error issue with heroku, the chrome build pack enable remote debugging by default, and you need to remove the flag --remote-debugging-port=9222in the build pack before using it. That is what I did here https://github.com/aurelmegn/heroku-buildpack-google-chrome . Feel free to use it or fork it.

@Leventi
Copy link

Leventi commented Jul 27, 2022

Hi @vgalin

I am using docker and the browser is not installed on the system.
In the script settings

hti = Html2Image(custom_flags=['--disable-remote-debugging', '--no-sandbox'], size=(1240, 3508), output_path=tmp_dir.name)
hti.browser_executable = "/usr/bin/alpine-chrome"

The browser is installed as a service from docker-compose
I use - https://github.com/Zenika/alpine-chrome

Service in docker-compose

chrome:
    image: zenika/alpine-chrome:latest
    container_name: alpine-chrome
    restart: always
    command: [chromium-browser, "--headless", "--disable-gpu", "--no-sandbox", "--remote-debugging-address=0.0.0.0", "--remote-debugging-port=9222"]
    ports:
      - "9222:9222"
    networks:
        main:

When executed, the browser works correctly and displays the content of the page
docker exec -it alpine-chrome wget -q -O - http://foo.bar

But when I run my script I get an error
FileNotFoundError: Could not find a Chrome executable on this machine, please specify it yourself.

What I need write in hti.browser_executable ?

Or maybe I'm using the wrong image for the browser?

@Leventi
Copy link

Leventi commented Jul 27, 2022

Hi again @vgalin :)

Decided just add this line to my app dockerfile
RUN apt-get update -y && apt-get install -y chromium

Error about "Could not find a Chrome" disappeared but now I get other errors

[0727/121639.333749:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121639.333998:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121639.531901:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable.
[0727/121639.727884:ERROR:angle_platform_impl.cc(43)] Display.cpp:1006 (initialize): ANGLE Display::initialize error 12289: Could not open the default X display.
[0727/121639.728244:ERROR:gl_surface_egl.cc(822)] EGL Driver message (Critical) eglInitialize: Could not open the default X display.
[0727/121639.728477:ERROR:gl_surface_egl.cc(1352)] eglInitialize Default failed with error EGL_NOT_INITIALIZED
[0727/121639.747416:ERROR:gl_ozone_egl.cc(21)] GLSurfaceEGL::InitializeOneOff failed.
[0727/121639.764793:ERROR:viz_main_impl.cc(186)] Exiting GPU process due to errors during initialization
[0727/121639.845294:ERROR:angle_platform_impl.cc(43)] Display.cpp:1006 (initialize): ANGLE Display::initialize error 12289: Could not open the default X display.
[0727/121639.845509:ERROR:gl_surface_egl.cc(822)] EGL Driver message (Critical) eglInitialize: Could not open the default X display.
[0727/121639.845645:ERROR:gl_surface_egl.cc(1352)] eglInitialize Default failed with error EGL_NOT_INITIALIZED
[0727/121639.845760:ERROR:gl_ozone_egl.cc(21)] GLSurfaceEGL::InitializeOneOff failed.
[0727/121639.848393:ERROR:viz_main_impl.cc(186)] Exiting GPU process due to errors during initialization
[0727/121639.890302:ERROR:angle_platform_impl.cc(43)] Display.cpp:1006 (initialize): ANGLE Display::initialize error 12289: Could not open the default X display.
[0727/121639.890452:ERROR:gl_surface_egl.cc(822)] EGL Driver message (Critical) eglInitialize: Could not open the default X display.
[0727/121639.890509:ERROR:gl_surface_egl.cc(1352)] eglInitialize Default failed with error EGL_NOT_INITIALIZED
[0727/121639.890575:ERROR:gl_ozone_egl.cc(21)] GLSurfaceEGL::InitializeOneOff failed.
[0727/121639.891874:ERROR:viz_main_impl.cc(186)] Exiting GPU process due to errors during initialization
[0727/121639.897917:ERROR:gpu_init.cc(486)] Passthrough is not supported, GL is disabled, ANGLE is
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
libpng warning: iCCP: known incorrect sRGB profile
[0727/121645.291664:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121645.291850:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121645.291947:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121645.292451:WARNING:property.cc(144)] DaemonVersion: GetAndBlock: failed.
[0727/121645.293177:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121645.293279:ERROR:bus.cc(398)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[0727/121645.905927:WARNING:media_session.cc(939)] RED codec red is missing an associated payload type.
[0727/121646.537109:INFO:headless_shell.cc(660)] Written to file /app/temp/tmp6v_2kqk4/screen.png.

My Dockerfile

FROM python:3.10.0-slim-bullseye
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
ENV PY3VE_IGNORE_UPDATER=1

WORKDIR /app

COPY requirements.txt requirements.txt
RUN pip3 install --no-cache-dir -r requirements.txt
RUN apt-get update -y && apt-get install -y chromium

COPY ./src .

@vgalin
Copy link
Owner

vgalin commented Jul 27, 2022

Hello @Leventi
The first error message was directly outputted by Html2Image:
If Chrome/Chromium is installed through a "stantard" way, Html2Image should be able to find it (and this is indeed what happened). You can also specify a path to a Chrome/Chromium executable using hti.browser_executable.

Concerning the new errors, these are outputted directly by Chromium. They are harmless most of the time and it can be difficult or impossible to get rid of them, so if your screenshot is generated properly, you could ignore them.

@Leventi
Copy link

Leventi commented Jul 27, 2022

Thx @vgalin
Part of warnings and errors hide by flags
custom_flags=['--no-sandbox', '--headless', '--disable-gpu', '--disable-software-rasterizer', '--disable-dev-shm-usage']

@megaprimatus
Copy link

megaprimatus commented Oct 19, 2023

Similar issue with deploying to Render.

As it was said from render: Chrome is not installed by default on Render Native Runtimes
so we must install it executing this code within Build Command:

#!/usr/bin/env bash
# exit on error
set -o errexit

STORAGE_DIR=/opt/render/project/.render

if [[ ! -d $STORAGE_DIR/chrome ]]; then
  echo "...Downloading Chrome"
  mkdir -p $STORAGE_DIR/chrome
  cd $STORAGE_DIR/chrome
  wget -P ./ https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
  dpkg -x ./google-chrome-stable_current_amd64.deb $STORAGE_DIR/chrome
  rm ./google-chrome-stable_current_amd64.deb
  cd $HOME/project/src # Make sure we return to where we were
else
  echo "...Using Chrome from cache"
fi

Start Command should have the following:
export PATH="${PATH}:/opt/render/project/.render/chrome/opt/google/chrome"

After this I did include hti.browser_executable to my python code and still was getting FileNotFoundError
FileNotFoundError: Could not find a Chrome executable on this machine, please specify it yourself.

hti = Html2Image(custom_flags=['--disable-remote-debugging', '--no-sandbox'])
hti.browser_executable = '/opt/render/project/.render/chrome/opt/google/chrome/'
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='plot.png')

@vgalin
Copy link
Owner

vgalin commented Oct 19, 2023

@ser-zhm Both are undocumented features, but you can force the browser path using two methods.

The first one is creating an instance of Html2Image and manually setting the value of the browser._executable attribute:

>>> from html2image import Html2Image
>>> hti = Html2Image()
>>> hti.browser._executable # check the current executable filepath
'C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe'
>>> hti.browser._executable = ... # set a different executable filepath

The second one relies on environment variables but is quite unreliable for the moment. To try it:

  1. Set the HTML2IMAGE_TOGGLE_ENV_VAR_LOOKUP environment variable to something (it just has to exist).
  2. Set one of the following variables to your browser path:
  • HTML2IMAGE_CHROME_BIN
  • HTML2IMAGE_CHROME_EXE
  • CHROME_BIN
  • CHROME_EXE

If done properly, Html2Image should display some kind of message, like so:

>>> from html2image import Html2Image
>>> Html2Image()
Found a potential chrome executable in the "your filepath" environment variable:
"your filepath"

(The message leaves a lot to be desired.)

@megaprimatus
Copy link

megaprimatus commented Oct 19, 2023

Thanks for reply.
Is there a difference between hti.browser._executable
and this
hti.browser_executable
I’m using without dot to point the location of Chrome binary

@vgalin
Copy link
Owner

vgalin commented Oct 19, 2023

As-is, the browser executable cannot be changed afterwards using hti.browser_executable, it is a parameter of the Html2Image class' __init__ function, but it is not an attribute of this class.

Concerning hti.browser._executable : In our scenario, hti.browser is an instance of the ChromeHeadless class. browser._executable is an attribute holding the filepath to the browser executable.

To summarize : browser_executable is a parameter of the __init__ function of the Html2Image class, but not an attribute, while browser._executable is the raw filepath that is used to call the browser executable


This behavior is probably too confusing to be left as-is, doing everything from hti.browser_executable would be more convenient I guess, I'll open a ticket about this and see what can be done to make it more practical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants