nokap
Capture screenshots and PDFs from web pages using headless Chrome via CDP.
Installation
pip install nokap
Chrome or Chromium must be installed separately. nokap auto-discovers the
browser binary, or you can set CHROME_PATH.
Decision Table
| Need |
Use |
| Screenshot a URL |
nokap.webshot(url, "out.png") |
| Screenshot a local HTML file |
nokap.webshot("page.html", "out.png") |
| Screenshot raw HTML string |
nokap.from_html(html_str, "out.png") |
| Capture specific element |
nokap.webshot(url, "out.png", selector="table") |
| High-resolution capture (2x) |
nokap.webshot(url, "out.png", zoom=2) |
| Add padding around element |
nokap.webshot(url, "out.png", selector="h1", expand=10) |
| Full-page PDF |
nokap.webshot(url, "out.pdf") |
| Element-bounded PDF |
nokap.webshot(url, "out.pdf", selector="table") |
| Capture from HTML to PDF |
nokap.from_html(html_str, "out.pdf", selector="table") |
| Clean up browser process |
nokap.close() |
| CLI screenshot |
nokap webshot URL file.png |
| CLI from HTML file |
nokap from-html file.html out.png |
| Check Chrome availability |
nokap doctor or nokap info |
Core API
webshot(url, file, *, ...)
Main capture function. Output format determined by file extension (.png,
.jpg, .webp, .pdf).
Key parameters:
url: URL or local file path (auto-converted to file://)
file: Output path (default: "webshot.png")
selector: CSS selector to crop to element's bounding box
cliprect: Explicit (x, y, width, height) clip rectangle
expand: Padding around selector (int for all sides, or 4-tuple)
zoom: Scale factor for raster images (>1 = higher resolution)
delay: Seconds to wait after page load (default: 0.2)
vwidth / vheight: Viewport dimensions (default: 992×744)
useragent: Custom User-Agent string
from_html(html, file, *, selector="html", encoding="utf-8", **kwargs)
Render an HTML string to image or PDF. Writes HTML to a temp file and calls
webshot(). Accepts all webshot() keyword arguments.
close()
Explicitly close the module-level browser. Called automatically at exit, but
use this in long-running processes or after batch captures.
Gotchas
- The module name is
nokap, not no-kap or no_kap.
selector and cliprect are mutually exclusive: never pass both.
zoom only affects raster images (PNG/JPEG/WebP), not PDF output. PDFs are vector and always sharp.
from_html() defaults to selector="html" (full document), while webshot() defaults to selector=None (viewport only).
- The browser singleton auto-starts on first call. Call
nokap.close() to free resources in long-running scripts.
- Local file paths are auto-converted to
file:// URLs: no manual conversion needed.
expand uses CSS pixel units and applies padding around the selector bounding box.
- For wide elements (tables), nokap auto-detects intrinsic width and widens the viewport to avoid clipping.
- Chrome must be installed separately: nokap does not bundle a browser.
- Set
CHROME_PATH environment variable if Chrome is not in a standard location.
Error Handling
All nokap errors inherit from NokapError:
import nokap
try:
nokap.webshot("https://example.com", "out.png", selector="#missing")
except nokap.SelectorError:
print("Element not found")
except nokap.PageLoadTimeout:
print("Page took too long")
except nokap.ChromeNotFoundError:
print("Install Chrome first")
except nokap.NokapError as e:
print(f"Capture failed: {e}")
Resources
1---2name: nokap3description: Capture screenshots and PDFs from web pages using headless Chrome via CDP. Use when writing Python code that captures, screenshots, or renders web pages, local HTML files, or raw HTML strings to images or PDFs.4license: MIT5---67# nokap89Capture screenshots and PDFs from web pages using headless Chrome via CDP.1011## Installation1213```bash14pip install nokap15```1617Chrome or Chromium must be installed separately. nokap auto-discovers the18browser binary, or you can set `CHROME_PATH`.1920## Decision Table2122| Need | Use |23|------|-----|24| Screenshot a URL | `nokap.webshot(url, "out.png")` |25| Screenshot a local HTML file | `nokap.webshot("page.html", "out.png")` |26| Screenshot raw HTML string | `nokap.from_html(html_str, "out.png")` |27| Capture specific element | `nokap.webshot(url, "out.png", selector="table")` |28| High-resolution capture (2x) | `nokap.webshot(url, "out.png", zoom=2)` |29| Add padding around element | `nokap.webshot(url, "out.png", selector="h1", expand=10)` |30| Full-page PDF | `nokap.webshot(url, "out.pdf")` |31| Element-bounded PDF | `nokap.webshot(url, "out.pdf", selector="table")` |32| Capture from HTML to PDF | `nokap.from_html(html_str, "out.pdf", selector="table")` |33| Clean up browser process | `nokap.close()` |34| CLI screenshot | `nokap webshot URL file.png` |35| CLI from HTML file | `nokap from-html file.html out.png` |36| Check Chrome availability | `nokap doctor` or `nokap info` |3738## Core API3940### `webshot(url, file, *, ...)`4142Main capture function. Output format determined by file extension (`.png`,43`.jpg`, `.webp`, `.pdf`).4445Key parameters:4647- `url`: URL or local file path (auto-converted to `file://`)48- `file`: Output path (default: `"webshot.png"`)49- `selector`: CSS selector to crop to element's bounding box50- `cliprect`: Explicit `(x, y, width, height)` clip rectangle51- `expand`: Padding around selector (int for all sides, or 4-tuple)52- `zoom`: Scale factor for raster images (>1 = higher resolution)53- `delay`: Seconds to wait after page load (default: 0.2)54- `vwidth` / `vheight`: Viewport dimensions (default: 992×744)55- `useragent`: Custom User-Agent string5657### `from_html(html, file, *, selector="html", encoding="utf-8", **kwargs)`5859Render an HTML string to image or PDF. Writes HTML to a temp file and calls60`webshot()`. Accepts all `webshot()` keyword arguments.6162### `close()`6364Explicitly close the module-level browser. Called automatically at exit, but65use this in long-running processes or after batch captures.6667## Gotchas68691. The module name is `nokap`, not `no-kap` or `no_kap`.702. `selector` and `cliprect` are mutually exclusive: never pass both.713. `zoom` only affects raster images (PNG/JPEG/WebP), not PDF output. PDFs are vector and always sharp.724. `from_html()` defaults to `selector="html"` (full document), while `webshot()` defaults to `selector=None` (viewport only).735. The browser singleton auto-starts on first call. Call `nokap.close()` to free resources in long-running scripts.746. Local file paths are auto-converted to `file://` URLs: no manual conversion needed.757. `expand` uses CSS pixel units and applies padding around the selector bounding box.768. For wide elements (tables), nokap auto-detects intrinsic width and widens the viewport to avoid clipping.779. Chrome must be installed separately: nokap does not bundle a browser.7810. Set `CHROME_PATH` environment variable if Chrome is not in a standard location.7980## Error Handling8182All nokap errors inherit from `NokapError`:8384```python85import nokap8687try:88 nokap.webshot("https://example.com", "out.png", selector="#missing")89except nokap.SelectorError:90 print("Element not found")91except nokap.PageLoadTimeout:92 print("Page took too long")93except nokap.ChromeNotFoundError:94 print("Install Chrome first")95except nokap.NokapError as e:96 print(f"Capture failed: {e}")97```9899## Resources100101- [Full documentation](https://posit-dev.github.io/nokap/)102- [llms.txt](https://posit-dev.github.io/nokap/llms.txt): API overview for LLMs103- [llms-full.txt](https://posit-dev.github.io/nokap/llms-full.txt): Complete reference104- [Source code](https://github.com/posit-dev/nokap)