From fca572a3c0258de2ea395b6e1b3f05dfc3d27553 Mon Sep 17 00:00:00 2001 From: Ron Shinall <81988008+ron-shinall@users.noreply.github.com> Date: Sat, 15 Feb 2025 19:40:41 +0000 Subject: [PATCH 1/3] Adding network.wait_for_idle options --- README.md | 3 ++- lib/ferrum_pdf.rb | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 7912122..8f5371d 100644 --- a/README.md +++ b/README.md @@ -71,7 +71,8 @@ FerrumPdf.render_pdf( url: "https://example.com", # or provide a URL to the content host: request.base_url + "/", # Used for setting the host for relative paths protocol: request.protocol, # Used for handling relative protocol paths - authorize: {user: "username", password: "password"}, # Used for authenticating with basic auth + authorize: { user: "username", password: "password" }, # Used for authenticating with basic auth + wait_for_idle_options: { connections: 0, duration: 0.05, timeout: 5 }, # Used for setting network wait_for_idle options pdf_options: { landscape: false, # paper orientation diff --git a/lib/ferrum_pdf.rb b/lib/ferrum_pdf.rb index 85409cf..70330e9 100644 --- a/lib/ferrum_pdf.rb +++ b/lib/ferrum_pdf.rb @@ -20,8 +20,8 @@ def browser(**options) @browser ||= Ferrum::Browser.new(options) end - def render_pdf(html: nil, url: nil, host: nil, protocol: nil, authorize: nil, pdf_options: {}) - render(host: host, protocol: protocol, html: html, url: url, authorize: authorize) do |page| + def render_pdf(html: nil, url: nil, host: nil, protocol: nil, authorize: nil, wait_for_idle_options: nil, pdf_options: {}) + render(host: host, protocol: protocol, html: html, url: url, authorize: authorize, wait_for_idle_options: wait_for_idle_options) do |page| page.pdf(**pdf_options.with_defaults(encoding: :binary)) end end @@ -32,15 +32,15 @@ def render_screenshot(html: nil, url: nil, host: nil, protocol: nil, authorize: end end - def render(host:, protocol:, html: nil, url: nil, authorize: nil) + def render(host:, protocol:, html: nil, url: nil, authorize: nil, wait_for_idle_options: nil) browser.create_page do |page| page.network.authorize(user: authorize[:user], password: authorize[:password]) { |req| req.continue } if authorize if html page.content = FerrumPdf::HTMLPreprocessor.process(html, host, protocol) - page.network.wait_for_idle else page.go_to(url) end + page.network.wait_for_idle(**wait_for_idle_options) yield page end rescue Ferrum::DeadBrowserError From 2a43d8c7d4149c13a10e674a6c4c6f5f1c0e132b Mon Sep 17 00:00:00 2001 From: Ron Shinall <81988008+ron-shinall@users.noreply.github.com> Date: Sat, 15 Feb 2025 19:41:05 +0000 Subject: [PATCH 2/3] Simplify authorize hash --- lib/ferrum_pdf.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ferrum_pdf.rb b/lib/ferrum_pdf.rb index 70330e9..73602c8 100644 --- a/lib/ferrum_pdf.rb +++ b/lib/ferrum_pdf.rb @@ -34,7 +34,7 @@ def render_screenshot(html: nil, url: nil, host: nil, protocol: nil, authorize: def render(host:, protocol:, html: nil, url: nil, authorize: nil, wait_for_idle_options: nil) browser.create_page do |page| - page.network.authorize(user: authorize[:user], password: authorize[:password]) { |req| req.continue } if authorize + page.network.authorize(**authorize) { |req| req.continue } if authorize if html page.content = FerrumPdf::HTMLPreprocessor.process(html, host, protocol) else From 4fb0d61ab6b04e91d1d8b16e3cbbf9c78eb9c02d Mon Sep 17 00:00:00 2001 From: Ron Shinall <81988008+ron-shinall@users.noreply.github.com> Date: Sat, 15 Feb 2025 21:44:44 +0000 Subject: [PATCH 3/3] Add page.close --- lib/ferrum_pdf.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/ferrum_pdf.rb b/lib/ferrum_pdf.rb index 73602c8..825f567 100644 --- a/lib/ferrum_pdf.rb +++ b/lib/ferrum_pdf.rb @@ -42,6 +42,8 @@ def render(host:, protocol:, html: nil, url: nil, authorize: nil, wait_for_idle_ end page.network.wait_for_idle(**wait_for_idle_options) yield page + ensure + page.close end rescue Ferrum::DeadBrowserError retry