bidi2pdf 0.1.24 โ†’ 0.1.26

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 924c1d8c78d46fdb5e08e333861afbac5384a17b3d4a78c365f9cd09d88b5068
4
- data.tar.gz: 973fcae887672a67d00acb6ee131866ef904f2b6f67661f180b7e51f16b90c3f
3
+ metadata.gz: f9f6c1ea872b0d4250ca35a59990f11f3d0c3599e55b3560e6c6ba925b045dd7
4
+ data.tar.gz: 8c4ae64d0944396a7eed9dc09288747a17491a1235d9f64f3bdb2ea5eb222bcb
5
5
  SHA512:
6
- metadata.gz: f73a0fddac84bfc084e57a2a5353587d00d72ad9dd9f37d6ec35ce86050794b033fed60a7ed8d7425ee5624b9a1757941e8f646b1155057f171f704c2799811e
7
- data.tar.gz: 96dcbd832b570bc9723581cffc4eeae40bb547eb7eb9ca81578911ea0985953c3353b7ac4c8896cfd490240862b48d88aade7d38e4cb788a2c23058ee20350d0
6
+ metadata.gz: dd5616ed110fe4018b78ce18f8403302e1bba37a41fc470a2ed03ce76fc879042a73528ecd81afd21d2fe824c08d5f0034bcbed1bd517ff00dc05b970dfe4eec
7
+ data.tar.gz: b50f99a5143f851b435325763d3b4c1a3817e459170c17ea652c6dbb10ce43ef5ce7a4eec34d01d4e3f4df8c58be51b358167dfe9a84615fbddb9bc948021a3e
@@ -0,0 +1,21 @@
1
+ {
2
+ "name": "bidi2pdf",
3
+ "interface": {
4
+ "displayName": "bidi2pdf"
5
+ },
6
+ "plugins": [
7
+ {
8
+ "name": "bidi2pdf",
9
+ "source": {
10
+ "source": "local",
11
+ "path": "./claude-plugin"
12
+ },
13
+ "policy": {
14
+ "installation": "AVAILABLE",
15
+ "authentication": "ON_USE",
16
+ "products": ["CODEX"]
17
+ },
18
+ "category": "Developer Tools"
19
+ }
20
+ ]
21
+ }
data/CHANGELOG.md CHANGED
@@ -8,10 +8,46 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
8
8
 
9
9
  ## [Unreleased]
10
10
 
11
- [unreleased]: https://github.com/dieter-medium/bidi2pdf/compare/v0.1.24..HEAD
11
+ [unreleased]: https://github.com/dieter-medium/bidi2pdf/compare/v0.1.26..HEAD
12
12
 
13
13
  <!-- generated by git-cliff end -->
14
14
 
15
+ ## [0.1.26] - 2026-10-29
16
+
17
+ ### ๐Ÿ› Fixed
18
+ - Check screenshot widths on a page that fits
19
+ - Check screenshot widths on pages without Paged.js
20
+ - Read the session start limit once per process
21
+ - Keep session gate permits on every path
22
+
23
+ ### ๐Ÿ”„ Changed
24
+ - Merge pull request #184 from dieter-medium/feat/session-start-gate
25
+
26
+ ### ๐Ÿš€ Added
27
+ - Limit concurrent Chrome session setups
28
+
29
+ ## [0.1.25] - 2026-10-27
30
+
31
+ ### ๐Ÿ› Fixed
32
+ - Keep the websocket-native hint off stdout
33
+ - Load the version constant with the library
34
+ - Trim plugin ground rules and preflight
35
+ - Keep plugin agents on bidi2pdf's own path
36
+ - Follow the gem's 0.1.25.pre in the Codex manifest
37
+ - Set SKILL_DIR before the screenshot script runs
38
+ - Check landing page options per command
39
+
40
+ ### ๐Ÿ”„ Changed
41
+ - Merge pull request #181 from dieter-medium/fix/require-version
42
+ - Merge pull request #180 from dieter-medium/fix/plugin-ground-rules
43
+ - Merge pull request #179 from dieter-medium/feat/codex-plugin
44
+ - Merge pull request #178 from dieter-medium/feat/agent-landing-page
45
+ - Merge pull request #177 from dieter-medium/chore/plugin-icon
46
+
47
+ ### ๐Ÿš€ Added
48
+ - Offer the plugin's skills to Codex
49
+ - Publish a landing page and llms.txt for agents
50
+
15
51
  ## [0.1.24] - 2026-10-27
16
52
 
17
53
  ### ๐Ÿ› Fixed
@@ -611,7 +647,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
611
647
 
612
648
  ### ๐Ÿ”„ Released
613
649
 
614
- - [unreleased](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.24..HEAD)
650
+ - [unreleased](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.26..HEAD)
651
+ - [0.1.26](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.25..v0.1.26)
652
+ - [0.1.25](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.24..v0.1.25)
615
653
  - [0.1.24](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.23..v0.1.24)
616
654
  - [0.1.23](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.22..v0.1.23)
617
655
  - [0.1.22](https://github.com/dieter-medium/bidi2pdf/compare/v0.1.21..v0.1.22)
data/README.md CHANGED
@@ -11,6 +11,10 @@
11
11
  **BiDirectional (BiDi)** protocol. Whether you're automating reports, archiving websites, or shipping documentation,
12
12
  Bidi2pdf gives you **precision, flexibility, and full control**.
13
13
 
14
+ **For agents and automation:** the use cases on one page - [dieter-medium.github.io/bidi2pdf](https://dieter-medium.github.io/bidi2pdf/),
15
+ and as plain text for language models: [llms.txt](https://dieter-medium.github.io/bidi2pdf/llms.txt). Both are
16
+ published from `site/` by `.github/workflows/pages.yml`.
17
+
14
18
  ---
15
19
 
16
20
  ## ๐Ÿ“š Table of Contents
@@ -202,7 +206,8 @@ still emits exactly this shape, with `ok: false` and a structured `error` (`code
202
206
  ### stdin/stdout, manifests, and progress streaming
203
207
 
204
208
  ```bash
205
- # Render HTML piped on stdin, PDF bytes piped out on stdout - no files touched
209
+ # Render HTML piped on stdin, PDF bytes piped out on stdout - no input or output file of your own
210
+ # (the PDF goes straight to stdout; the HTML is kept in a temporary file while Chrome loads it, then deleted)
206
211
  cat page.html | bidi2pdf render --stdin --output - > page.pdf
207
212
 
208
213
  # A render manifest: enough to reproduce and diagnose the render later
@@ -283,13 +288,17 @@ agent uses bidi2pdf the way it is meant to be used instead of guessing options:
283
288
 
284
289
  | Skill | For |
285
290
  |---|---|
286
- | `render-pdf` | a URL or HTML file to PDF with the CLI: order of commands, waits, screen/dark/mobile view, reading the JSON result and exit codes |
291
+ | `render-pdf` | a URL or HTML file to PDF with the CLI: a preflight that suggests how to get Ruby and a browser, order of commands, waits, screen/dark/mobile view, reading the JSON result and exit codes |
287
292
  | `pdf-recipe` | a recipe for pages that need steps before printing, validated without a browser |
288
293
  | `diagnose-pdf` | a blank, unstyled or wrongly paged PDF: a checklist around `bidi2pdf diagnose` |
289
294
  | `remote-chrome` | a shared chromedriver container: settings, sizing, session cleanup, untrusted HTML |
290
295
  | `page-screenshots` | review screenshots per page at phone and desktop width, light and dark (with a script) |
291
296
  | `pdf-specs` | RSpec specs with the gem's PDF matchers and Chrome container helpers |
292
297
 
298
+ Every skill starts with short ground rules: bidi2pdf is the only renderer (a failure is reported,
299
+ not worked around with another PDF engine), nothing is installed without the user's go-ahead, and
300
+ bidi2pdf's own JSON result comes before other PDF tools. `render-pdf`'s preflight has the setup details.
301
+
293
302
  #### Installing the skills
294
303
 
295
304
  The plugin is not in a central store: this repository is its own Claude Code marketplace
@@ -361,6 +370,22 @@ shapes; a spec (`spec/unit/claude_plugin_spec.rb`) checks every command, option,
361
370
  method they name against the gem, so they cannot drift from it. The plugin is not part of the gem
362
371
  itself.
363
372
 
373
+ #### In Codex
374
+
375
+ The same six skills install in OpenAI's Codex. Codex reads the plugin folder through its portable manifest
376
+ (`claude-plugin/plugin.json`, Agent Plugins 1.0.0, listing under `extensions.com.openai`) and the repository's
377
+ own marketplace, `.agents/plugins/marketplace.json`:
378
+
379
+ ```bash
380
+ codex plugin marketplace add dieter-medium/bidi2pdf # follows the default branch
381
+ codex plugin marketplace add dieter-medium/bidi2pdf@v<version> # pinned to a release tag (or a commit SHA)
382
+ ```
383
+
384
+ Then install **bidi2pdf** from that marketplace in Codex's plugin browser (`/plugins` in the CLI, or the
385
+ Plugins Directory in the desktop app) and start a new session. The marketplace entry offers the plugin for Codex
386
+ only (`"products": ["CODEX"]`, as in OpenAI's own marketplace): the skills run `bidi2pdf` in a shell, which
387
+ ChatGPT does not have. `codex plugin marketplace upgrade bidi2pdf` fetches a newer state of the marketplace.
388
+
364
389
  ---
365
390
 
366
391
  ## ๐Ÿง  Programmatic API
@@ -700,6 +725,8 @@ Bidi2pdf.configure do |config|
700
725
  config.enable_default_logging_subscriber = true
701
726
  config.log_truncate_limit = 200 # bytes - see below
702
727
  config.chromedriver_log_level = "WARNING" # see below
728
+ config.session_start_concurrency = 2 # Chrome session setups at once in this process - see "Session start limit"
729
+ config.session_start_timeout = nil # seconds a setup waits for its turn; nil: default_timeout
703
730
  end
704
731
  ```
705
732
 
@@ -709,16 +736,115 @@ end
709
736
  | `enable_default_logging_subscriber` | `true` | Subscribes a default logger to the gem's internal instrumentation events. |
710
737
  | `log_truncate_limit` | `200` | Max bytes kept when logging a value that can be large (e.g. a `data:` URL) - truncated at a byte, not character, boundary. |
711
738
  | `chromedriver_log_level` | `nil` | ChromeDriver's own `--log-level` (`"ALL"`/`"INFO"`/`"WARNING"`/`"SEVERE"`). Unset mirrors `Bidi2pdf.logger.level`; set explicitly to quiet ChromeDriver's own (often very verbose) output independently of your app's log level. |
739
+ | `session_start_concurrency` | `2` | How many Chrome session setups (session request to first open tab) may run at once in this process; `nil` turns the limit off. See "Session start limit". |
740
+ | `session_start_timeout` | `nil` | Seconds a setup waits for its turn before it fails with `SessionStartTimeoutError` (code `BROWSER_LAUNCH_FAILED`, retryable); `nil` uses `default_timeout`. |
741
+ | `session_gate` | `nil` | An object of your own answering `with_permit { ... }` - e.g. a lock shared by several processes - instead of the per-process semaphore. See "Session start limit". |
712
742
 
713
743
  `Bidi2pdf.logger`, `Bidi2pdf.network_events_logger`, `Bidi2pdf.browser_console_logger`, and
714
744
  `Bidi2pdf.notification_service` are also configurable in the same block, for more advanced
715
745
  logging/instrumentation needs.
716
746
 
747
+ ### Session start limit
748
+
749
+ When several new Chrome sessions are set up at the same moment, chromedriver/Chrome sometimes leaves one of the
750
+ setup commands unanswered (the first `browsingContext.create` or `navigate` of a fresh session): the render waits
751
+ out its command timeout, and the session is left behind unanswering, which wears the Chrome down for the renders
752
+ after it. Measured on chromedriver/Chrome 156 against one remote chromedriver: two setups at once stayed clean in
753
+ 720 renders; three at once failed in the first round.
754
+
755
+ So bidi2pdf lets at most `session_start_concurrency` setups run at once in a process - from the session request to
756
+ the first open tab, in every path (the CLI, `Bidi2pdf::DSL.with_tab`, the session warmer). Rendering is never
757
+ limited: eight PDFs can render side by side while their setups took turns. A setup that cannot get a turn within
758
+ `session_start_timeout` fails with `SessionStartTimeoutError` instead of queueing for ever. Every turn instruments
759
+ `session_gate.bidi2pdf` (`limit`, `wait` in seconds, `acquired`).
760
+
761
+ **The limit holds per process.** Chrome sees the setups of every process that talks to it, so processes sharing one
762
+ Chrome divide its budget: two processes with a limit of 1 each are as safe as one with 2 - two with 2 each failed in
763
+ every run. With Puma workers and a job process on one remote chromedriver, set the limit so that limit x processes
764
+ stays at about 2, or give the processes Chromes of their own.
765
+
766
+ It is one budget per process, whichever Chrome a setup goes to: two independent chromedrivers (a local one and a
767
+ remote one, or two remote endpoints) still take turns. The limit is read once per process, at its first session
768
+ setup (and again in a forked child): set it at boot, in an initializer. A later change is logged and takes effect
769
+ after a restart.
770
+
771
+ **A gate across processes.** `config.session_gate` takes an object of your own instead of the per-process
772
+ semaphore - anything answering `with_permit { ... }` - so several processes can share one budget. Every setup
773
+ path (CLI, `Bidi2pdf::DSL`, the session warmer, bidi2pdf-rails) asks it, and `session_gate.bidi2pdf` is
774
+ instrumented as before. Your gate has to run the block with a permit, give the permit back however the block ends,
775
+ raise `Bidi2pdf::SessionStartTimeoutError` when none comes free in time, and not lose permits when a process dies
776
+ or forks (`session_start_concurrency` and `session_start_timeout` apply only if it reads them). A sketch for
777
+ processes on one host - the kernel drops a `flock` when its process ends:
778
+
779
+ ```ruby
780
+ # N lock files in a directory every process can reach: one free file is one permit.
781
+ class FileLockGate
782
+ def initialize(dir, slots: 2, timeout: 60)
783
+ @paths = Array.new(slots) { |index| File.join(dir, "session-start-#{index}.lock") }
784
+ @timeout = timeout
785
+ end
786
+
787
+ def with_permit
788
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @timeout
789
+ loop do
790
+ @paths.each do |path|
791
+ File.open(path, File::RDWR | File::CREAT, 0o600) do |file|
792
+ return yield if file.flock(File::LOCK_EX | File::LOCK_NB) # closing the file releases the lock
793
+ end
794
+ end
795
+ raise Bidi2pdf::SessionStartTimeoutError, "no session start permit free" if Process.clock_gettime(Process::CLOCK_MONOTONIC) > deadline
796
+
797
+ sleep 0.05
798
+ end
799
+ end
800
+ end
801
+
802
+ Bidi2pdf.configure { |config| config.session_gate = FileLockGate.new("/var/lib/myapp/bidi2pdf", slots: 2) }
803
+ ```
804
+
805
+ **The evidence.** bidi2pdf's own measurements come first (2026-10-28, chromedriver/Chrome 156, one remote
806
+ chromedriver, the chromedriver swept between runs): at a limit of 2, 0 of 720 renders failed at 4, 6 and 8 renders at
807
+ once in ten interleaved rounds; at 3, renders failed from the first round on a freshly started Chrome; ungated, 104 of
808
+ 160 failed. Every unanswered command was traced as written to an open WebSocket, with no reply carrying its id ever
809
+ read and no error raised - the client loses nothing. `bin/session-start-repro` reproduces it against any chromedriver
810
+ (`LIMIT=none` against `LIMIT=2`).
811
+
812
+ **Related reports.** No upstream report of exactly this was found (searched 2026-10-29: chromium-bidi, ChromeDriver,
813
+ Selenium, Puppeteer, WebdriverIO, Mozilla). What follows is context, not confirmation - no other project has
814
+ reported this bug.
815
+
816
+ Related concurrency and lifecycle defects in WebDriver BiDi:
817
+
818
+ - [Mozilla bug 2019511](https://bugzilla.mozilla.org/show_bug.cgi?id=2019511) "browsingContext.setViewport can
819
+ timeout if several contexts are created in parallel" - the closest analogy. In Firefox's BiDi a context's active
820
+ state could change before a waiting command ran, so a command depending on it timed out under parallel context
821
+ creation; fixed in Firefox 150 by always updating the dimensions synchronously in `browsingContext.create`.
822
+ Another browser and another cause, but the same kind of race: parallel context creation leaving a command
823
+ waiting.
824
+ - ChromeDriver 127, [crbug 341048786](https://crbug.com/341048786) and [crbug 1503101](https://crbug.com/1503101)
825
+ ([crrev.com/c/5575497](https://crrev.com/c/5575497)): a race between ChromeDriver and the BiDi mapper during
826
+ session creation, fixed by waiting for `browsingContext.getTree` to return a non-empty list. The same phase - a
827
+ session's start - but already fixed in the 156 measured here, so what bidi2pdf meets is a race still left there.
828
+ - ChromeDriver 126, [crbug 4762](https://crbug.com/4762) ([crrev.com/c/5515013](https://crrev.com/c/5515013)), and
829
+ 144, [crbug 458027819](https://crbug.com/458027819) ([crrev.com/c/7128058](https://crrev.com/c/7128058)): Chrome
830
+ throttling background and occluded windows in BiDi sessions. bidi2pdf already passes
831
+ `--disable-background-timer-throttling`, `--disable-renderer-backgrounding` and
832
+ `--disable-backgrounding-occluded-windows`.
833
+
834
+ The same symptom, no shown common cause:
835
+
836
+ - [puppeteer#4039](https://github.com/puppeteer/puppeteer/issues/4039) "browser.newPage freezes (never resolves)" -
837
+ a new page that never comes back; no root cause was found, closed as out of scope.
838
+ - [webdriverio#16051](https://github.com/webdriverio/webdriverio/issues/16051) - pending BiDi commands waiting for
839
+ their whole timeout after the connection closed, because the client never rejected them; fixed in WebdriverIO
840
+ (PR #16054). A client-side cause of the same symptom, ruled out for bidi2pdf by the trace above.
841
+
717
842
  ### Pre-warmed sessions (`Bidi2pdf::SessionWarmer`)
718
843
 
719
844
  Optional. Keeps a few Chrome sessions started in the background so a render skips browser startup.
720
845
  Every slot is still used for exactly one render and then discarded - isolation is the same as
721
- launching a fresh Chrome per PDF.
846
+ launching a fresh Chrome per PDF. A slot is warmed together with its user context, window and tab,
847
+ under one turn of the session start limit, so a checkout sets up nothing.
722
848
 
723
849
  ```ruby
724
850
  Bidi2pdf::SessionWarmer.configure do |c|
data/lib/bidi2pdf/dsl.rb CHANGED
@@ -40,6 +40,8 @@ module Bidi2pdf
40
40
  def self.with_tab(remote_browser_url: nil, port: 0, headless: true, chrome_args: Bidi2pdf::Bidi::Session::DEFAULT_CHROME_ARGS.dup)
41
41
  manager = nil
42
42
  session = nil
43
+ context = nil
44
+ window = nil
43
45
  tab = nil
44
46
 
45
47
  begin
@@ -56,13 +58,16 @@ module Bidi2pdf
56
58
  manager.session
57
59
  end
58
60
 
59
- session.start
60
- session.client.on_close { Bidi2pdf.logger.info "WebSocket session closed" }
61
+ # Only the setup waits for the session start limit, the work in the tab does not.
62
+ Bidi2pdf::SessionGate.with_permit do
63
+ session.start
64
+ session.client.on_close { Bidi2pdf.logger.info "WebSocket session closed" }
61
65
 
62
- browser = session.browser
63
- context = browser.create_user_context
64
- window = context.create_browser_window
65
- tab = window.create_browser_tab
66
+ browser = session.browser
67
+ context = browser.create_user_context
68
+ window = context.create_browser_window
69
+ tab = window.create_browser_tab
70
+ end
66
71
 
67
72
  yield(tab)
68
73
  ensure
@@ -18,6 +18,7 @@ module Bidi2pdf
18
18
  InvalidRecipeError => "INVALID_RECIPE",
19
19
  PdfInspectionUnavailableError => "PDF_INSPECTION_UNAVAILABLE",
20
20
  SessionNotStartedError => "BROWSER_LAUNCH_FAILED",
21
+ SessionStartTimeoutError => "BROWSER_LAUNCH_FAILED",
21
22
  CmdTimeoutError => "COMMAND_TIMEOUT",
22
23
  WebsocketError => "BROWSER_DISCONNECTED",
23
24
  NavigationTimeoutError => "NAVIGATION_TIMEOUT",
@@ -0,0 +1,109 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "concurrent"
4
+
5
+ module Bidi2pdf
6
+ # At most Bidi2pdf.session_start_concurrency Chrome session setups at once in this process - from the session
7
+ # request to the first open tab (README "Session start limit"). When several new sessions are set up at the same
8
+ # moment, chromedriver/Chrome sometimes leaves a setup command unanswered and the session hangs until the command
9
+ # times out (measured on chromedriver/Chrome 156: three setups at once failed, two did not). Rendering is never
10
+ # limited, only the setup before it.
11
+ #
12
+ # The limit holds per process: processes sharing one Chrome (Puma workers, a job process) divide its budget
13
+ # between them. It is one budget for the process, whichever Chrome a setup goes to - two independent chromedrivers
14
+ # still take turns (an endpoint-scoped gate would not; not built). nil turns the gate off.
15
+ #
16
+ # The limit is read once per process - at its first setup, and again in a forked child, whose semaphore is its own
17
+ # so permits held by the parent's threads at the fork cannot be lost in it. A later change takes effect after a
18
+ # restart and is logged: set it at boot (an initializer), not while setups run. Changing it live would have to
19
+ # keep the setups in flight counted, which needs an API Concurrent::Semaphore does not document.
20
+ #
21
+ # Bidi2pdf.session_gate replaces the semaphore with an object of your own, to share the budget across processes
22
+ # (a file lock, a database or Redis lock). Its contract: +with_permit+ runs the block only while it holds a permit,
23
+ # gives the permit back however the block ends, raises Bidi2pdf::SessionStartTimeoutError when none comes free in
24
+ # time, and loses no permit when a process dies or forks. Every setup path still comes through here, so the
25
+ # notification below is sent either way - it trusts that the block runs under a permit. session_start_concurrency
26
+ # and session_start_timeout then apply only if that object reads them.
27
+ module SessionGate
28
+ LOCK = Mutex.new
29
+
30
+ class << self
31
+ # Runs the block with a permit, released however the block ends. Waits for one at most
32
+ # Bidi2pdf.session_start_timeout (default: Bidi2pdf.default_timeout) seconds, then raises
33
+ # SessionStartTimeoutError - a queue behind a hung setup fails loudly instead of growing.
34
+ def with_permit(&)
35
+ return with_custom_gate(Bidi2pdf.session_gate, &) if Bidi2pdf.session_gate
36
+
37
+ semaphore, limit = current
38
+ return yield unless semaphore
39
+
40
+ acquired, waited = acquire(semaphore)
41
+ refuse(limit, waited) unless acquired
42
+
43
+ # Everything after the permit is taken sits inside the ensure - a raising subscriber must not keep it.
44
+ begin
45
+ Bidi2pdf.notification_service.instrument("session_gate.bidi2pdf", { limit: limit, wait: waited, acquired: true })
46
+ yield
47
+ ensure
48
+ semaphore.release(1)
49
+ end
50
+ end
51
+
52
+ # Forgets the semaphore; the next permit builds one for the configured limit. For specs.
53
+ def reset!
54
+ LOCK.synchronize { @semaphore = @limit = @pid = @warned_limit = nil }
55
+ end
56
+
57
+ private
58
+
59
+ # The wait is the time until the custom gate runs the block - it reports a timeout itself, by raising.
60
+ def with_custom_gate(gate)
61
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
62
+ gate.with_permit do
63
+ waited = (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).round(3)
64
+ Bidi2pdf.notification_service.instrument("session_gate.bidi2pdf", { limit: nil, wait: waited, acquired: true, gate: gate.class.name })
65
+ yield
66
+ end
67
+ end
68
+
69
+ # The semaphore of this process, built for the limit configured at its first setup (and in a forked child).
70
+ def current
71
+ LOCK.synchronize do
72
+ if @pid == Process.pid
73
+ warn_changed_limit
74
+ else
75
+ @limit = Bidi2pdf.session_start_concurrency
76
+ @semaphore = @limit && Concurrent::Semaphore.new(@limit)
77
+ @pid = Process.pid
78
+ end
79
+ [@semaphore, @limit]
80
+ end
81
+ end
82
+
83
+ # Once per new value: the limit in force stays until a restart.
84
+ def warn_changed_limit
85
+ configured = Bidi2pdf.session_start_concurrency
86
+ return if [@limit, @warned_limit].include?(configured)
87
+
88
+ @warned_limit = configured
89
+ Bidi2pdf.logger.warn "session_gate: session_start_concurrency changed to #{configured.inspect} after the first session " \
90
+ "setup - #{@limit.inspect} stays in force until the process restarts"
91
+ end
92
+
93
+ def refuse(limit, waited)
94
+ Bidi2pdf.notification_service.instrument("session_gate.bidi2pdf", { limit: limit, wait: waited, acquired: false })
95
+ raise SessionStartTimeoutError.new(timeout_message(limit, waited), details: { limit: limit, waited: waited })
96
+ end
97
+
98
+ def acquire(semaphore)
99
+ started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
100
+ acquired = semaphore.try_acquire(1, Bidi2pdf.session_start_timeout || Bidi2pdf.default_timeout)
101
+ [acquired, (Process.clock_gettime(Process::CLOCK_MONOTONIC) - started).round(3)]
102
+ end
103
+
104
+ def timeout_message(limit, waited)
105
+ "No Chrome session could start within #{waited}s: #{limit} other session setup(s) in this process held every permit"
106
+ end
107
+ end
108
+ end
109
+ end
@@ -56,9 +56,7 @@ module Bidi2pdf
56
56
  # rubocop: enable Metrics/ParameterLists
57
57
 
58
58
  def run
59
- @session.start
60
- @session.client.on_close { Bidi2pdf.logger.info "WebSocket closed" }
61
-
59
+ open_tab
62
60
  setup_browser
63
61
  run_flow
64
62
  end
@@ -69,9 +67,7 @@ module Bidi2pdf
69
67
  #
70
68
  # @return [Bidi2pdf::Bidi::BrowserTab] the navigated tab.
71
69
  def run_diagnose
72
- @session.start
73
- @session.client.on_close { Bidi2pdf.logger.info "WebSocket closed" }
74
-
70
+ open_tab
75
71
  setup_browser
76
72
  navigate_and_wait
77
73
 
@@ -88,18 +84,21 @@ module Bidi2pdf
88
84
 
89
85
  private
90
86
 
91
- def setup_browser
92
- browser = @session.browser
93
- user_context = browser.create_user_context
94
-
95
- window = user_context.create_browser_window
96
- tab = window.create_browser_tab
87
+ # The session's setup, up to its first tab, waits for the session start limit (Bidi2pdf::SessionGate);
88
+ # everything after it does not.
89
+ def open_tab
90
+ Bidi2pdf::SessionGate.with_permit do
91
+ @session.start
92
+ @session.client.on_close { Bidi2pdf.logger.info "WebSocket closed" }
97
93
 
98
- @window = window
99
- @tab = tab
100
- @user_context = user_context
94
+ @user_context = @session.browser.create_user_context
95
+ @window = @user_context.create_browser_window
96
+ @tab = @window.create_browser_tab
97
+ end
98
+ end
101
99
 
102
- add_cookies(tab)
100
+ def setup_browser
101
+ add_cookies(@tab)
103
102
 
104
103
  add_headers
105
104
  add_basic_auth
@@ -326,23 +326,17 @@ module Bidi2pdf
326
326
  @sweeper&.sweep!(reason: reason)
327
327
  end
328
328
 
329
- # Checks out a slot, creates an isolated UserContext/Window/Tab for one render, yields the tab,
330
- # then unconditionally closes those resources and retires the underlying slot.
329
+ # Checks out a slot, yields its tab - in a UserContext of its own, created with the slot (#new_slot) - for one
330
+ # render, then unconditionally closes the tab, window and user context and retires the slot.
331
331
  def with_tab
332
332
  slot = checkout
333
- user_context = nil
334
- window = nil
335
- tab = nil
336
333
 
337
334
  begin
338
- user_context = slot[:browser].create_user_context
339
- window = user_context.create_browser_window
340
- tab = window.create_browser_tab
341
- yield tab
335
+ yield slot[:tab]
342
336
  ensure
343
- safe_close("tab") { tab&.close }
344
- safe_close("window") { window&.close }
345
- safe_close("user context") { user_context&.close }
337
+ safe_close("tab") { slot[:tab]&.close }
338
+ safe_close("window") { slot[:window]&.close }
339
+ safe_close("user context") { slot[:user_context]&.close }
346
340
  retire(slot)
347
341
  end
348
342
  end
@@ -446,14 +440,28 @@ module Bidi2pdf
446
440
  end
447
441
 
448
442
  # A chromedriver out of room for another Chrome refuses the session; with a sweeper, close
449
- # everything nobody holds (a pressure sweep) and try once more.
443
+ # everything nobody holds (a pressure sweep) and try once more. Each attempt waits for the session
444
+ # start limit (Bidi2pdf::SessionGate), the sweep between them does not.
450
445
  def new_slot
451
- @slot_factory.call
446
+ Bidi2pdf::SessionGate.with_permit { with_first_tab(@slot_factory.call) }
452
447
  rescue Bidi2pdf::SessionNotStartedError
453
448
  raise unless @sweeper && @config.retry_refused_sessions
454
449
 
455
450
  @sweeper.sweep!(reason: :create_failed, pressure: true)
456
- @slot_factory.call
451
+ Bidi2pdf::SessionGate.with_permit { with_first_tab(@slot_factory.call) }
452
+ end
453
+
454
+ # The slot's user context, window and tab, opened together with its session under one permit: what the limit
455
+ # has to bound is the number of sessions between their request and their first tab - split into two permits,
456
+ # sessions started in between waited for their tab side by side, and chromedriver left setups unanswered
457
+ # (docs: README "Session start limit"). A slot whose tab cannot be opened is retired at once.
458
+ def with_first_tab(slot)
459
+ user_context = slot[:browser].create_user_context
460
+ window = user_context.create_browser_window
461
+ slot.merge(user_context: user_context, window: window, tab: window.create_browser_tab)
462
+ rescue StandardError
463
+ self.class.retire_slot(session: slot[:session], manager: slot[:manager])
464
+ raise
457
465
  end
458
466
 
459
467
  def start_background_work
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bidi2pdf
4
- VERSION = "0.1.24"
4
+ VERSION = "0.1.26"
5
5
  end
data/lib/bidi2pdf.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "concurrent-ruby"
4
4
  require "logger"
5
5
 
6
+ require_relative "bidi2pdf/version"
6
7
  require_relative "bidi2pdf/process_tree"
7
8
  require_relative "bidi2pdf/launcher"
8
9
  require_relative "bidi2pdf/bidi/session"
@@ -14,6 +15,7 @@ require_relative "bidi2pdf/session_registry"
14
15
  require_relative "bidi2pdf/session_sweeper"
15
16
  require_relative "bidi2pdf/chromedriver_api"
16
17
  require_relative "bidi2pdf/chrome_sweeper"
18
+ require_relative "bidi2pdf/session_gate"
17
19
  require_relative "bidi2pdf/session_warmer"
18
20
  require_relative "bidi2pdf/verbose_logger"
19
21
 
@@ -55,6 +57,14 @@ module Bidi2pdf # rubocop:disable Metrics/ModuleLength
55
57
  def hint = "Check Chrome/chromedriver are installed and reachable, or that --remote-browser-url points at a running instance"
56
58
  end
57
59
 
60
+ # No permit of the session start limit (Bidi2pdf::SessionGate) came free in time: other session setups in this
61
+ # process held them all. Reported like a session that did not start (BROWSER_LAUNCH_FAILED).
62
+ class SessionStartTimeoutError < Error
63
+ def retryable? = true
64
+
65
+ def hint = "Start fewer renders at once, or raise session_start_concurrency / session_start_timeout"
66
+ end
67
+
58
68
  class WebsocketError < Error
59
69
  def retryable? = true
60
70
  end
@@ -175,7 +185,29 @@ module Bidi2pdf # rubocop:disable Metrics/ModuleLength
175
185
 
176
186
  class << self
177
187
  attr_accessor :default_timeout, :enable_default_logging_subscriber, :log_truncate_limit, :chromedriver_log_level
178
- attr_reader :logging_subscriber, :logger, :network_events_logger, :browser_console_logger, :notification_service
188
+ attr_reader :session_start_concurrency, :session_start_timeout, :session_gate, :logging_subscriber, :logger, :network_events_logger, :browser_console_logger, :notification_service
189
+
190
+ # How many Chrome session setups may run at once in this process (Bidi2pdf::SessionGate); nil: no limit.
191
+ def session_start_concurrency=(limit)
192
+ raise InvalidConfigError, "session_start_concurrency must be nil or a positive Integer, got #{limit.inspect}" unless limit.nil? || (limit.is_a?(Integer) && limit.positive?)
193
+
194
+ @session_start_concurrency = limit
195
+ end
196
+
197
+ # An object of your own for the session start limit - anything answering +with_permit { ... }+, e.g. a lock shared
198
+ # by several processes (Bidi2pdf::SessionGate); nil: the built-in per-process semaphore.
199
+ def session_gate=(gate)
200
+ raise InvalidConfigError, "session_gate must be nil or respond to with_permit, got #{gate.inspect}" unless gate.nil? || gate.respond_to?(:with_permit)
201
+
202
+ @session_gate = gate
203
+ end
204
+
205
+ # Seconds a setup waits for a permit; nil: default_timeout.
206
+ def session_start_timeout=(seconds)
207
+ raise InvalidConfigError, "session_start_timeout must be nil or a positive number, got #{seconds.inspect}" unless seconds.nil? || (seconds.is_a?(Numeric) && seconds.positive?)
208
+
209
+ @session_start_timeout = seconds
210
+ end
179
211
 
180
212
  # Allow configuration through a block
181
213
  def configure
@@ -193,7 +225,9 @@ module Bidi2pdf # rubocop:disable Metrics/ModuleLength
193
225
  rescue LoadError => e
194
226
  raise unless e.message =~ /websocket-native/
195
227
 
196
- logger.warn "websocket-native not available; installing it may enhance performance."
228
+ # Debug, not warn: this runs while the library loads, before the CLI moves logs off stdout, and a
229
+ # missing optional speed-up is no problem to report on every run.
230
+ logger.debug "websocket-native not available; installing it may enhance performance."
197
231
  end
198
232
  end
199
233
 
@@ -268,6 +302,11 @@ module Bidi2pdf # rubocop:disable Metrics/ModuleLength
268
302
 
269
303
  config.default_timeout = 60
270
304
 
305
+ # Two setups at once: measured clean on chromedriver/Chrome 156, three were not (README "Session start limit").
306
+ config.session_start_concurrency = 2
307
+ config.session_start_timeout = nil
308
+ config.session_gate = nil
309
+
271
310
  config.log_truncate_limit = 200
272
311
 
273
312
  config.notification_service = Notifications
@@ -0,0 +1,53 @@
1
+ module Bidi2pdf
2
+ # At most Bidi2pdf.session_start_concurrency Chrome session setups at once in this process - from the session
3
+ # request to the first open tab (README "Session start limit"). When several new sessions are set up at the same
4
+ # moment, chromedriver/Chrome sometimes leaves a setup command unanswered and the session hangs until the command
5
+ # times out (measured on chromedriver/Chrome 156: three setups at once failed, two did not). Rendering is never
6
+ # limited, only the setup before it.
7
+ #
8
+ # The limit holds per process: processes sharing one Chrome (Puma workers, a job process) divide its budget
9
+ # between them. It is one budget for the process, whichever Chrome a setup goes to - two independent chromedrivers
10
+ # still take turns (an endpoint-scoped gate would not; not built). nil turns the gate off.
11
+ #
12
+ # The limit is read once per process - at its first setup, and again in a forked child, whose semaphore is its own
13
+ # so permits held by the parent's threads at the fork cannot be lost in it. A later change takes effect after a
14
+ # restart and is logged: set it at boot (an initializer), not while setups run. Changing it live would have to
15
+ # keep the setups in flight counted, which needs an API Concurrent::Semaphore does not document.
16
+ #
17
+ # Bidi2pdf.session_gate replaces the semaphore with an object of your own, to share the budget across processes
18
+ # (a file lock, a database or Redis lock). Its contract: +with_permit+ runs the block only while it holds a permit,
19
+ # gives the permit back however the block ends, raises Bidi2pdf::SessionStartTimeoutError when none comes free in
20
+ # time, and loses no permit when a process dies or forks. Every setup path still comes through here, so the
21
+ # notification below is sent either way - it trusts that the block runs under a permit. session_start_concurrency
22
+ # and session_start_timeout then apply only if that object reads them.
23
+ module SessionGate
24
+ self.@warned_limit: untyped
25
+
26
+ LOCK: untyped
27
+
28
+ # Runs the block with a permit, released however the block ends. Waits for one at most
29
+ # Bidi2pdf.session_start_timeout (default: Bidi2pdf.default_timeout) seconds, then raises
30
+ # SessionStartTimeoutError - a queue behind a hung setup fails loudly instead of growing.
31
+ def self.with_permit: () { () -> untyped } -> untyped
32
+
33
+ # Forgets the semaphore; the next permit builds one for the configured limit. For specs.
34
+ def self.reset!: () -> untyped
35
+
36
+ private
37
+
38
+ # The wait is the time until the custom gate runs the block - it reports a timeout itself, by raising.
39
+ def self.with_custom_gate: (untyped gate) { () -> untyped } -> untyped
40
+
41
+ # The semaphore of this process, built for the limit configured at its first setup (and in a forked child).
42
+ def self.current: () -> untyped
43
+
44
+ # Once per new value: the limit in force stays until a restart.
45
+ def self.warn_changed_limit: () -> (nil | untyped)
46
+
47
+ def self.refuse: (untyped limit, untyped waited) -> untyped
48
+
49
+ def self.acquire: (untyped semaphore) -> ::Array[untyped]
50
+
51
+ def self.timeout_message: (untyped limit, untyped waited) -> ::String
52
+ end
53
+ end
@@ -58,12 +58,6 @@ module Bidi2pdf
58
58
 
59
59
  @emulate_media: untyped
60
60
 
61
- @window: untyped
62
-
63
- @tab: untyped
64
-
65
- @user_context: untyped
66
-
67
61
  @uri: untyped
68
62
 
69
63
  # rubocop: disable Metrics/ParameterLists
@@ -84,6 +78,10 @@ module Bidi2pdf
84
78
 
85
79
  private
86
80
 
81
+ # The session's setup, up to its first tab, waits for the session start limit (Bidi2pdf::SessionGate);
82
+ # everything after it does not.
83
+ def open_tab: () -> untyped
84
+
87
85
  def setup_browser: () -> untyped
88
86
 
89
87
  def add_cookies: (untyped tab) -> untyped
@@ -248,8 +248,8 @@ module Bidi2pdf
248
248
  # @return [ChromeSweeper::Result, nil] nil when no sweeper is configured.
249
249
  def sweep!: (?reason: ::Symbol) -> untyped
250
250
 
251
- # Checks out a slot, creates an isolated UserContext/Window/Tab for one render, yields the tab,
252
- # then unconditionally closes those resources and retires the underlying slot.
251
+ # Checks out a slot, yields its tab - in a UserContext of its own, created with the slot (#new_slot) - for one
252
+ # render, then unconditionally closes the tab, window and user context and retires the slot.
253
253
  def with_tab: () { (untyped) -> untyped } -> untyped
254
254
 
255
255
  # Retires every currently-warm spare and waits for any in-flight background replenishment to
@@ -297,9 +297,16 @@ module Bidi2pdf
297
297
  def create_slot: () -> untyped
298
298
 
299
299
  # A chromedriver out of room for another Chrome refuses the session; with a sweeper, close
300
- # everything nobody holds (a pressure sweep) and try once more.
300
+ # everything nobody holds (a pressure sweep) and try once more. Each attempt waits for the session
301
+ # start limit (Bidi2pdf::SessionGate), the sweep between them does not.
301
302
  def new_slot: () -> untyped
302
303
 
304
+ # The slot's user context, window and tab, opened together with its session under one permit: what the limit
305
+ # has to bound is the number of sessions between their request and their first tab - split into two permits,
306
+ # sessions started in between waited for their tab side by side, and chromedriver left setups unanswered
307
+ # (docs: README "Session start limit"). A slot whose tab cannot be opened is retired at once.
308
+ def with_first_tab: (untyped slot) -> untyped
309
+
303
310
  def start_background_work: () -> untyped
304
311
 
305
312
  def build_sweeper: () -> (nil | untyped)
@@ -1,3 +1,3 @@
1
1
  module Bidi2pdf
2
- VERSION: "0.1.24"
2
+ VERSION: "0.1.26"
3
3
  end
data/sig/bidi2pdf.rbs CHANGED
@@ -1,4 +1,10 @@
1
1
  module Bidi2pdf
2
+ self.@session_start_concurrency: untyped
3
+
4
+ self.@session_gate: untyped
5
+
6
+ self.@session_start_timeout: untyped
7
+
2
8
  self.@logger: untyped
3
9
 
4
10
  self.@network_events_logger: untyped
@@ -33,6 +39,14 @@ module Bidi2pdf
33
39
  def hint: () -> "Check Chrome/chromedriver are installed and reachable, or that --remote-browser-url points at a running instance"
34
40
  end
35
41
 
42
+ # No permit of the session start limit (Bidi2pdf::SessionGate) came free in time: other session setups in this
43
+ # process held them all. Reported like a session that did not start (BROWSER_LAUNCH_FAILED).
44
+ class SessionStartTimeoutError < Error
45
+ def retryable?: () -> true
46
+
47
+ def hint: () -> "Start fewer renders at once, or raise session_start_concurrency / session_start_timeout"
48
+ end
49
+
36
50
  class WebsocketError < Error
37
51
  def retryable?: () -> true
38
52
  end
@@ -155,6 +169,12 @@ module Bidi2pdf
155
169
 
156
170
  attr_accessor self.chromedriver_log_level: untyped
157
171
 
172
+ attr_reader self.session_start_concurrency: untyped
173
+
174
+ attr_reader self.session_start_timeout: untyped
175
+
176
+ attr_reader self.session_gate: untyped
177
+
158
178
  attr_reader self.logging_subscriber: untyped
159
179
 
160
180
  attr_reader self.logger: untyped
@@ -165,6 +185,16 @@ module Bidi2pdf
165
185
 
166
186
  attr_reader self.notification_service: untyped
167
187
 
188
+ # How many Chrome session setups may run at once in this process (Bidi2pdf::SessionGate); nil: no limit.
189
+ def self.session_start_concurrency=: (untyped limit) -> untyped
190
+
191
+ # An object of your own for the session start limit - anything answering +with_permit { ... }+, e.g. a lock shared
192
+ # by several processes (Bidi2pdf::SessionGate); nil: the built-in per-process semaphore.
193
+ def self.session_gate=: (untyped gate) -> untyped
194
+
195
+ # Seconds a setup waits for a permit; nil: default_timeout.
196
+ def self.session_start_timeout=: (untyped seconds) -> untyped
197
+
168
198
  # Allow configuration through a block
169
199
  def self.configure: () ?{ (untyped) -> untyped } -> untyped
170
200
 
data/site/index.html ADDED
@@ -0,0 +1,135 @@
1
+ <!doctype html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1">
6
+ <title>bidi2pdf โ€“ HTML to PDF with Chrome's BiDi protocol, for scripts, CI and AI agents</title>
7
+ <meta name="description" content="bidi2pdf is an open-source Ruby gem and CLI that converts web pages and HTML to PDF with headless Chrome over WebDriver BiDi โ€“ for Ruby and Rails apps, CI pipelines and AI agents: JSON results, stable exit codes, page diagnosis and declarative recipes.">
8
+ <link rel="canonical" href="https://dieter-medium.github.io/bidi2pdf/">
9
+ <link rel="alternate" type="text/plain" href="llms.txt" title="llms.txt">
10
+ <link rel="icon" type="image/png" href="icon.png">
11
+ <meta property="og:type" content="website">
12
+ <meta property="og:title" content="bidi2pdf โ€“ HTML to PDF for scripts, CI and AI agents">
13
+ <meta property="og:description" content="Render URLs and HTML to PDF with headless Chrome over WebDriver BiDi: JSON output, exit codes, JSON Schemas, diagnosis and recipes.">
14
+ <meta property="og:url" content="https://dieter-medium.github.io/bidi2pdf/">
15
+ <meta property="og:image" content="https://dieter-medium.github.io/bidi2pdf/icon.png">
16
+ <script type="application/ld+json">
17
+ {
18
+ "@context": "https://schema.org",
19
+ "@type": "SoftwareSourceCode",
20
+ "name": "bidi2pdf",
21
+ "description": "A Ruby gem and CLI that renders URLs and HTML to PDF and screenshots with headless Chrome over WebDriver BiDi, with structured JSON output for scripts, CI and AI agents.",
22
+ "codeRepository": "https://github.com/dieter-medium/bidi2pdf",
23
+ "programmingLanguage": "Ruby",
24
+ "runtimePlatform": "Ruby 3.4 or later, Chrome or Chromium",
25
+ "license": "https://opensource.org/licenses/MIT",
26
+ "url": "https://dieter-medium.github.io/bidi2pdf/",
27
+ "keywords": ["HTML to PDF", "PDF generation", "headless Chrome", "WebDriver BiDi", "CLI", "Ruby", "Rails", "AI agents", "Claude Code"]
28
+ }
29
+ </script>
30
+ <style>
31
+ :root { color-scheme: light dark; --bg: #fffce6; --fg: #1b1b18; --muted: #55534a; --accent: #b23a26; --panel: #fff8d6; --line: #e7dfb5; }
32
+ @media (prefers-color-scheme: dark) {
33
+ :root { --bg: #16150f; --fg: #f3f0dc; --muted: #c4c0a8; --accent: #f08a73; --panel: #22201a; --line: #3a372c; }
34
+ }
35
+ * { box-sizing: border-box; }
36
+ body { margin: 0; background: var(--bg); color: var(--fg); font: 1.0625rem/1.6 system-ui, -apple-system, "Segoe UI", sans-serif; }
37
+ main { max-width: 46rem; margin: 0 auto; padding: 2.5rem 1rem 4rem; }
38
+ header { display: flex; align-items: center; gap: 1rem; }
39
+ header img { width: 4rem; height: 4rem; border-radius: 0.75rem; }
40
+ h1 { font-size: 2rem; line-height: 1.2; margin: 0; }
41
+ .lead { font-size: 1.15rem; color: var(--muted); }
42
+ h2 { font-size: 1.35rem; margin: 2.5rem 0 0.75rem; }
43
+ a { color: var(--accent); }
44
+ a:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
45
+ ul { padding-left: 1.25rem; }
46
+ li { margin: 0.4rem 0; }
47
+ pre { background: var(--panel); border: 1px solid var(--line); border-radius: 0.5rem; padding: 0.9rem 1rem; overflow-x: auto; font-size: 0.9rem; line-height: 1.5; }
48
+ code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
49
+ table { border-collapse: collapse; width: 100%; font-size: 0.95rem; }
50
+ th, td { text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--line); vertical-align: top; }
51
+ footer { margin-top: 3rem; color: var(--muted); font-size: 0.95rem; }
52
+ @media (max-width: 30rem) {
53
+ header { flex-direction: column; align-items: flex-start; gap: 0.75rem; }
54
+ h1 { font-size: 1.6rem; }
55
+ }
56
+ .visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
57
+ </style>
58
+ </head>
59
+ <body>
60
+ <main>
61
+ <header>
62
+ <img src="icon.png" alt="" width="64" height="64">
63
+ <h1>HTML to PDF in Ruby, with headless Chrome and WebDriver BiDi</h1>
64
+ </header>
65
+ <p class="lead"><strong>bidi2pdf</strong> is an open-source Ruby gem and command-line tool that turns web pages and HTML into PDFs. It is built for developers, CI pipelines and AI coding agents that need PDF generation they can script: structured JSON results, predictable exit codes and rendering by a real browser.</p>
66
+
67
+ <h2 id="use-cases">What it does</h2>
68
+ <ul>
69
+ <li><strong>A web page or HTML file to PDF</strong> from the command line, with waits for page load or network idle, cookies, headers and basic auth.</li>
70
+ <li><strong>HTML in, PDF out</strong>: <code>--stdin</code> and <code>--output -</code> pipe without an input or output file of your own. The PDF goes straight to stdout; the piped HTML is kept in a temporary file while Chrome loads it, then deleted.</li>
71
+ <li><strong>Automation and agents</strong>: with <code>--json</code>, stdout carries exactly one JSON document per call; failures come back in the same shape with a structured error and a stable exit code; the JSON Schema of every shape is built in.</li>
72
+ <li><strong>Why does the PDF not look like the page?</strong> <code>bidi2pdf diagnose</code> reports console errors, failed requests, font loading, print rules and Paged.js โ€“ without producing a PDF.</li>
73
+ <li><strong>Pages that need steps before printing</strong>: declarative recipes, validated without launching a browser.</li>
74
+ <li><strong>Screen, dark and mobile views</strong> of a page as PDF (<code>--media</code>, <code>--color_scheme</code>), and screenshots through the Ruby API, for review.</li>
75
+ <li><strong>A shared remote Chrome</strong> for several processes โ€“ Puma workers, job workers โ€“ with pre-warmed sessions and a sweeper for leaked ones.</li>
76
+ <li><strong>Ruby and Rails</strong>: a programmatic API and DSL, <a href="https://github.com/dieter-medium/bidi2pdf-rails">bidi2pdf-rails</a> for <code>render pdf:</code> in controllers, and RSpec matchers for PDF specs.</li>
77
+ </ul>
78
+
79
+ <h2 id="quick-start">Quick start</h2>
80
+ <pre tabindex="0"><code>gem install bidi2pdf
81
+ bidi2pdf render --url https://example.com --output example.pdf</code></pre>
82
+ <p>Requires Ruby 3.4 or later and Chrome or Chromium โ€“ or use the published Docker image.</p>
83
+
84
+ <h2 id="tasks">Common tasks</h2>
85
+ <ul>
86
+ <li><strong>Generate PDFs in Ruby or Rails</strong> โ€“ reports, invoices and other HTML templates, rendered by headless Chrome: <a href="https://github.com/dieter-medium/bidi2pdf-rails">bidi2pdf-rails</a> gives controllers <code>render pdf:</code>; plain Ruby uses the gem's API.</li>
87
+ <li><strong>Convert HTML to PDF in CI</strong> โ€“ one command, then read <code>ok</code> and the exit code instead of parsing logs:
88
+ <pre tabindex="0"><code>bidi2pdf render --html-file report.html --output report.pdf --json</code></pre></li>
89
+ <li><strong>Debug a PDF that looks wrong</strong> โ€“ missing fonts, failed requests, print CSS, JavaScript errors, without producing a PDF:
90
+ <pre tabindex="0"><code>bidi2pdf diagnose --url https://example.com/invoice --json</code></pre></li>
91
+ </ul>
92
+
93
+ <h2 id="when">When to use bidi2pdf</h2>
94
+ <p>It fits when the PDF should look like the page does in a browser โ€“ modern CSS, web fonts, content a script renders, Paged.js print layouts โ€“ because Chrome itself renders it; when PDFs are made unattended, in CI, background jobs or by an agent, and a failure has to say what went wrong; and when the code around it is Ruby or Rails, or several processes share one remote Chrome.</p>
95
+ <p>Something else fits better when the PDF is drawn in code rather than from HTML โ€“ a PDF library does that without a browser โ€“ or where neither Chrome nor Docker can run.</p>
96
+
97
+ <h2 id="agents">For agents and CI</h2>
98
+ <pre tabindex="0"><code># check compatibility - no browser launched
99
+ bidi2pdf version --json
100
+
101
+ # discover the shape of every result
102
+ bidi2pdf schema render
103
+
104
+ # validate a recipe without a browser, then run it
105
+ bidi2pdf run recipe.yml --validate
106
+ bidi2pdf run recipe.yml --json
107
+
108
+ # render with a machine-readable result
109
+ bidi2pdf render --url https://example.com --output example.pdf --json</code></pre>
110
+ <table>
111
+ <caption class="visually-hidden">Exit codes</caption>
112
+ <thead><tr><th scope="col">Exit code</th><th scope="col">Meaning</th></tr></thead>
113
+ <tbody>
114
+ <tr><td><code>0</code></td><td>success</td></tr>
115
+ <tr><td><code>2</code></td><td>CLI, configuration or recipe-validation error</td></tr>
116
+ <tr><td><code>3</code></td><td>browser or navigation error</td></tr>
117
+ <tr><td><code>4</code></td><td>page not as expected (a recipe action or assertion, or a diagnose selector, failed)</td></tr>
118
+ <tr><td><code>6</code></td><td>output or PDF generation failure</td></tr>
119
+ <tr><td><code>70</code></td><td>unexpected internal error</td></tr>
120
+ </tbody>
121
+ </table>
122
+ <p>Details: <a href="https://github.com/dieter-medium/bidi2pdf#structured-render-output">structured output</a>, <a href="https://github.com/dieter-medium/bidi2pdf#stdinstdout-manifests-and-progress-streaming">stdin/stdout and progress events</a>, <a href="https://github.com/dieter-medium/bidi2pdf#diagnosing-a-page-before-trusting-its-pdf">diagnose</a>, <a href="https://github.com/dieter-medium/bidi2pdf#declarative-recipes">recipes</a>. A plain-text summary for language models: <a href="llms.txt">llms.txt</a>.</p>
123
+
124
+ <h2 id="claude-code">Claude Code plugin</h2>
125
+ <p>Six skills teach an agent to use bidi2pdf as intended: rendering, recipes, diagnosing a wrong PDF, a shared remote Chrome, review screenshots and PDF specs.</p>
126
+ <pre tabindex="0"><code>/plugin marketplace add dieter-medium/bidi2pdf
127
+ /plugin install bidi2pdf@bidi2pdf</code></pre>
128
+ <p><a href="https://github.com/dieter-medium/bidi2pdf#claude-code-skills">About the skills</a></p>
129
+
130
+ <footer>
131
+ <p><a href="https://github.com/dieter-medium/bidi2pdf">Source on GitHub</a> ยท <a href="https://rubygems.org/gems/bidi2pdf">RubyGems</a> ยท <a href="https://github.com/dieter-medium/bidi2pdf/blob/main/CHANGELOG.md">Changelog</a> ยท MIT License</p>
132
+ </footer>
133
+ </main>
134
+ </body>
135
+ </html>
data/site/llms.txt ADDED
@@ -0,0 +1,22 @@
1
+ # bidi2pdf
2
+
3
+ > bidi2pdf is a Ruby gem and CLI that renders a URL or HTML to PDF with headless Chrome over WebDriver BiDi. It is built to be driven by scripts, CI and AI agents: with `--json` every call writes exactly one JSON document to stdout, failures come back in the same shape with a structured error and a stable exit code, and the JSON Schema of every result is built into the CLI.
4
+
5
+ Use it to: render a web page or HTML file to PDF (`bidi2pdf render --url https://example.com --output example.pdf`); pipe HTML in and PDF out (`--stdin`, `--output -`); find out why a PDF does not look like the page (`bidi2pdf diagnose`); print pages that need steps first with declarative recipes, validated without a browser (`bidi2pdf run recipe.yml --validate`); print screen, dark or mobile views (`--media`, `--color_scheme`); share one remote Chrome between processes with pre-warmed sessions; render from Ruby, Rails (bidi2pdf-rails) and RSpec.
6
+
7
+ Start with `bidi2pdf version --json` (no browser launched), then `bidi2pdf schema render`, `bidi2pdf schema diagnose`, `bidi2pdf schema run` or `bidi2pdf schema recipe` for the shape of a result or a recipe. Exit codes: 0 success, 2 CLI/configuration/recipe-validation error, 3 browser or navigation error, 4 page not as expected, 6 output or PDF generation failure, 70 unexpected internal error. Requires Ruby 3.4 or later and Chrome or Chromium; a Docker image is published.
8
+
9
+ ## Docs
10
+
11
+ - [README](https://github.com/dieter-medium/bidi2pdf#readme): installation, CLI, library API, Docker, configuration
12
+ - [Structured render output](https://github.com/dieter-medium/bidi2pdf#structured-render-output): the JSON result and exit codes
13
+ - [stdin/stdout, manifests and progress streaming](https://github.com/dieter-medium/bidi2pdf#stdinstdout-manifests-and-progress-streaming): piping, render manifests, NDJSON progress events
14
+ - [Diagnosing a page](https://github.com/dieter-medium/bidi2pdf#diagnosing-a-page-before-trusting-its-pdf): console errors, failed requests, fonts, print rules, Paged.js
15
+ - [Declarative recipes](https://github.com/dieter-medium/bidi2pdf#declarative-recipes): waits and PDF assertions as a YAML contract
16
+ - [Claude Code skills](https://github.com/dieter-medium/bidi2pdf#claude-code-skills): the plugin with six skills and how to install it
17
+
18
+ ## Optional
19
+
20
+ - [Rails integration](https://github.com/dieter-medium/bidi2pdf-rails): `render pdf:` in controllers
21
+ - [Changelog](https://github.com/dieter-medium/bidi2pdf/blob/main/CHANGELOG.md)
22
+ - [RubyGems](https://rubygems.org/gems/bidi2pdf)
data/site/sitemap.xml ADDED
@@ -0,0 +1,5 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
3
+ <url><loc>https://dieter-medium.github.io/bidi2pdf/</loc></url>
4
+ <url><loc>https://dieter-medium.github.io/bidi2pdf/llms.txt</loc></url>
5
+ </urlset>
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bidi2pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.24
4
+ version: 0.1.26
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dieter S.
@@ -391,6 +391,7 @@ executables:
391
391
  extensions: []
392
392
  extra_rdoc_files: []
393
393
  files:
394
+ - ".agents/plugins/marketplace.json"
394
395
  - ".devcontainer/Dockerfile"
395
396
  - ".devcontainer/devcontainer.json"
396
397
  - ".devcontainer/docker-compose.yml"
@@ -496,6 +497,7 @@ files:
496
497
  - lib/bidi2pdf/result.rb
497
498
  - lib/bidi2pdf/result_collector.rb
498
499
  - lib/bidi2pdf/schema.rb
500
+ - lib/bidi2pdf/session_gate.rb
499
501
  - lib/bidi2pdf/session_registry.rb
500
502
  - lib/bidi2pdf/session_registry/heartbeat.rb
501
503
  - lib/bidi2pdf/session_runner.rb
@@ -604,6 +606,7 @@ files:
604
606
  - sig/bidi2pdf/result.rbs
605
607
  - sig/bidi2pdf/result_collector.rbs
606
608
  - sig/bidi2pdf/schema.rbs
609
+ - sig/bidi2pdf/session_gate.rbs
607
610
  - sig/bidi2pdf/session_registry.rbs
608
611
  - sig/bidi2pdf/session_registry/heartbeat.rbs
609
612
  - sig/bidi2pdf/session_runner.rbs
@@ -632,6 +635,9 @@ files:
632
635
  - sig/bidi2pdf/verbose_logger.rbs
633
636
  - sig/bidi2pdf/version.rbs
634
637
  - sig/vendor/thor.rbs
638
+ - site/index.html
639
+ - site/llms.txt
640
+ - site/sitemap.xml
635
641
  - tasks/changelog.rake
636
642
  - tasks/coverage.rake
637
643
  - tasks/generate_rbs.rake