Examples

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

Heads up before you start

Puppeteer has no official Python port. The community port, pyppeteer, is no longer maintained and bundles an old Chromium. These examples still work, but for new projects we recommend Playwright for Python — it covers everything Puppeteer does and is actively maintained.

Install the Python Puppeteer port 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('<html><body><h1>Hello World</h1></body></html>')
    image = await page.screenshot({'encoding': 'base64'})
    print(image)
    await browser.close()

asyncio.run(main())

Find out how to generate a PNG from HTML with Python and Puppeteer.

That was 12 lines of code — plus a browser to run in production

Keeping headless browsers fed, patched and scaled is a job in itself. We built Urlbox so the same screenshot is one API call:

curl "https://api.urlbox.com/v1/YOUR_API_KEY/png?url=https://example.com"

Try the Urlbox screenshot API — free trial, no browser babysitting.