Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 7 additions & 5 deletions lib/ferrum_pdf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,16 +32,18 @@ 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
page.network.authorize(**authorize) { |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
ensure
page.close
end
rescue Ferrum::DeadBrowserError
retry
Expand Down
Loading