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

Image output_path confusion on Heroku #67

Closed kingkong-cmd closed 2 years ago

kingkong-cmd commented 2 years ago

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')
vgalin commented 2 years ago

Hello, could you take a look at #39, particularly this comment (and maybe the one following it), and tell me if it solves your problem?

kingkong-cmd commented 2 years ago

Aha, I was sure it was a path problem, but seems you are right. The img was never created in the first place.

However, after setting the "no-sandbox" flag I am now getting the "remote debugging" error, which I see you have referenced here.

[0528/202630.387206:ERROR:headless_shell.cc(209)] Capture screenshot is disabled when remote debugging is enabled.

I switched from the heroku chrome buildpack to the aurelmegn chrome buildpack (https://github.com/aurelmegn/heroku-buildpack-google-chrome.git), and set the below flags, and now it is working.

hti = Html2Image(custom_flags=['--disable-remote-debugging', '--no-sandbox'])

A few hoops to jump through though. Thanks for the help and for the awesome package!