ferrum_pdf 0.4.2 → 1.0.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: 1bb953e452513432ae962fdf430ca58dfda05c95ef099a3a081c280aad8b7013
4
- data.tar.gz: 8eea8b0b4b4fdcd2f24c36f7da512742c1d84efca70585cefbc23207e3287a68
3
+ metadata.gz: a1356ce4acb8121982cca17f42a53f88595cdf6004101d60e4192d912fb6a699
4
+ data.tar.gz: 12cfdc58b24168b8a45f6239d668ba49f4443ea928b51554189dd565d34c15d8
5
5
  SHA512:
6
- metadata.gz: b1159b7493ae224c5c2eede63d2d0e4b30b51fb33929572ed1315bc5ca59aa2f39c1778a8a12ddc8df7d7d85e0038961250c0e9073300988055e5d0be032ddfd
7
- data.tar.gz: 93823c9ad5df71ffae73925c09af0bed905ec64a6192d249d9d733075e0e92bc7126aede0b952a2293dc708123a6d93c0e9c5f5399954a3bd9baf447815bdc5b
6
+ metadata.gz: e9078a7f52c54d9aed0b6b2840a136853c306a9f4414f92cc3c6ca5649e68b31ead2798ab9a8ec91848709222e67be188bebc570d72e150e56a1d697e19acedd
7
+ data.tar.gz: f7947db380dcc18275a9df3a437240d4affe52a4809d7cabdb50a46a34cbf22b93ea67c7278e3cf6a41bf75a7556cf4481f32a5c6e9bc7fd5333a149b52e70d0
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.2"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/ferrum_pdf.rb CHANGED
@@ -13,23 +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
- # Creates and registers a new browser for exports
29
- # If a browser with the same name already exists, it will be shut down before instantiating the new one
30
- def add_browser(name, **options)
31
- @@browsers[name].quit if @@browsers[name]
32
- @@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
33
53
  end
34
54
 
35
55
  # Renders HTML or URL to PDF
@@ -68,36 +88,32 @@ module FerrumPdf
68
88
  #
69
89
  # This automatically applies HTML preprocessing if `html:` is present
70
90
  #
71
- 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)
72
92
  try = 0
73
93
 
74
- # Lookup browser if a name was passed
75
- 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
76
99
 
77
- # Automatically restart the browser if it was disconnected
78
- 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
79
106
 
80
- # Closes page automatically after block finishes
81
- # https://github.com/rubycdp/ferrum/blob/main/lib/ferrum/browser.rb#L169
82
- browser.create_page do |page|
83
- 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)
84
109
 
85
- # Load content
86
- if html
87
- page.content = FerrumPdf::HTMLPreprocessor.process(html, base_url)
88
- else
89
- page.go_to(url)
110
+ yield browser, page
90
111
  end
91
-
92
- # Wait for everything to load
93
- page.network.wait_for_idle(**wait_for_idle_options)
94
-
95
- yield browser, page
96
112
  end
97
113
  rescue Ferrum::DeadBrowserError
98
114
  try += 1
99
115
  if try <= retries
100
- browser.restart
116
+ with_browser(&:restart)
101
117
  retry
102
118
  else
103
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.2
4
+ version: 1.0.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: []