Forums

html2image library

Hi

Does pythonanywhere support html2image library because i use this library to save a html file as image in a specified folder but the image not getting saved.

What is the code that you're using? And does it generate any errors when you run it?

[edited by admin]

directory_path = '/home/dfdlpreprod/DSiRE/DsireProj/DsireApp/templates/'
template_filename=aadhar_template1.html
template_filename = os.path.join(directory_path,template_filename)
template = get_template(template_filename)
html = template.render(context)
save_folder = os.path.join(settings.STATIC_ROOT, 'Aadhaarimg')
screenshot_filename = 'aadhar_template_1.png'
hti = Html2Image()
hti.output_path = save_folder
try:
    hti.screenshot(
        html_str=html,
        save_as=screenshot_filename)
    print("Screenshot saved successfully.")
except Exception as e:
    logging.error(f"An error occurred: {str(e)}")

I dont get any error i get Screenshot saved successfully. in my server log but i dont get the image.

What is the screen that you expect to screenshot? PythonAnywhere servers have no screens.

i have a html file and need to screenshot it as image

How are you running that code -- is it in a console, or inside your website's code, or some other way?

Inside a function i have this code so when the function is called this code will be executed

But where is the function executed? In a web app, task, or console?

in my web app

Are there any errors in the error log or the server log at around the time you're seeing the "Screenshot saved successfully" message?

I dont get any error i get the print statement "Screenshot saved successfully" in my server log

Maybe add more logging, for example what is the value of the save_folder -- maybe it is being saved but in a different location? Also, you use a hardcoded screenshot_filename which is not variable, so maybe the screenshots are being overwritten?

No checked it the image is not getting saved anywhere.

I tried a similar code to yours -- it looks like you need to add --no-sandbox flag to the browser settings. Add this

hti.browser.flags = ["--no-sandbox"]

to the section where you set up hti, reload your web app and see if that works. Also -- hti.screenshot will not raise when the flag is missing, it will only print error to the stdout, so that's why you keep seeing "Success" messages in your log.

It worked.. Thanks

Glad to hear that!