Skip to main content

Capture screenshots and PDFs from web pages using headless Chrome via CDP.

Project description

nokap

Screenshots and PDFs from web pages. Powered by headless Chrome.

Python Versions PyPI License CI

nokap captures screenshots and PDFs from web pages (or local HTML) using headless Chrome via the Chrome DevTools Protocol. It doesn't need Selenium or Playwright. We use just one lightweight dependency (websockets).

Installation

pip install nokap

Chrome or Chromium must be installed on the system. nokap will hunt it down pretty quickly.

Quick Start

import nokap

# Screenshot a URL
nokap.webshot("https://example.com", "example.png")

# Save as PDF
nokap.webshot("https://example.com", "example.pdf")

# Screenshot with a CSS selector (captures just that element)
nokap.webshot("https://example.com", "header.png", selector="h1")

# From an HTML string (great for table libraries)
nokap.from_html("<h1>Hello, world!</h1>", "hello.png")

API

nokap.webshot()

Take a screenshot or PDF of a web page.

nokap.webshot(
    url,                # URL or local file path
    file="webshot.png", # Output path (.png, .jpg, .webp, .pdf)
    *,
    vwidth=992,         # Viewport width (px)
    vheight=744,        # Viewport height (px)
    selector=None,      # CSS selector to capture
    cliprect=None,      # Clip rectangle (x, y, width, height)
    expand=0,           # Padding around selector (px)
    delay=0.2,          # Wait after page load (seconds)
    zoom=1,             # Scale factor (2 = retina)
    useragent=None,     # Custom User-Agent string
)

nokap.from_html()

Render an HTML string to an image or PDF. Designed for integration with table/report libraries.

nokap.from_html(
    html,               # HTML content
    file="webshot.png", # Output path
    *,
    selector="html",    # CSS selector to capture
    **kwargs,           # All webshot() options
)

nokap.close()

Shut down the background Chrome process. Called automatically at exit, but available for explicit cleanup.

nokap.close()

How It Works

nokap communicates directly with Chrome over the Chrome DevTools Protocol via WebSockets. No browser driver binaries, no heavyweight automation frameworks.

The architecture:

  1. Launch: Finds and starts headless Chrome with a random debugging port
  2. Connect: Opens a WebSocket to Chrome's CDP endpoint
  3. Capture: Creates a tab, navigates, waits, then calls Page.captureScreenshot or Page.printToPDF
  4. Cleanup: Closes the tab; Chrome stays running for reuse until nokap.close() or process exit

Features

Feature Details
Image formats PNG, JPEG, WebP
PDF generation Configurable page size, margins, orientation
CSS selectors Capture specific elements (or union of multiple)
Zoom/scale Produce retina-quality (2×, 3×) images
Expand/padding Add whitespace around captured elements
Local HTML Render HTML strings or local .html files
Viewport control Set width/height for responsive layouts
Custom User-Agent Override the browser UA string
Auto-cleanup Chrome process managed via atexit
Jupyter-safe Works in notebooks (no event loop conflicts)

Configuration

Environment Variable Purpose
CHROME_PATH Override Chrome binary location

Requirements

  • Python ≥ 3.10
  • Chrome or Chromium installed on the system

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nokap-0.1.0.tar.gz (77.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nokap-0.1.0-py3-none-any.whl (36.2 kB view details)

Uploaded Python 3

File details

Details for the file nokap-0.1.0.tar.gz.

File metadata

  • Download URL: nokap-0.1.0.tar.gz
  • Upload date:
  • Size: 77.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nokap-0.1.0.tar.gz
Algorithm Hash digest
SHA256 63071cf5e7c4f0487532f6281c6ea85d90673d5cd835ffe5e64c913989742eb9
MD5 32060477d731431ebad8fec3bfaa6fd7
BLAKE2b-256 b9f7a4f3d6f89773df40884bc10790977c80a9ea17ad526ea0b0cd35a703595c

See more details on using hashes here.

File details

Details for the file nokap-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: nokap-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 36.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nokap-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 36f2cae8d4251945dbe311ef68916c7eddfe51da490aabe0d68c5d13294b2539
MD5 b0cff1c402fb06dfcd206bc3f7927a1e
BLAKE2b-256 73dcc35af5ccf8dbf3268750c54a3c0830f486b8faec38c811aff92335aa99ba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page