web/page-to-pdf.py
Render a webpage to a PDF (headless Chromium via Playwright) -- the print-to-PDF cousin of web/screenshot.py. Pass headers=<json object> to send extra request headers, as in web/screenshot.py.
Use when the user wants a PDF of a web page as the browser would print it, including layout and images.
- the file
- web/page-to-pdf.py
- its skill
- web/SKILL.md (the whole web pack)
- as knowledge
- okf/web/page-to-pdf.md
- on GitHub
- web/page-to-pdf.py ↗
Run it
1. Once per machine: install fragletc (Docker must already be running).
curl -fsSL https://raw.githubusercontent.com/ofthemachine/fraglet/main/install.sh | sh
2. Fetch the tool and print its contract.
curl -fsSLO https://tools.ofthemachine.com/web/page-to-pdf.py && chmod +x page-to-pdf.py && ./page-to-pdf.py --fraglet-help
3. Run it.
./page-to-pdf.py -p url="<url>" [-p headers="<headers>"] --output page.pdf
Parameters
| name | type | requirement | description |
|---|---|---|---|
| url | string | required | Page URL to render |
| headers | string | optional | JSON object of extra non-secret HTTP headers (e.g. Accept-Language); recorded in plaintext in receipts, so never credentials |
Outputs
page.pdf
Provenance
- image
- ofthemachine/headless-browser@sha256:f79c496c6737113c0f6e2d648474a67fb1a416fa34c22220d5714d0b7c6a6036
- reach
network=required— output depends on live external state; do not cache indefinitely.- procedure_hash
- sha256:e6c751e4af2b550620d30da2809fdf059802332f245d418bb4ec7dc5db2a7443
- receipt
- add
--receipt run.jsonto any run;receipt/attest.pyverifies it against this hash without re-running.
Source
page-to-pdf.py · 39 lines · sha256 e6c751e4af2b…
#!/usr/bin/env -S fragletc --image ofthemachine/headless-browser@sha256:f79c496c6737113c0f6e2d648474a67fb1a416fa34c22220d5714d0b7c6a6036
#: d=Render a webpage to a PDF (headless Chromium via Playwright) -- the print-to-PDF cousin of web/screenshot.py. Pass headers=<json object> to send extra request headers, as in web/screenshot.py.
#: when=Use when the user wants a PDF of a web page as the browser would print it, including layout and images.
#: network=required
#: stdin=none
#: param=url:required:d=Page URL to render
#: param=headers:d=JSON object of extra non-secret HTTP headers (e.g. Accept-Language); recorded in plaintext in receipts, so never credentials
#: output=page.pdf
import json
import os
import sys
from playwright.sync_api import sync_playwright
url = os.environ["URL"]
headers_raw = os.environ.get("HEADERS", "")
headers = {}
if headers_raw:
try:
headers = json.loads(headers_raw)
except json.JSONDecodeError as e:
print(f"headers param is not valid JSON: {e}", file=sys.stderr)
sys.exit(1)
with sync_playwright() as p:
browser = p.chromium.launch(headless=True)
context = browser.new_context(viewport={"width": 1280, "height": 800}, extra_http_headers=headers)
page = context.new_page()
try:
page.goto(url, wait_until="load", timeout=30_000)
page.pdf(path="/output/page.pdf", print_background=True)
except Exception as e:
print(f"pdf render failed: {e}", file=sys.stderr)
sys.exit(1)
finally:
browser.close()
print(f"Rendered PDF of {url} -> page.pdf")