ferrum_pdf 3.0.1 → 3.2.0

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: efe120091d2cc3eaaf6ce3e7fc5ccb3c34d3fabe59570986fa5b08d89295b58a
4
- data.tar.gz: b3923cb2306e1cd27023725ddb088ae9f6ed290aab538db1e0d27bdb032d79cd
3
+ metadata.gz: 768ac72c382db28aed8f4d0957053bfe432bbdcf6d418f98a6a2940c6b0998cc
4
+ data.tar.gz: 23902eae3d0173d8db1a165f796d0e9a8437ebf037c623ad5b504d776a523343
5
5
  SHA512:
6
- metadata.gz: 4e44f85006fc98e28298c2e3720b3066bc59611c32b439791df54d5b70e4dae5eaa62fabe1499d75f7f85599ff79e52ffee60485556f3b1167a1082aeb381702
7
- data.tar.gz: 86ad9105c1a8e2bf3c867a91e0a409d045d5ffdbc6755bdfa5af02a7948271c32ab063a138f3ff09903daa3f100642ef8f328aea154cb4e316add75944d07680
6
+ metadata.gz: 31fa1ab95de7c58f4fb4b2c1afb35b2f8d982302656321c8916429260dc3a2e6d11aa128700e4ea52d602a7c0b15da8a7be263058a23893852b58b395c5f94b1
7
+ data.tar.gz: 1197dc9b1332b9e3eed2b0a9f5d1536ba426c1eaab2978594b4b97f5311a41521515dc0adf98be18d3f4c471ed25a15fa8785ad848a84c9773754f63002837da
data/README.md CHANGED
@@ -70,11 +70,13 @@ FerrumPdf.render_pdf(
70
70
  url: "https://example.com/page", # Provide a URL to the content
71
71
 
72
72
  html: content, # or provide HTML
73
- display_url: request.original_url, # When supplying content via :html its best to give Chrome a hint of the current url so that it can process relative paths in the document. If you don't provide this, http://example.com will be used instead.
73
+ display_url: request.original_url, # Base URL for resolving relative paths in :html. Defaults to FerrumPdf.config.page_options.display_url.
74
74
 
75
75
  authorize: { user: "username", password: "password" }, # Used for authenticating with basic auth
76
76
  wait_for_idle_options: { connections: 0, duration: 0.05, timeout: 5 }, # Used for setting network wait_for_idle options
77
77
  timeout_if_open_connections: true,
78
+ viewport: { width: 1200, height: 800, scale_factor: 3 }, # Used for setting the viewport dimensions and device scale factor (DPR) when rendering the page
79
+ javascript_enabled: true, # Set to false to prevent scripts in the page from running. Note: this does not block network requests (images, stylesheets, iframes) or meta refresh redirects.
78
80
 
79
81
  pdf_options: {
80
82
  landscape: false, # paper orientation
@@ -107,7 +109,7 @@ FerrumPdf.render_pdf(
107
109
  )
108
110
  ```
109
111
 
110
- See [Chrome DevTools Protocol docs](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF) and [Ferrum's `#pdf` docs](https://github.com/rubycdp/ferrum?tab=readme-ov-file#pdfoptions--string--boolean) for the full set of options.
112
+ See [Chrome DevTools Protocol docs](https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF) and [Ferrum's pdf docs](https://docs.rubycdp.com/docs/ferrum/screenshots/#pdfoptions--string--boolean) for the full set of options.
111
113
 
112
114
  ### 🎆 Screenshots
113
115
 
@@ -149,7 +151,7 @@ def show
149
151
  end
150
152
  ```
151
153
 
152
- See [Ferrum screenshot docs](https://github.com/rubycdp/ferrum?tab=readme-ov-file#screenshotoptions--string--integer) for the full set of options.
154
+ See [Ferrum's screenshot docs](https://docs.rubycdp.com/docs/ferrum/screenshots/#screenshotoptions--string--integer) for the full set of options.
153
155
 
154
156
  #### Render Screenshots
155
157
 
@@ -167,7 +169,7 @@ FerrumPdf.render_screenshot(
167
169
  url: "https://example.com/page", # Provide a URL to the content
168
170
 
169
171
  html: content, # or provide HTML
170
- display_url: request.original_url, # When supplying content via :html its best to give Chrome a hint of the current url so that it can process relative paths in the document. If you don't provide this, http://example.com will be used instead.
172
+ display_url: request.original_url, # Base URL for resolving relative paths in :html. Defaults to FerrumPdf.config.page_options.display_url.
171
173
 
172
174
  screenshot_options: {
173
175
  format: "png" # or "jpeg"
@@ -190,6 +192,9 @@ FerrumPdf.configure do |config|
190
192
  config.page_options.authorize = { user: "username", password: "password" }
191
193
  config.page_options.wait_for_idle_options = { timeout: 90 }
192
194
  config.page_options.retries = 3
195
+ config.page_options.viewport = { width: 1200, height: 800, scale_factor: 3 }
196
+ config.page_options.javascript_enabled = false
197
+ config.page_options.display_url = "https://example.org"
193
198
 
194
199
  config.pdf_options.margin_top = 0.2
195
200
  config.pdf_options.margin_bottom = 0.2
@@ -205,7 +210,7 @@ end
205
210
 
206
211
  You can set the default browser options with the configure block.
207
212
 
208
- See [Ferrum's Customization docs](https://github.com/rubycdp/ferrum?tab=readme-ov-file#customization) for a full list of options.
213
+ See [Ferrum's Customization docs](https://docs.rubycdp.com/docs/ferrum/customization) for a full list of options.
209
214
 
210
215
  ```ruby
211
216
  FerrumPdf.configure do |config|
@@ -1,3 +1,3 @@
1
1
  module FerrumPdf
2
- VERSION = "3.0.1"
2
+ VERSION = "3.2.0"
3
3
  end
data/lib/ferrum_pdf.rb CHANGED
@@ -5,6 +5,9 @@ require "ferrum_pdf/renderers"
5
5
  require "ferrum_pdf/version"
6
6
 
7
7
  module FerrumPdf
8
+ # Reserved .invalid TLD so relative paths fail fast on DNS instead of hanging
9
+ # against a domain we don't control.
10
+ DEFAULT_DISPLAY_URL = "http://ferrum-pdf.invalid"
8
11
  DEFAULT_HEADER_TEMPLATE = "<div class='date text left'></div><div class='title text center'></div>"
9
12
  DEFAULT_FOOTER_TEMPLATE = <<~HTML
10
13
  <div class='url text left grow'></div>
@@ -44,8 +47,9 @@ module FerrumPdf
44
47
  yield browser
45
48
  else
46
49
  browser_mutex.synchronize do
50
+ discard_unusable_browser
47
51
  @@browser ||= Ferrum::Browser.new(config.except(:page_options, :pdf_options, :screenshot_options))
48
- @@browser.restart unless @@browser.client.present?
52
+ @@browser.restart if @@browser.client.nil?
49
53
  yield @@browser
50
54
  end
51
55
  end
@@ -85,20 +89,41 @@ module FerrumPdf
85
89
 
86
90
  private
87
91
 
92
+ # A browser whose start died between Client.new and Contexts.new keeps a client but no
93
+ # contexts, and #restart cannot repair it: quit dereferences the nil contexts. Drop it and
94
+ # release the process and socket it still holds, so the next call builds a fresh browser.
95
+ def discard_unusable_browser
96
+ return if @@browser.nil? || @@browser.client.nil? || !@@browser.contexts.nil?
97
+
98
+ begin
99
+ @@browser.process&.stop
100
+ @@browser.client&.close
101
+ ensure
102
+ @@browser = nil
103
+ end
104
+ end
105
+
88
106
  # Loads page into the browser to be used for rendering PDFs or screenshots
89
107
  #
90
- def load_page(url: nil, html: nil, display_url: nil, authorize: nil, wait_for_idle_options: nil, timeout_if_open_connections: nil, browser: nil, retries: nil)
108
+ def load_page(url: nil, html: nil, display_url: nil, authorize: nil, wait_for_idle_options: nil, timeout_if_open_connections: nil, browser: nil, retries: nil, viewport: nil, javascript_enabled: nil)
91
109
  try ||= 0
92
110
  authorize ||= config.dig(:page_options, :authorize)
93
111
  retries ||= config.page_options.fetch(:retries, 1)
94
112
  wait_for_idle_options = config.page_options.fetch(:wait_for_idle_options, {}).merge(wait_for_idle_options || {})
95
113
  timeout_if_open_connections = config.page_options.fetch(:timeout_if_open_connections, true) if timeout_if_open_connections.nil?
114
+ viewport ||= config.dig(:page_options, :viewport)
115
+ display_url ||= config.dig(:page_options, :display_url)
116
+ javascript_enabled = config.page_options.fetch(:javascript_enabled, true) if javascript_enabled.nil?
96
117
 
97
118
  with_browser(browser) do |browser|
98
119
  # Closes page automatically after block finishes
99
120
  # https://github.com/rubycdp/ferrum/blob/main/lib/ferrum/browser.rb#L169
100
121
  browser.create_page do |page|
101
122
  page.network.authorize(**authorize) { |req| req.continue } if authorize
123
+ page.set_viewport(**viewport) if viewport
124
+
125
+ # Has to happen before #go_to, otherwise inline scripts have already run.
126
+ page.disable_javascript unless javascript_enabled
102
127
 
103
128
  # Load content
104
129
  if html
@@ -112,7 +137,7 @@ module FerrumPdf
112
137
  request.respond(body: html.blank? ? " " : html)
113
138
  end
114
139
  end
115
- page.go_to(display_url || "http://example.com")
140
+ page.go_to(display_url || DEFAULT_DISPLAY_URL)
116
141
  else
117
142
  page.go_to(url)
118
143
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferrum_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -72,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
72
72
  - !ruby/object:Gem::Version
73
73
  version: '0'
74
74
  requirements: []
75
- rubygems_version: 3.6.9
75
+ rubygems_version: 4.0.18
76
76
  specification_version: 4
77
77
  summary: PDFs & screenshots for Rails using Ferrum & headless Chrome
78
78
  test_files: []