How to convert HTML to a base64 image with Playwright and Python
The most popular way to do it is with the Grover gem. Grover is a wrapper around Playwright that makes it easy to generate images from HTML.
Install Playwright and Playwright client for Python
pip install pytest-playwright
playwright install
Get the base64 encoded image of some HTML
import asyncio
import base64
from playwright.async_api import async_playwright
async def main():
async with async_playwright() as p:
browser = await p.chromium.launch()
page = await browser.new_page()
await page.set_content('<html><body><h1>Hello World</h1></body></html>')
image = await page.screenshot()
print(base64.b64encode(image))
await browser.close()
asyncio.run(main())
We created Urlbox to make converting HTML to images easy
Learn more about our website screenshot API.