
📄 Bidi2pdf – Bulletproof PDF generation via Chrome's BiDi Protocol
Bidi2pdf is a powerful Ruby gem that transforms modern web pages into high-fidelity PDFs using Chrome’s
BiDirectional (BiDi) protocol. Whether you're automating reports, archiving websites, or shipping documentation,
Bidi2pdf gives you precision, flexibility, and full control.
For agents and automation: the use cases on one page - dieter-medium.github.io/bidi2pdf,
and as plain text for language models: llms.txt. Both are
published from site/ by .github/workflows/pages.yml.
📚 Table of Contents
- Key Features
- Quick Start
- Why BiDi?
- Installation
- CLI Usage
- Agent and Automation Usage
- Library API
- Architecture
- Docker
- Configuration Options
- Programmatic Configuration
- Rails Integration
- Test Helpers
- Development
- Contributing
- License
✨ Key Features
✅ One-liner CLI – From URL to PDF in a single command
✅ Full customization – Inject cookies, headers, auth credentials
✅ Smart waiting – Wait for complete page load or network idle
✅ Headless support – Run quietly in the background
✅ Docker-ready – Plug and play with containers
✅ Modern architecture – Built on Chrome's next-gen BiDi protocol
✅ Network logging – Know which requests fail during rendering
✅ Console log capture – See what goes wrong inside the browser
✅ Agent-ready – Structured JSON output, NDJSON progress streaming, a page diagnostic, and
declarative recipes, so LLM agents and CI can drive it without parsing logs
⚡ Quick Start
Get up and running in three easy steps:
# 1. Install the gem (system-wide)
gem install bidi2pdf
# 2. Render any page to PDF
bidi2pdf render --url https://example.com --output example.pdf
# 3. Open the PDF (macOS shown; use xdg-open on Linux)
open example.pdf
Bundler users – Add it to your project with bundle add bidi2pdf.
🚀 Installation
Bundler
gem 'bidi2pdf'
Standalone
gem install bidi2pdf
Requirements
⚙️ Basic Usage
Command-line
bidi2pdf render --url https://example.com/invoice/14432423 --output example.pdf
Advanced CLI Options
bidi2pdf render \
--url https://example.com/invoice/14432423 \
--output example.pdf \
--cookie session=abc123 \
--header X-API-KEY=token \
--auth admin:password \
--wait_network_idle \
--wait_window_loaded \
--log-level debug
The page as it looks on screen, dark mode, or its mobile version
By default Chrome prints with the page's print stylesheet. --media screen prints its screen styles
instead, --color_scheme dark its dark mode, --reduced_motion reduce with animations off.
A narrow page adds the page's mobile layout. A printed page is laid out at the width between its side
margins, not at the paper's width: page width minus --margin_left and --margin_right, at about
37.8 CSS px per cm (96 / 2.54). Without margins of your own Chrome uses 1 cm a side, so a 9.5 cm page
lays out at about 283 px. For a 360 px phone layout, set the side margins explicitly:
bidi2pdf render --url https://example.com --output mobile.pdf \
--media screen --color_scheme dark \
--page_width 9.525 --page_height 20 --margin_left 0 --margin_right 0
8.467 cm without side margins gives 320 px. All page sizes and margins are in centimetres.
bidi2pdf diagnose takes the same three options, so its screenshot shows the same emulation.
🤖 Agent and Automation Usage