vgalin / html2image Goto Github PK
View Code? Open in Web Editor NEWA 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.
License: MIT License
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.
License: MIT License
Thanks for putting together this package. Could you give me some guidance on how to take a full screenshot of a website ? The screenshot I got from your example is just the visual part of the website.
A Background image as css is not working! any solution?
thanks
When using virtual-time-budget + background color flag & set to transparent. Program get's stuck in a loop..
There are lot's of other instances and bugs that get it stuck in a loop.
As well if you set background style sheet to transparent - same thing.
How to deploy Django project that uses this package to take screenshots on Heroku?
It throws error that it cannot find Google Chrome.
Can you please help if it can be used on Heroku? @vgalin
Please add an option to screenshot at high dpi.
I found that adding --force-device-scale-factor=3
argument creates 3x high-res images.
command = [
f'{self.executable}',
'--headless',
f'--screenshot={os.path.join(output_path, output_file)}',
f'--force-device-scale-factor=3',
f'--window-size={size[0]},{size[1]}',
*self.flags,
f'{input}',
]
I have a very small project on a Windows 10 machine with the following image in the file example.png:
I try to make a screenshot of a simple html project using the following code:
from html2image import Html2Image
hti = Html2Image()
html_str ="""
<!DOCTYPE html>
<html>
<body>
<img src="example.jpg">
</body>
</html>
"""
css_str = """
body {
margin: 0;
background: black;
}
"""
hti.screenshot(html_str=html_str, css_str=css_str, save_as="test.png")
I would expect to see the image in the result. But it only results in the following:
If I copy the html and css strings to a file (and link them) I can see the expected result with my browser. So I guess it has nothing to do with reading access or so.
I am using Django to render an HTML file, then feed that file into html2image.
I am using the latest release: html2image==2.0.1
On Linux, using Chromium 93.0.4577.82 snap (Ubuntu 20.04)
To replicate:
hti = Html2Image()
hti.screenshot(html_str="<h1>Hey there</h1>")
This outputs a screenshot.png
in my current working directory, however this screenshot is just a transparent image file, with no html content in it.
I tracked down the source of this issue to be the fact that on new versions of Ubuntu, Chromium is delivered only as a snap package, even if you use apt to install it (in this case, apt just uses snap under the hood). The problem with snap-installed packages is that they don't have access to the filesystem outside of the user's home directory. This includes the /tmp
folder.
This took me a while to hunt down, and I feel like other less-python-inclined developers may also run into this issue, especially if they don't know a lot about Ubuntu. My fix was to pass the temp_path
parameter, which I was only able to find by going through your source.
This parameter is only alluded to here in your CLI docs:
https://github.com/vgalin/html2image#using-the-cli
Once I found this, it was a simple fix:
hti = Html2Image(temp_path="/home/myuser")
hti.screenshot(html_str="<h1>Hey there</h1>")
And it worked. My humble recommendation would be to add to the docs to explain this parameter, and maybe a blurb about the limitations of Chromium installed via snap.
Thanks for your work on this project. It's really useful.
from html2image import Html2Image
hti = Html2Image()
hti.browser.flags = ['no-sandbox']
hti.output_path = root+'/x/'
hti.screenshot(
html_file=root+'/x/x.html',
save_as='plot_'+str(i)+'.png'
)
Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
Open multiple tabs is only supported when remote debugging is enabled.
Any idea how to fix this?
[Issue to be edited / formatted later]
See : https://stackoverflow.com/questions/71001835/how-to-convert-a-formated-table-in-html-to-image-png-in-colab
This is a Chrome/Chromium related issue, but it would be interesting to see if a workaround exists.
When I pass --quiet
, the tool still spits out lots of info - presumably coming from the browser process?
I expect a tool to have zero output when a "quiet" option is on.
Also, I couldn't find a quiet
option in the Python API.
Is the command line / Python API not equivalent?
Hi...
I am trying to load external fonts from local machine using @font-face but they are not loading. I am stuck with serif.
Please help !!!
My workaround was adding the flag '--crash-dumps-dir=' in the initialization. I guess that only worked because the files and folders here have that level of permissions.
It would be great to have a way to disable these crash logs natively, I don't know if it will work on another server if you don't have permissions.
Hi All,
I need your help.
I install google chrome on server CentOs
but my program notice error
Traceback (most recent call last):
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 2464, in __call__
return self.wsgi_app(environ, start_response)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 2450, in wsgi_app
response = self.handle_exception(e)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 1867, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 272, in error_router
return original_handler(e)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/_compat.py", line 38, in reraise
raise value.with_traceback(tb)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 468, in wrapper
resp = resource(*args, **kwargs)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask/views.py", line 89, in view
return self.dispatch_request(*args, **kwargs)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/flask_restful/__init__.py", line 583, in dispatch_request
resp = meth(*args, **kwargs)
File "/home/python_crawler/backend/api-python/api.py", line 129, in post
hti = Html2Image()
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/html2image/html2image.py", line 163, in __init__
self.chrome_path = chrome_path
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/html2image/html2image.py", line 176, in chrome_path
self._chrome_path = _find_chrome(value)
File "/home/python_crawler/backend/api-python/.venv/lib/python3.7/site-packages/html2image/html2image.py", line 60, in _find_chrome
["google-chrome", "-version"]
File "/usr/local/lib/python3.7/subprocess.py", line 395, in check_output
**kwargs).stdout
File "/usr/local/lib/python3.7/subprocess.py", line 472, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/lib/python3.7/subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python3.7/subprocess.py", line 1522, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'google-chrome': 'google-chrome'
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
hello,how can i set time out for screenshot
Hello,
I would like to ask you about whether can I use the html2image on the Linux Server without Desktop. I can install the Headless Chrome Browswer on the server.
Do I need to pass some arguments or flags during the execution ?
Thank you
Following #54. A way to toggle browsers' output on or off could be useful, especially if the information displayed is not related to errors or bugs that can be fixed.
Example of this output using Chromium/Chrome (taken from issue #28):
[0615/164730.282582:ERROR:file_io.cc(91)] ReadExactly: expected 8, observed 0
[0615/164730.283193:ERROR:xattr.cc(63)] setxattr org.chromium.crashpad.database.initialized on file /var/folders/8_/qh0jnghd1xd_99znh3z6tvgc0000gn/T/: Operation not permitted (1)
[0615/164730.329920:WARNING:headless_browser_main_parts.cc(106)] Cannot create Pref Service with no user data dir.
[0615/164730.396624:WARNING:address_sorter_posix.cc(388)] FromSockAddr failed on netmask
In some cases, the browser just spams the stdout/stderr with errors or warnings that cannot be resolved. However, in other cases, when screenshots fail to be taken, these messages could hold valuable hints and allow the users to solve the issue : for this reason, these messages may not be silenced by default.
Hi, hti.screenshot is working fine for us even if we have concern regarding performance.
We have error messages during screenshots
0615/164730.282582:ERROR:file_io.cc(91)] ReadExactly: expected 8, observed 0
[0615/164730.283193:ERROR:xattr.cc(63)] setxattr org.chromium.crashpad.database.initialized on file /var/folders/8_/qh0jnghd1xd_99znh3z6tvgc0000gn/T/: Operation not permitted (1)
[0615/164730.329920:WARNING:headless_browser_main_parts.cc(106)] Cannot create Pref Service with no user data dir.
[0615/164730.396624:WARNING:address_sorter_posix.cc(388)] FromSockAddr failed on netmask
Does theses errors have an impact on performance. Currently if takes 1,6s to generate a frame, and we would like to generate up to 100 (25img/s for 4s)
Setting save_as
on the screenshot method only accepts a filename, if I put a folder into the string like comments/hbl8g9l.png
it will throw
File "/home/oskar/Documents/GitHub/reddit-video-bot/venv/lib/python3.9/site-packages/html2image/html2image.py", line 479, in screenshot
self.load_str(content=content, as_filename=html_filename)
File "/home/oskar/Documents/GitHub/reddit-video-bot/venv/lib/python3.9/site-packages/html2image/html2image.py", line 138, in load_str
with open(os.path.join(self.temp_path, as_filename), 'wb') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/html2image/comments/hbl8g9l.html'
If I set it as open("comments/hbl8g9l.png")
it throws
File "/home/oskar/Documents/GitHub/reddit-video-bot/venv/lib/python3.9/site-packages/html2image/html2image.py", line 454, in screenshot
save_as = Html2Image._extend_save_as_param(
File "/home/oskar/Documents/GitHub/reddit-video-bot/venv/lib/python3.9/site-packages/html2image/html2image.py", line 278, in _extend_save_as_param
filename, extention = save_as[-1].split('.')
IndexError: list index out of range
html2image.Html2Image().screenshot(url='https://www.google.com', size=(100, 0))
Causes a hang. Should probably raise ValueError
hti = Html2Image(custom_flags=['--disable-remote-debugging', '--no-sandbox'])
try:
hti.screenshot(html_file='index1.html',
save_as='Image-1.jpg',
size=(1200, 1200)
)
hti.screenshot(html_file='index2.html',
save_as='Image-2.jpg',
size=(1200, 1200)
)
except:
print(e)
return ['Image-1.jpg', 'Image-2.jpg']
DockerFile:
`
FROM python:3.7
EXPOSE 9567
WORKDIR /app
COPY . /app
RUN echo "Acquire::Check-Valid-Until "false";\nAcquire::Check-Date "false";" | cat > /etc/apt/apt.conf.d/10no--check-valid-until
RUN sed -i 's|security.debian.org/debian-security|mirrors.ustc.edu.cn/debian-security|g' /etc/apt/sources.list
RUN sed -i 's#http://deb.debian.org#https://mirrors.163.com#g' /etc/apt/sources.list
RUN apt-get clean
RUN apt-get update -y && apt-get install -y icc-profiles-free
RUN apt-get update -y && apt-get install -y chromium
RUN echo 'export CHROMIUM_FLAGS="$CHROMIUM_FLAGS --no-sandbox --disable-gpu"' >> /etc/chromium.d/default-flags
RUN echo " \n =============HTML2IMAGE============= \n Welcome to the html2image CLI container ! \n Type html2image -h for help :)" >> /etc/motd
RUN echo "clear" >> /root/.bashrc
RUN echo "cat /etc/motd" >> /root/.bashrc
RUN pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
RUN pip install -r requirements.txt
`
requirements.txt
`
Pillow
pytest
html2image
`
python script
`
from html2image import Html2Image
hti = Html2Image(custom_flags=['--no-sandbox','--disable-gpu', '--disable-software-rasterizer'])
hti.screenshot(url='http://www.python.org', save_as='python_org.png', size = (1500, 13000))
`
error info:
[1026/042946.265913:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[1026/042946.266046:ERROR:bus.cc(399)] Failed to connect to the bus: Failed to connect to socket /run/dbus/system_bus_socket: No such file or directory
[1026/042946.268083:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable.
[1026/042946.271534:ERROR:gpu_init.cc(521)] Passthrough is not supported, GL is disabled, ANGLE is
[1026/043157.712559:ERROR:headless_shell.cc(434)] Abnormal renderer termination.
I've been using variants for the parameters --browser
and --chrome_path
with no success. The program always does the same thing. It is poorly documented what these values should be. What is --browser
for?
Switching on the -v
verbose param only tells the parameters passed by the user but not the actual values computed by the program.
As a user I'd like to know:
--browser
parameter acceptchromium
a valid substitute to chrome
; if so please document itHello,
Thanks for this lib.
Did you do any test with special characters (é, î, ö...) on windows ?
I note that special character doesn't render correctly when the html is provided as string hti.screenshot(html_str=...). (I guess it's because you write the html_str to temp file before calling the browser headless command. Converting the html_str to bytes
should fix the problem.)
Here is simple test that illustrate test_html2image.zip.
Good job anyway.
Hi !
I have installed chromium-browser
in a Ubuntu Docker container to try some things out.
However, when I execute a python script using your package, the following stack-trace pops out :
Traceback (most recent call last):
File "/examples/load_filestr_example.py", line 19, in <module>
hti = Html2Image()
File "/usr/local/lib/python3.9/site-packages/html2image/html2image.py", line 84, in __init__
self.browser = browser_class(
File "/usr/local/lib/python3.9/site-packages/html2image/browsers/chrome.py", line 113, in __init__
self.executable_path = executable_path
File "/usr/local/lib/python3.9/site-packages/html2image/browsers/chrome.py", line 130, in executable_path
self._executable_path = _find_chrome(value)
File "/usr/local/lib/python3.9/site-packages/html2image/browsers/chrome.py", line 52, in _find_chrome
version_result = subprocess.check_output(
File "/usr/local/lib/python3.9/subprocess.py", line 424, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/local/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/local/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/local/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'google-chrome'
Isn't it supposed to find and use chromium-browser
instead of google-chrome
if this one can't be found ?
In browsers/chrome.py
, a lot of things are happening, and some are failing.
For the Linux (Allow me to interject...) case, wouldn't it be easier to only use which
as you've done here ?
which_result = shutil.which('chromium-browser')
Keep up the good work :)
Hello guys,
I'm trying to resize an image, my monitor is 1920x1080p but I want to crop the image.
For example:
hti.screenshot(html_file = 'index.html', save_as = 'out.jpg', size = (600, 600))
Where starts to crop the image size parameter?
My screenshot is getting cropped badly and only is working if I set my full screen resolution.
Kind regards
I'm trying to generate an image of a widget generated by a thirdparty javascript, the full code is as follows:
from html2image import Html2Image
hti = Html2Image(
custom_flags=['--virtual-time-budget=10000'],
)
html = '''
<script defer src="https://www.livecoinwatch.com/static/lcw-widget.js"></script>
<div class="livecoinwatch-widget-1" lcw-coin="AO" lcw-base="USD" lcw-secondary="ALGO" lcw-period="d" lcw-color-tx="#ffffff" lcw-color-pr="#58c7c5" lcw-color-bg="#1f2434" lcw-border-w="1" ></div>
'''
hti.screenshot(html_str=html, save_as='test.png')
I added the virtual time budget flag to give time for the script to fun and the page to render but the program doesn't seem to slow down and the output image is just blank.
How can i render the image before taking the screenshot?
i´m trying to convert html to png files, but it always return this error:
Traceback (most recent call last):
File "c:\Users...\CDCConverterWatchDog.py", line 8, in
hti.screenshot_loaded_file(file=file)
File "C:\Users...\Python\Python39\lib\site-packages\html2image\html2image.py", line 193, in screenshot_loaded_file
self.browser.screenshot(
AttributeError: 'str' object has no attribute 'screenshot'
PS C:\Users\user>
code:----------------------------------------------------------------------------------
from html2image import Html2Image
hti = Html2Image()
hti.browser = 'chrome'
hti.output_path = "C:/Users/ArntV"
hti.size = (1080,1920)
file = 'C:/CDC_template.html'
hti.screenshot_loaded_file(file=file)
hti.screenshot_loaded_file(file=file,size=(1080,1920))
HTML file contents:----------------------------------------------------------------------------------
<style> .container{ background-image: url("http://www.agcofinance.com.br/portal_dll/campanhas/Comunicados/Imagens_RD_Station/CDC Template/Background_AGCO.png"); background-repeat: no-repeat; background-size: 100% 100%; /* width height */ width:1080px; height:1920px; margin:0px; } </style> <title>teste</title>Confira as nossas condições de *|MES|* de *|ANO|*
PRAZO (MESES) |
TAXA% (periocidade mensal) |
TAXA% (periocidade anual) |
12 |
*|TAXAM12|* |
*|TAXAA12|* |
24 |
*|TAXAM24|* |
*|TAXAA24|* |
36 |
*|TAXAM36|* |
*|TAXAA36|* |
48 |
*|TAXAM48|* |
*|TAXAA48|* |
60 |
*|TAXAM60|* |
*|TAXAA60|* |
Vigência: até *|VIGENCIA|*
Carência: até 18 meses, de acordo com a safra do cliente
Forma de pagamento: mensal, bimestral, trimestral, quadrimestral, semestral ou anual
</div>
<div style="display: flex;padding-left: 10%;padding-top: 2%;padding-right: 10%;width:80%;height:20%; marign:0px;">
<div style="padding-left: 5%;padding-right: 5%;width:70%;height:100%; marign:0px;">
<p style="text-align: left;" ><span lang="PT-BR" style="font-size:18.0pt;font-family:"Franklin Gothic Book",sans-serif;color:#ffffff;mso-ansi-language:PT-BR">
Condições válidas para financiamentos através da linha CDC – Crédito Direto ao Consumidor. Condições válidas apenas para financiamentos com prazo de citados na tabela acima. Condições comerciais para Pessoas Físicas e Pessoas Jurídicas. Os vencimentos das parcelas serão ajustados em função dos finais de semana e/ou feriados; Para condições comerciais diferenciadas, consulte um representante comercial. Condições sujeitas a alterações em caso de flutuações de mercado e sem aviso prévio. Condições de CDC praticadas pelo Banco de Lage Landen Brasil S.A.. Condições sujeitas a aprovação de crédito pelo Banco De Lage Landen S.A.
</span>
</p>
</div>
<div style="width:20%;height:100%; marign:0px;">
<img style="width: 100%;height: 100%;object-fit:scale-down;" src="http://www.agcofinance.com.br/portal_dll/campanhas/Comunicados/Imagens_RD_Station/CDC Template/AGCO_Finance_logo.png">
</div>
</div>
<div style="display: flex;padding-left: 10%;padding-right: 10%;width:80%;height:1%; marign:0px;">
<p style="text-align: center;" ><span lang="PT-BR" style="font-size:15.0pt;font-family:"Franklin Gothic Book",sans-serif;color:#ffffff;mso-ansi-language:PT-BR">
AGCO FINANCE é uma marca registrada e unidade de negócios do Banco De Lage Landen Brasil S.A.
</span>
</p>
</div>
</div>
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.
Struggling with paths on Heroku. I know Heroku uses ephemeral file system, but i think i should be able to save the image file and then open it again to send it back to the front-end. However, when i try to open the saved file (or in the case send_file back to the front end) i get the below error. If tried all sorts of different permutations of the file + path string, but cannot seem to find the file. Anyone know how i can get the save image file?
Ps. It works fine locally, so I guess it must be something with the paths on Heroku.
app[web.1]: File "/app/.heroku/python/lib/python3.9/site-packages/werkzeug/utils.py", line 697, in send_file
app[web.1]: stat = os.stat(path)
app[web.1]: FileNotFoundError: [Errno 2] No such file or directory: '/app/static/twitter_image_unpo.png'
from flask import send_file
from html2image import Html2Image
def create_dynamic_image(url, username):
hti = Html2Image()
filename = 'twitter_image_' + username + '.png'
hti.output_path = 'static'
file = hti.screenshot(url=url, save_as=filename, size=(1000, 650))
return send_file(file[0], mimetype='image/gif')
Hello, there is some way that html2image gets the image in memory or base64?
That is to say that instead of saving the image in a file I generate a string with the image in base64 code
from html2image import Html2Image
hti = Html2Image()
imgcode64 = hti.screenshot(html_str=render_template('profile/_riesgoimg.html'))
print(f'Se convirtio a imagen:{imgcode64}')
In this case I would expect the imgcode64 variable to have something like the following:
imgcode64 = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAALQAAAAoCAYAAABXadAKAAAALnRFWHRDcmVhdGlvbiBUaW1lAHZpZS4gMTkgZmViLiErkJggg=="
Regards
Question. Can I send a screenshot to Pillow directly in memory without saving it to disk? Thanks
I tried from command line and from Python as well, I always get a blank PNG file.
Example:
$ hti -v -H kaya.al.html -S op.png -s 1024,768 -o ~/tmp/hti
args = Namespace(browser=None, chrome_path=None, css=[], html=['kaya.al.html'], other=[], output_path='<home>/tmp/hti', quiet=False, save_as=['op.png'], size=[(1024, 768)], temp_path=None, url=[], verbose=True)
[0217/100850.406393:WARNING:bluez_dbus_manager.cc(248)] Floss manager not present, cannot set Floss enable/disable.
[0217/100850.417779:ERROR:sandbox_linux.cc(377)] InitializeSandbox() called with multiple threads in process gpu-process.
[0217/100850.565404:INFO:headless_shell.cc(659)] Written to file <home>/tmp/hti/op.png.
Created 1 file(s):
<home>/tmp/hti/op.png
Hi!
I m trying to save a local HTML file as an image but the figures inside are missing. Would you please take a look at this?
>>> hti.screenshot(html_file='index.html', save_as='out.png')
and the file hierarchy looks like this, figures the index.html is referring are stored under images/:
drwxr-sr-x. 3 kaiwkh ivc 4096 May 25 11:10 ..
-rw-------. 1 kaiwkh ivc 60152 May 25 11:15 out.png
drwxr-sr-x. 2 kaiwkh ivc 49152 May 25 11:23 images
-rw-r--r--. 1 kaiwkh ivc 317090 May 25 11:23 index.html
drwxr-sr-x. 3 kaiwkh ivc 69 May 25 11:35 .
The output image doesn't contain those images:
Thanks!
Hi Vgalin, not entirely sure If I am doing something wrong or if css isn't loaded correctly.
I have "messages.html" file (actually like 100 of them, I have exported one chat from Telegram, and was trying to automate making screenshots). "style.css" is located at path "css\style.css" from the messages file.
So I'm running it as:
hti.screenshot( html_file='messages.html', css_file='css\style.css', save_as='aaatest.jpg' )
The html2image does work - it produces image file, however - instead of looking more&less as the original html+css - it's just black text on grey background.
Attaching html2image result and manually made screenshot.
Would be very grateful if you could point me to my issue.
Thank you in advanc
e
I want save image to other path eg: "/var/www/images/out.jpg"
I receive notice: "ValueError: the output_file parameter should be a filename and not a path."
My code: Html2Image().screenshot(html_str=schedule, save_as='red_page.png')
Error: FileNotFoundHError: Could not find a Chrome executable on this machine, please specify it yourself.
This is the simplest line that takes html and saves it as an image. When launched on my computer, everything works perfectly. When running on Heroku, I get this error on the line with the html2Image shown above. It looks like I figured out what the problem is, there is no Chrome on the server. How can I change the path settings for the required components for html2image to work and which components do I need? (I tried to put it on heroku Chrome, the server does not allow to do it)
Hi
I am trying to run a script where I'm creating a png image with html but getting the following error:
mkdir: cannot create directory ‘/run/user/0’: Permission denied [1021/052947.190152:WARNING:bluez_dbus_manager.cc(247)] Floss manager not present, cannot set Floss enable/disable. [1021/052947.261096:WARNING:sandbox_linux.cc(380)] InitializeSandbox() called with multiple threads in process gpu-process. [1021/052947.504123:ERROR:headless_shell.cc(634)] Writing to file /home/ubuntu/.../screenshot.png was unsuccessful, could not open file: FILE_ERROR_ACCESS_DENIED
I'm running this through the root user
Following is the code I'm using:
hti = Html2Image( custom_flags=["--no-sandbox","--default-background-color=1"] ) hti.screenshot(html_str=response_html, save_as=image_name)
I also tried "--disable-gpu", in the custom flags but still getting the same error
What should be the correct solution for this.
Thanks
The --dump-dom
flag would be very beneficial for processing as sometimes you need some other return values from chromium (e.g. pixel perfect coordinates of the generated images, something which javascript can do), by simply outputting them in an invisible element of the html page.
This could be done with that flag, however you would need to redirect stdout
of the executed command to a file, which you can't do currently without tinkering with the module yourself.
When running as root, this error pops up and no images were saved.
[0517/064559.081037:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
Hi,
I have a large html visualization to plot to an image. any idea how to get that to work?
from html2image import Html2Image
hti = Html2Image()
#hti.browser.flags.extend(['--disable-software-rasterizer', '--disable-gpu']) <-- does not work
hti.screenshot(
html_file='model_plot.html',
save_as='blue_page.png',
size=(4000, 40000) # may need to increase to 50000 or 60000 ...
)
python convert.py
objc[12926]: Class WebSwapCGLLayer is implemented in both /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/Frameworks/libANGLE-shared.dylib (0x7ffb44709948) and /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/106.0.5249.119/Libraries/libGLESv2.dylib (0x10bc7f668). One of the two will be used. Which one is undefined.
[1021/072630.558755:ERROR:command_buffer_proxy_impl.cc(128)] ContextResult::kTransientFailure: Failed to send GpuControl.CreateCommandBuffer.
[1021/072634.255316:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072634.260627:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072634.260987:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072634.463683:WARNING:crash_report_exception_handler.cc(235)] UniversalExceptionRaise: (os/kern) failure (5)
[1021/072634.479950:ERROR:gpu_process_host.cc(974)] GPU process exited unexpectedly: exit_code=5
[1021/072634.480027:WARNING:gpu_process_host.cc(1276)] The GPU process has crashed 1 time(s)
objc[12942]: Class WebSwapCGLLayer is implemented in both /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/Frameworks/libANGLE-shared.dylib (0x7ffb44709948) and /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/106.0.5249.119/Libraries/libGLESv2.dylib (0x10706b668). One of the two will be used. Which one is undefined.
[1021/072634.775134:WARNING:gpu_process_host.cc(997)] Reinitialized the GPU process after a crash. The reported initialization time was 225 ms
[1021/072634.984031:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072634.986238:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072634.986510:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072635.313578:WARNING:crash_report_exception_handler.cc(235)] UniversalExceptionRaise: (os/kern) failure (5)
[1021/072635.332282:ERROR:gpu_process_host.cc(974)] GPU process exited unexpectedly: exit_code=5
[1021/072635.332373:WARNING:gpu_process_host.cc(1276)] The GPU process has crashed 2 time(s)
objc[12944]: Class WebSwapCGLLayer is implemented in both /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/Frameworks/libANGLE-shared.dylib (0x7ffb44709948) and /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/106.0.5249.119/Libraries/libGLESv2.dylib (0x10b811668). One of the two will be used. Which one is undefined.
[1021/072635.624863:WARNING:gpu_process_host.cc(997)] Reinitialized the GPU process after a crash. The reported initialization time was 229 ms
[1021/072635.803154:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072635.805006:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072635.805230:ERROR:tile_manager.cc(830)] WARNING: tile memory limits exceeded, some content may not draw
[1021/072635.966006:WARNING:crash_report_exception_handler.cc(235)] UniversalExceptionRaise: (os/kern) failure (5)
[1021/072635.980111:ERROR:gpu_process_host.cc(974)] GPU process exited unexpectedly: exit_code=5
[1021/072635.980179:WARNING:gpu_process_host.cc(1276)] The GPU process has crashed 3 time(s)
objc[12946]: Class WebSwapCGLLayer is implemented in both /System/Library/Frameworks/WebKit.framework/Versions/A/Frameworks/WebCore.framework/Versions/A/Frameworks/libANGLE-shared.dylib (0x7ffb44709948) and /Applications/Google Chrome.app/Contents/Frameworks/Google Chrome Framework.framework/Versions/106.0.5249.119/Libraries/libGLESv2.dylib (0x120802668). One of the two will be used. Which one is undefined.
[1021/072636.283871:WARNING:gpu_process_host.cc(997)] Reinitialized the GPU process after a crash. The reported initialization time was 243 ms
[1021/072636.284229:ERROR:headless_shell.cc(575)] Capture screenshot failed
I have an Ubuntu for development and the module works as well with this code;
def png_invoice(request, order_token):
order = Order.objects.get(token=order_token)
from html2image import Html2Image
hti = Html2Image(
custom_flags=['--no-sandbox'])
url = request.build_absolute_uri().replace('image/', '') # it makes my target url
filename = f'{order.id}.png'
hti.output_path = ''.join((settings.MEDIA_ROOT , 'invoice/image/'))
hti.chrome_path = '/usr/bin/google-chrome/'
hti.screenshot(url=url, save_as=filename)
image = open(f"media/invoice/image/{filename}", 'rb')
response = FileResponse(image)
return response
This code works correctly on my local machine with ubuntu os but it does not work on my ubuntu server in production.
When I try to open the image in the last three lines on my server, it says there is no such file. It means html2image has no errors but it does not save the image on server.
I also check the permission on my server to have read and write permission.
Hi!
When I run the screenshot in docker, I get the following error:
[1128/031750.348471:ERROR:zygote_host_impl_linux.cc(90)] Running as root without --no-sandbox is not supported. See https://crbug.com/638180.
This may be because you did not specify --no-sandbox
as a launch argument for chrome.
How about adding arguments, or allowing users to choose to add arguments?
Thanks.
Hello I am running the example code and got this error
Traceback (most recent call last):
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 31, in <module>
start(fakepyfile,mainpyfile)
File "/data/user/0/ru.iiec.pydroid3/files/accomp_files/iiec_run/iiec_run.py", line 30, in start
exec(open(mainpyfile).read(), __main__.__dict__)
File "<string>", line 2, in <module>
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/html2image/html2image.py", line 154, in __init__
self.temp_path = temp_path
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/site-packages/html2image/html2image.py", line 191, in temp_path
os.makedirs(temp_dir, exist_ok=True)
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/os.py", line 213, in makedirs
makedirs(head, exist_ok=exist_ok)
File "/data/user/0/ru.iiec.pydroid3/files/aarch64-linux-android/lib/python3.8/os.py", line 223, in makedirs
mkdir(name, mode)
OSError: [Errno 30] Read-only file system: '/tmp'
[Program finished]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.