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 +4 -4
- data/README.md +31 -8
- data/lib/ferrum_pdf/version.rb +1 -1
- data/lib/ferrum_pdf.rb +42 -23
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 286b43b8607b0e0b3763aa9b16b3f6152abf88ae4ce213df5eccdbed399be730
|
4
|
+
data.tar.gz: 7090ac955effd15d1e350140195ba6c979d88fc7286f306021d54fbe95639a12
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
###
|
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
|
-
#
|
219
|
-
|
220
|
-
FerrumPdf.
|
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
|
-
|
223
|
-
FerrumPdf.render_pdf(url: "https://example.org"
|
231
|
+
# All subsequent calls will use this browser
|
232
|
+
FerrumPdf.render_pdf(url: "https://example.org")
|
224
233
|
```
|
225
234
|
|
226
|
-
|
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
|
-
|
251
|
+
other_browser = Ferrum::Browser.new
|
252
|
+
FerrumPdf.render_pdf(url: "https://example.org", browser: other_browser)
|
230
253
|
```
|
231
254
|
|
232
255
|
## Debugging
|
data/lib/ferrum_pdf/version.rb
CHANGED
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
|
-
|
29
|
-
|
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:
|
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
|
-
|
72
|
-
|
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
|
-
|
75
|
-
|
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
|
-
|
78
|
-
|
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
|
-
|
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
|
-
|
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
|
+
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.
|
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: []
|