ferrum_pdf 0.4.1 → 0.5.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: 32142f653d2f6e9d0b5f1f5ec7f86d897c2f2dd1e3107299809a5561b540d604
4
- data.tar.gz: a7b306cc4817869043b1f9785d7ddf8b5eb1d40e957bc9091315ca3d1983851f
3
+ metadata.gz: 286b43b8607b0e0b3763aa9b16b3f6152abf88ae4ce213df5eccdbed399be730
4
+ data.tar.gz: 7090ac955effd15d1e350140195ba6c979d88fc7286f306021d54fbe95639a12
5
5
  SHA512:
6
- metadata.gz: afec9dc5c17c618ac9fb6108f445310c6680d98b8329ff1f31d3967a62a8f1fa7ac3329cb852b2c04df285884f8c12598b64f38681d148d53216993891093944
7
- data.tar.gz: df9c2ce1b26fd9f9b077621d80d865e1cd3b32b041fa28cbf10d573ad8731e72634d4e755c273fecf1fb98c1b48efbf50b3e4cfdbad0005576b5b6c4f829884c
6
+ metadata.gz: f7dbeee04fdb949642d331c88f22bd49ad6317a9e64f63b4239b0612deeea4278bd3f74850c1b8b50601f47b5fbb9ec60d0c6a9e4306d0e3cba13b4d0ac7d004
7
+ data.tar.gz: 1999f985c696e8c684792d169df268ac099ea28af0890a6b0868ec17ab8a8328f418f5fa6c13fd4b767bab887d9a9646839d1bbe390236d80ddc79336588df51
data/README.md CHANGED
@@ -212,21 +212,44 @@ RUN apt-get update && apt-get install gnupg wget -y && \
212
212
  rm -rf /var/lib/apt/lists/*
213
213
  ```
214
214
 
215
- ### Multiple Browser Support
215
+ ### Browser Management
216
+
217
+ FerrumPdf uses a single browser instance per Ruby process that is automatically created when needed using your configuration settings:
218
+
219
+ ```ruby
220
+ # Browser is auto-created on first use
221
+ FerrumPdf.render_pdf(url: "https://example.org")
222
+ ```
223
+
224
+ You can also set a custom browser instance:
216
225
 
217
226
  ```ruby
218
- # Create two browsers using the FerrumPdf config, but overriding `window_size`
219
- FerrumPdf.add_browser(:small, window_size: [1024, 768]))
220
- FerrumPdf.add_browser(:large, window_size: [1920, 1080]))
227
+ # Set a custom browser with specific options
228
+ custom_browser = Ferrum::Browser.new(window_size: [800, 600], headless: false)
229
+ FerrumPdf.browser = custom_browser
221
230
 
222
- FerrumPdf.render_pdf(url: "https://example.org", browser: :small)
223
- FerrumPdf.render_pdf(url: "https://example.org", browser: :large)
231
+ # All subsequent calls will use this browser
232
+ FerrumPdf.render_pdf(url: "https://example.org")
224
233
  ```
225
234
 
226
- You can also create a `Ferrum::Browser` instance and pass it in as `browser`:
235
+ To safely shut down the browser process:
236
+
237
+ ```ruby
238
+ # This will quit the current browser and set it to nil
239
+ FerrumPdf.browser = nil
240
+
241
+ # Next call will auto-create a new browser
242
+ FerrumPdf.render_pdf(url: "https://example.org")
243
+ ```
244
+
245
+ > [!TIP]
246
+ > **Thread Safety**: FerrumPdf is thread-safe within a single Ruby process. Multiple threads can safely use FerrumPdf concurrently, and they will share the same Chrome browser instance. However, each Ruby worker process will have its own separate Chrome instance.
247
+
248
+ If you need multiple browsers, you can pass the browser option when rendering:
227
249
 
228
250
  ```ruby
229
- FerrumPdf.render_pdf(url: "https://example.org", browser: Ferrum::Browser.new)
251
+ other_browser = Ferrum::Browser.new
252
+ FerrumPdf.render_pdf(url: "https://example.org", browser: other_browser)
230
253
  ```
231
254
 
232
255
  ## Debugging
@@ -1,3 +1,3 @@
1
1
  module FerrumPdf
2
- VERSION = "0.4.1"
2
+ VERSION = "0.5.0"
3
3
  end
data/lib/ferrum_pdf.rb CHANGED
@@ -13,20 +13,43 @@ module FerrumPdf
13
13
  autoload :Controller, "ferrum_pdf/controller"
14
14
  autoload :HTMLPreprocessor, "ferrum_pdf/html_preprocessor"
15
15
 
16
+ mattr_accessor :browser_mutex, default: Mutex.new
16
17
  mattr_accessor :include_assets_helper_module, default: true
17
18
  mattr_accessor :include_controller_module, default: true
18
- mattr_accessor :browsers, default: {}
19
19
  mattr_accessor :config, default: ActiveSupport::OrderedOptions.new.merge(
20
20
  window_size: [ 1920, 1080 ]
21
21
  )
22
22
 
23
+ # This doesn't use mattr_accessor because having a `.browser` getter and also
24
+ # having local variables named `browser` would be confusing. For simplicity,
25
+ # this is explicitly accessed.
26
+ @@browser = nil
27
+
23
28
  class << self
24
29
  def configure
25
30
  yield config
26
31
  end
27
32
 
28
- def add_browser(name, **options)
29
- @@browsers[name] = Ferrum::Browser.new(@@config.merge(options))
33
+ # Sets the browser instance to use for all operations
34
+ # If a browser is already set, it will be shut down before setting the new one
35
+ def browser=(browser_instance)
36
+ browser_mutex.synchronize do
37
+ @@browser&.quit
38
+ @@browser = browser_instance
39
+ end
40
+ end
41
+
42
+ # Provides thread-safe access to the browser instance
43
+ def with_browser(browser=nil)
44
+ if browser
45
+ yield browser
46
+ else
47
+ browser_mutex.synchronize do
48
+ @@browser ||= Ferrum::Browser.new(config)
49
+ @@browser.restart unless @@browser.client.present?
50
+ yield @@browser
51
+ end
52
+ end
30
53
  end
31
54
 
32
55
  # Renders HTML or URL to PDF
@@ -65,36 +88,32 @@ module FerrumPdf
65
88
  #
66
89
  # This automatically applies HTML preprocessing if `html:` is present
67
90
  #
68
- def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: :default, retries: 1)
91
+ def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: nil, retries: 1)
69
92
  try = 0
70
93
 
71
- # Lookup browser if a name was passed
72
- browser = @@browsers[browser] || add_browser(browser) if browser.is_a? Symbol
94
+ with_browser(browser) do |browser|
95
+ # Closes page automatically after block finishes
96
+ # https://github.com/rubycdp/ferrum/blob/main/lib/ferrum/browser.rb#L169
97
+ browser.create_page do |page|
98
+ page.network.authorize(**authorize) { |req| req.continue } if authorize
73
99
 
74
- # Automatically restart the browser if it was disconnected
75
- browser.restart unless browser.client.present?
100
+ # Load content
101
+ if html
102
+ page.content = FerrumPdf::HTMLPreprocessor.process(html, base_url)
103
+ else
104
+ page.go_to(url)
105
+ end
76
106
 
77
- # Closes page automatically after block finishes
78
- # https://github.com/rubycdp/ferrum/blob/main/lib/ferrum/browser.rb#L169
79
- browser.create_page do |page|
80
- page.network.authorize(**authorize) { |req| req.continue } if authorize
107
+ # Wait for everything to load
108
+ page.network.wait_for_idle(**wait_for_idle_options)
81
109
 
82
- # Load content
83
- if html
84
- page.content = FerrumPdf::HTMLPreprocessor.process(html, base_url)
85
- else
86
- page.go_to(url)
110
+ yield browser, page
87
111
  end
88
-
89
- # Wait for everything to load
90
- page.network.wait_for_idle(**wait_for_idle_options)
91
-
92
- yield browser, page
93
112
  end
94
113
  rescue Ferrum::DeadBrowserError
95
114
  try += 1
96
115
  if try <= retries
97
- browser.restart
116
+ with_browser(&:restart)
98
117
  retry
99
118
  else
100
119
  raise
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: 0.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubygems_version: 3.6.9
79
+ rubygems_version: 3.7.0
80
80
  specification_version: 4
81
81
  summary: PDFs & screenshots for Rails using Ferrum & headless Chrome
82
82
  test_files: []