How To Convert Html File/URL/String Into Image Using Python

   I worked on converting HTML formate to PNG Image using python, its take 1 week to complete, First, I tried with Imgkit Package then I tried with Html2image Package, Later I tried with Pypperteer Package,

When I used Imgkit package, I faced on one issue what it means, in HTML file we have any embedded code or more loading javascript code, its take more than 5 sec so when I used imgkit it is not taking those things on the image.

when I used Html2image Package, I faced the same issue

Finally, I solved that loading issue using Pypperteer Package (https://github.com/pyppeteer/pyppeteer), In Pypperteer Package we have one option, by using that option we can stop converting into the image until load the complete HTML file, And here we have one more great option to pass HTML string to get Document Elements like OffsetHight, etc.


import asyncio
from pyppeteer import launch

def converting_html_image(input_url) -> str:
    file_name = input_url.split('/').pop().split('.html')[0] + '.png'
    image_output_filename = 'static\\' + file_name
    OUT_FILE = os.path.join(os.getcwd(), image_output_filename)
    

    async def main_fun() -> None:
        browser = await launch(handleSIGINT=False, handleSIGTERM=False, handleSIGHUP=False)
        page = await browser.newPage()
        await page.goto(input_url, {'waitUntil': 'networkidle2'})
        time.sleep(15)
        dimensions = await page.evaluate('''() => {
                    return { height: document.documentElement.offsetHeight}}''')
        await page.screenshot({'path': OUT_FILE, 'clip': {'x': 0, 'y': 0, 'width': 360, 'height':                                        dimensions['height']}})
        await browser.close()

    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    res = loop.run_until_complete(main_fun())

    file_path = os.path.join(os.getcwd(), image_output_filename)
    return file_path

Comments

Post a Comment

Popular posts from this blog

ERR: error parsing query: found influx, expected SELECT, DELETE, SHOW, CREATE, DROP, EXPLAIN, GRANT, REVOKE, ALTER, SET, KILL at line

How to get Full URL or get absolute url In Django