Examples

Screenshot of a particular size with Python and Puppeteer

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

Take a screenshot of the resized viewport and save to file

import asyncio
from pyppeteer import launch

async def main():
    browser = await launch()
    page = await browser.newPage()
    await page.setViewport({'width': 480, 'height': 240})
    await page.goto('https://www.htmltoimage.com/demo')
    await page.screenshot({'path': 'size.png'})
    await browser.close()

asyncio.run(main())

Find out how to change the size of the viewport 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.