How to convert HTML to a base64 image with Puppeteer and Python

Install Python Puppeteer Implementation and Chromium

pip install pyppeteer
pyppeteer-install

Get the base64 encoded image of some HTML and print to screen

import asyncio
from pyppeteer import launch

async def main():
    browser = await launch()
    page = await browser.newPage()

    await page.setContent("<h1>Hello World</h1>")
    image = await page.screenshot({"encoding": "base64"})
    print(image)

    await browser.close()

asyncio.run(main())

image

We created Urlbox to make converting HTML to images easy

Learn more about our website screenshot API.