Skip to main content

PyppeSnap: give it a URL, a headless Chromium renders the page, and you get a PNG (str) or a PDF (bytes) back as base64

Test PyPI version Python versions License: MIT

PyppeSnap takes a snapshot of a web page and hands it to you as base64 text: a PNG of what fits in the browser window, or a PDF of the whole page. It opens the page in a headless Chromium (no window) through Pyppeteer, so JavaScript-heavy pages render like they do in a real browser. One function call, nothing written to disk: handy when the snapshot goes into JSON, a database, an email or an <img src="data:…">.

Installation

pip install pyppesnap

The package is called PyppeSnap on PyPI and is imported as pyppesnap. It works on Python 3.7–3.14.

The first snapshot downloads Chromium. Pyppeteer fetches its own Chromium build (about 100–200 MB) into your user folder the first time, so that call takes a while and needs internet access. Later calls take about a second.

On a minimal Linux image (for example python:3.12-slim in Docker) Chromium also needs some system libraries. On Debian/Ubuntu:

apt-get install -y libnss3 libatk1.0-0 libatk-bridge2.0-0 libcups2 libdrm2 libxkbcommon0 \
  libxcomposite1 libxdamage1 libxfixes3 libxrandr2 libgbm1 libasound2 libpango-1.0-0 libcairo2

On Python 3.7, pip installs an older Pyppeteer (1.0.2) whose Chromium needs five more:

apt-get install -y libx11-xcb1 libxcursor1 libxtst6 libxss1 libgtk-3-0

If they are missing, the snapshot fails with Browser closed unexpectedly and a hint pointing here.

Quick start

from pyppesnap import get_image_snapshot_by_url, get_pdf_snapshot_by_url

png = get_image_snapshot_by_url('https://example.com')  # PNG as base64 str
pdf = get_pdf_snapshot_by_url('https://example.com')    # PDF as base64 bytes

print(png[:20])  # iVBORw0KGgoAAAANSUhE
print(pdf[:20])  # b'JVBERi0xLjQKJdPr6eEK'

Here is what the two functions return for the same page (real output, decoded):

The same page captured twice: get_image_snapshot_by_url returns a PNG of the 1600 x 900 browser window, get_pdf_snapshot_by_url returns a PDF of the whole page on a single sheet

API

Both functions take the same arguments: (url, width=1600, height=900, is_mobile=False).

Function Captures Returns
get_image_snapshot_by_url what fits in the width × height browser window PNG as base64 str
get_pdf_snapshot_by_url the whole page, laid out width pixels wide, on a single sheet PDF as base64 bytes

When a snapshot fails, both print the reason and return None.

  • url needs its scheme: https://example.com, not example.com.
  • width and height set the size of the browser window in CSS pixels.
  • is_mobile=True makes Chromium honour the page's mobile viewport tag. Pair it with a phone-sized window, for example width=390, height=844.
  • Each call starts its own Chromium and closes it again, even when something goes wrong.
  • A page gets 30 seconds to load, and a whole snapshot 120 seconds (the first-run Chromium download does not count). To change them, set pyppesnap.snapshot.NAVIGATION_TIMEOUT and pyppesnap.snapshot.SNAPSHOT_TIMEOUT (in seconds).
  • The functions are synchronous and also work where an event loop is already running, such as a Jupyter notebook. In async code, run them in a thread so they don't block your loop: png = await asyncio.to_thread(get_image_snapshot_by_url, url).

Save to a file or embed in HTML

import base64
from pyppesnap import get_image_snapshot_by_url, get_pdf_snapshot_by_url

with open('page.png', 'wb') as f:
    f.write(base64.b64decode(get_image_snapshot_by_url('https://example.com')))
with open('page.pdf', 'wb') as f:
    f.write(base64.b64decode(get_pdf_snapshot_by_url('https://example.com')))

# Or show the PNG straight in a web page:
html = '<img src="data:image/png;base64,{}">'.format(get_image_snapshot_by_url('https://example.com'))

Command line

Terminal: pyppesnap --url https://example.com prints the parameters and the PNG as base64; piping the PDF version through sed and base64 -d gives a valid one-page PDF

pyppesnap --url https://example.com              # PNG (the default, same as --type img)
pyppesnap --url https://example.com --type pdf   # PDF

Both types print plain base64 text on line 5, between the dashed lines, so you can save the file from the shell:

pyppesnap --url https://example.com --type pdf | sed -n 5p | base64 -d > example.pdf

The exit status is 0 when it worked, 1 when the snapshot failed (the reason is printed), and 2 when --url is missing or --type is not img or pdf.

Use it in a web app

example/ is a small Flask app: type a URL, press a button, and the server returns the snapshot, which the page shows as an image or an inline PDF.

pip install pyppesnap flask
cd example
python main.py   # then open http://127.0.0.1:9898

Don't put it on the internet as it is: it opens any URL a visitor types, including addresses inside your own network.

Development

pip install -e ".[test]"
pytest tests      # most tests use a fake browser; a few take real snapshots of example.com,
                  # so they need internet access and Chromium
tox -e py         # what CI runs: check-manifest + tests

The README images are made from the HTML files in docs/images/src/. take_demo_snapshots.py takes the real snapshots with PyppeSnap; each HTML file says how to render it.

License

MIT

Metadata

Release files for PyppeSnap 1.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for PyppeSnap 1.1.0
File Size Uploaded Provenance Actions
pyppesnap-1.1.0.tar.gz 15.5 kB Without provenance Details for pyppesnap-1.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for PyppeSnap 1.1.0
File Interpreter ABI Platform Actions
pyppesnap-1.1.0-py3-none-any.whl Python 3 none any Details for pyppesnap-1.1.0-py3-none-any.whl

Total release size: 25.0 kB

Release files / pyppesnap-1.1.0.tar.gz

Download URL pyppesnap-1.1.0.tar.gz
Size 15.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ca37035027f10f44f4e10d1de4e103b7baea7a595d42f081ddcde01cd6115816
BLAKE2b-256 checksum
How to use checksums
d7e0139d063ba4280b1687e7a4dd53e457ed1d4a8ef9b3cb605a123b831654e3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Release files / pyppesnap-1.1.0-py3-none-any.whl

Download URL pyppesnap-1.1.0-py3-none-any.whl
Size 9.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
afc9b9434534c732687bde94097009dff615e24592189ef8f3feba0d9c6505a6
BLAKE2b-256 checksum
How to use checksums
636c93d1421a48d647a9fd3c43206912fc7685221188b493dd56c16912b1ebb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.1

Security for PyppeSnap 1.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page