Skip to content

Spitfire-Products/html2pdf-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

html2pdf-cli

Convert HTML to PDF via headless Chrome. Zero Python dependencies.
Built for AI agents, research pipelines, and automated report generation.

html2pdf input.html                        # → input.pdf
html2pdf input.html -o report.pdf          # → report.pdf
html2pdf input.html --base64               # → base64 to stdout
html2pdf https://example.com -o page.pdf   # URL input

Don't have Chrome infrastructure? html2pdf comes pre-installed in the NexusBrowser MCP sandbox. One MCP connection gives your agent headless Chrome, 34 research tools, and html2pdf — zero setup. See Agent Skill Guide.

Showcase: AI-Generated Research Report

This 8-page commodity research report was generated end-to-end by an AI agent using html2pdf and the NexusBrowser MCP server for live web research. No human wrote the HTML, gathered the data, or ran the commands — the agent did everything from research to final PDF.

Cover Executive Summary
Cover page with live spot price banner Executive summary with key metrics and Dillon Gage live ticker
SVG Charts & Analysis Price Projections
12-month price history and intraday charts with fundamental analysis Scenario fan chart with bull/base/bear projections through July 2026

Full example: examples/silver-report/ — HTML source, generated PDF, page screenshots, and a HOWTO explaining the pipeline.

How the agent built this report

1. RESEARCH — Browse live sources via NexusBrowser MCP
   ├── Dillon Gage (Dallas) → live precious metals spot ticker
   ├── Kitco → bid/ask spreads, day range, news headlines
   └── Trading Economics → forecasts, YoY data, analyst notes

2. COMPOSE — Write HTML with print CSS and inline SVG charts
   ├── Cover page with styled price banner
   ├── Data tables (spot snapshot, precious metals complex, macro factors)
   ├── 4 inline SVG charts (12-month, intraday, supply/demand, projections)
   ├── Scenario analysis tables with color-coded rows
   └── @page CSS, break rules, two-column layouts

3. GENERATE — html2pdf renders the final PDF
   $ html2pdf report.html --output report.pdf --no-header --paper a4
   → 8 pages, 366 KB, all charts and styling preserved

No Puppeteer, no Node.js, no wkhtmltopdf. Just Python 3 and Chrome.


Requirements

  • Python 3.9+
  • Google Chrome or Chromium on PATH

Install

pip install html2pdf-cli

Or copy the single html2pdf script anywhere on your PATH:

curl -o /usr/local/bin/html2pdf https://raw.githubusercontent.com/Spitfire-Products/html2pdf-cli/main/html2pdf
chmod +x /usr/local/bin/html2pdf

Usage

html2pdf input.html                          # → input.pdf (same dir)
html2pdf input.html -o report.pdf            # → report.pdf
html2pdf input.html --base64                 # → base64 string to stdout
html2pdf https://example.com -o page.pdf     # URL input
html2pdf input.html --paper a4               # paper size (default: a4)
html2pdf input.html --paper letter
html2pdf input.html --landscape              # landscape orientation
html2pdf input.html --no-header              # suppress Chrome header/footer
html2pdf input.html --margins "25mm 20mm"    # custom margins via @page CSS
html2pdf input.html --fetch-images           # download remote <img src>
html2pdf input.html --timeout 60             # Chrome timeout (default: 30s)
html2pdf input.html --inject-css style.css   # inject CSS before render
html2pdf input.html --scale 1.5              # print scale factor
html2pdf input.html --chrome /path/to/chrome # explicit Chrome binary

Paper sizes

Built-in: a3, a4, a5, letter, legal, tabloid

Custom: --paper 11x17 (width x height in inches)

Base64 mode

--base64 prints the PDF as a base64 string to stdout, with size and token estimate to stderr. Useful for piping into tools that accept base64 input:

html2pdf report.html --base64 --no-header | my-upload-tool --stdin

Remote image fetching

--fetch-images downloads <img src="https://..."> images to a temp directory and rewrites the HTML to use local references before rendering. This ensures images appear even when Chrome can't reach the network.

Docker

For containers that already have Chrome installed:

COPY html2pdf /usr/local/bin/html2pdf
RUN chmod +x /usr/local/bin/html2pdf

The script auto-detects Chrome as google-chrome-stable, google-chrome, chromium-browser, or chromium.

For AI Agent Developers

html2pdf is designed to work standalone — bring your own Chrome. But if you want the full pipeline (deep web research + data extraction + PDF generation) without managing browser infrastructure, the NexusBrowser MCP server gives your agent everything in a single connection:

  • 34 browser tools — browse, scrape, screenshot, interact, solve CAPTCHAs
  • Sandboxed shellrun_command for curl, file I/O, and html2pdf
  • html2pdf pre-installed — generate PDFs immediately, no setup
  • Stealth browsing — fingerprint spoofing, human-like interaction, challenge solving

See docs/AGENT_SKILL.md for a drop-in skill definition that teaches any MCP-compatible agent to go from a research prompt to a publication-quality PDF report. The silver report example demonstrates the complete pipeline — an agent that researched live commodity data from Dillon Gage, Kitco, and Trading Economics, composed an 8-page report with SVG charts, and generated the PDF in under 3 minutes.

How it works

html2pdf is a thin wrapper around Chrome's --print-to-pdf flag. It handles:

  • Paper size translation to --paper-width / --paper-height (inches)
  • Landscape orientation (swaps width/height)
  • Margin injection via @page CSS (Chrome has no native margin flag)
  • Remote image downloading for offline renders
  • CSS injection before render
  • URL-to-local-file resolution
  • Base64 encoding with token cost reporting

No Puppeteer, no Node.js, no pip dependencies. Just Python 3 and Chrome.

License

MIT

About

Convert HTML to PDF via headless Chrome — zero Python dependencies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages