ferrum_pdf 3.1.0 → 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: '09655ccad2a5a6cbf6fa1d2d2e4bb34443a0bf29501265571207079b7a689482'
4
- data.tar.gz: 06f33b09c785e64e1c99dd68ef98ae2d7ea28b9460350d5d6d34e54efce3e1d0
3
+ metadata.gz: 768ac72c382db28aed8f4d0957053bfe432bbdcf6d418f98a6a2940c6b0998cc
4
+ data.tar.gz: 23902eae3d0173d8db1a165f796d0e9a8437ebf037c623ad5b504d776a523343
5
5
  SHA512:
6
- metadata.gz: '09d27b9a48ef1c126b473930e300b8ae62339404a1a7593910eae32a679106c0a9b5479d8d66ec101ca6ce7a62741665a7d350527f6990600e12181b38544c35'
7
- data.tar.gz: e0d5644936b52536b70c39c96d42e7a73f4548096797bab50d076fca3dd4068fad3fa7b03c54249d87f01c3c3ca5faa03d85ba28643fff1111f38fb8dfe4a355
6
+ metadata.gz: 31fa1ab95de7c58f4fb4b2c1afb35b2f8d982302656321c8916429260dc3a2e6d11aa128700e4ea52d602a7c0b15da8a7be263058a23893852b58b395c5f94b1
7
+ data.tar.gz: 1197dc9b1332b9e3eed2b0a9f5d1536ba426c1eaab2978594b4b97f5311a41521515dc0adf98be18d3f4c471ed25a15fa8785ad848a84c9773754f63002837da
data/README.md CHANGED
@@ -70,12 +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
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.
79
80
 
80
81
  pdf_options: {
81
82
  landscape: false, # paper orientation
@@ -108,7 +109,7 @@ FerrumPdf.render_pdf(
108
109
  )
109
110
  ```
110
111
 
111
- 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.
112
113
 
113
114
  ### 🎆 Screenshots
114
115
 
@@ -150,7 +151,7 @@ def show
150
151
  end
151
152
  ```
152
153
 
153
- 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.
154
155
 
155
156
  #### Render Screenshots
156
157
 
@@ -168,7 +169,7 @@ FerrumPdf.render_screenshot(
168
169
  url: "https://example.com/page", # Provide a URL to the content
169
170
 
170
171
  html: content, # or provide HTML
171
- 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.
172
173
 
173
174
  screenshot_options: {
174
175
  format: "png" # or "jpeg"
@@ -192,6 +193,8 @@ FerrumPdf.configure do |config|
192
193
  config.page_options.wait_for_idle_options = { timeout: 90 }
193
194
  config.page_options.retries = 3
194
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"
195
198
 
196
199
  config.pdf_options.margin_top = 0.2
197
200
  config.pdf_options.margin_bottom = 0.2
@@ -207,7 +210,7 @@ end
207
210
 
208
211
  You can set the default browser options with the configure block.
209
212
 
210
- 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.
211
214
 
212
215
  ```ruby
213
216
  FerrumPdf.configure do |config|
@@ -1,3 +1,3 @@
1
1
  module FerrumPdf
2
- VERSION = "3.1.0"
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,15 +89,31 @@ 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, viewport: 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?
96
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?
97
117
 
98
118
  with_browser(browser) do |browser|
99
119
  # Closes page automatically after block finishes
@@ -102,6 +122,9 @@ module FerrumPdf
102
122
  page.network.authorize(**authorize) { |req| req.continue } if authorize
103
123
  page.set_viewport(**viewport) if viewport
104
124
 
125
+ # Has to happen before #go_to, otherwise inline scripts have already run.
126
+ page.disable_javascript unless javascript_enabled
127
+
105
128
  # Load content
106
129
  if html
107
130
  html_intercepted = false
@@ -114,7 +137,7 @@ module FerrumPdf
114
137
  request.respond(body: html.blank? ? " " : html)
115
138
  end
116
139
  end
117
- page.go_to(display_url || "http://example.com")
140
+ page.go_to(display_url || DEFAULT_DISPLAY_URL)
118
141
  else
119
142
  page.go_to(url)
120
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.1.0
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: 4.0.2
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: []