ferrum_pdf 2.0.0 → 2.1.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 +32 -9
- data/lib/ferrum_pdf/version.rb +1 -1
- data/lib/ferrum_pdf.rb +15 -9
- metadata +2 -3
- data/lib/tasks/ferrum_pdf_tasks.rake +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e10a66bff6f06cfd7388c12247ca55e92a79bc48ff741e889e0ac0bfde0286e0
|
4
|
+
data.tar.gz: 2a92a79c5a7ca34091644fcd5795d1d1c84d49f7d834f02afd6d9ec462ba892b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c6d4472cb10faebb29ab64634f16063c7aac7927baf76e3e58dc72478d3d21819935138e6595a872f9e0766ff51a17303cad51a1857d62110b2e13fd4877234f
|
7
|
+
data.tar.gz: 9915cadbed613d26c113a57d5a23268ec3758ef9f566b5f6f387469ed4cce948d5348173a840cf9d36a44d935fda1a3e5f7cb06af5c5f042d73f2fb06707a892
|
data/README.md
CHANGED
@@ -4,6 +4,8 @@ PDFs & screentshots for Rails using [Ferrum](https://github.com/rubycdp/ferrum)
|
|
4
4
|
|
5
5
|
Inspired by [Grover](https://github.com/Studiosity/grover), but without the Node.js and puppeteer dependencies. 🎉
|
6
6
|
|
7
|
+
<img src="ferrum_pdf.png" alt="logo" style="width:450px;"/>
|
8
|
+
|
7
9
|
## Installation
|
8
10
|
|
9
11
|
First, make sure Chrome is installed. Then run the following or add the gem to your Gemfile:
|
@@ -39,8 +41,8 @@ end
|
|
39
41
|
You can also customize which template is rendered. This will render the template to string with `render_to_string` in Rails, then pass it along to Chrome. For example, you can add headers and footers using `pdf_options` and use a specific layout:
|
40
42
|
|
41
43
|
```ruby
|
42
|
-
|
43
|
-
|
44
|
+
def show
|
45
|
+
render ferrum_pdf: {
|
44
46
|
display_header_footer: true,
|
45
47
|
header_template: FerrumPdf::DEFAULT_HEADER_TEMPLATE,
|
46
48
|
footer_template: FerrumPdf::DEFAULT_FOOTER_TEMPLATE
|
@@ -49,7 +51,7 @@ render ferrum_pdf: {
|
|
49
51
|
template: "pdf",
|
50
52
|
disposition: :inline,
|
51
53
|
filename: "example.pdf"
|
52
|
-
|
54
|
+
end
|
53
55
|
```
|
54
56
|
|
55
57
|
#### Render PDFs
|
@@ -129,8 +131,8 @@ end
|
|
129
131
|
You can also customize which template is rendered. This will render the template to string with `render_to_string` in Rails, then pass it along to Chrome.
|
130
132
|
|
131
133
|
```ruby
|
132
|
-
|
133
|
-
|
134
|
+
def show
|
135
|
+
render ferrum_screenshot: {
|
134
136
|
format: "png" # or "jpeg"
|
135
137
|
quality: nil # Integer 0-100 works for jpeg only
|
136
138
|
full: true # Boolean whether you need full page screenshot or a viewport
|
@@ -140,10 +142,10 @@ render ferrum_screenshot: {
|
|
140
142
|
background_color: nil # Ferrum::RGBA.new(0, 0, 0, 0.0)
|
141
143
|
},
|
142
144
|
layout: "example",
|
143
|
-
template: "example"
|
145
|
+
template: "example",
|
144
146
|
disposition: :inline,
|
145
147
|
filename: "example.png"
|
146
|
-
|
148
|
+
end
|
147
149
|
```
|
148
150
|
|
149
151
|
See [Ferrum screenshot docs](https://github.com/rubycdp/ferrum?tab=readme-ov-file#screenshotoptions--string--integer) for the full set of options.
|
@@ -178,7 +180,28 @@ FerrumPdf.render_screenshot(
|
|
178
180
|
)
|
179
181
|
```
|
180
182
|
|
181
|
-
##
|
183
|
+
## Configuration
|
184
|
+
|
185
|
+
You can set default values for page loads, PDF renders, and screenshot renders with the configure block.
|
186
|
+
|
187
|
+
```ruby
|
188
|
+
FerrumPdf.configure do |config|
|
189
|
+
config.page_options.base_url = "https://example.com/"
|
190
|
+
config.page_options.authorize = { user: "username", password: "password" }
|
191
|
+
config.page_options.wait_for_idle_options = { timeout: 90 }
|
192
|
+
config.page_options.retries = 3
|
193
|
+
|
194
|
+
config.pdf_options.margin_top = 0.2
|
195
|
+
config.pdf_options.margin_bottom = 0.2
|
196
|
+
config.pdf_options.margin_left = 0.2
|
197
|
+
config.pdf_options.margin_right = 0.2
|
198
|
+
|
199
|
+
config.screenshot_options.format = :png
|
200
|
+
config.screenshot_options.full = false
|
201
|
+
end
|
202
|
+
```
|
203
|
+
|
204
|
+
### Configuring the Browser
|
182
205
|
|
183
206
|
You can set the default browser options with the configure block.
|
184
207
|
|
@@ -211,7 +234,7 @@ RUN apt-get update && apt-get install gnupg wget -y && \
|
|
211
234
|
rm -rf /var/lib/apt/lists/*
|
212
235
|
```
|
213
236
|
|
214
|
-
|
237
|
+
#### Browser Management
|
215
238
|
|
216
239
|
FerrumPdf uses a single browser instance per Ruby process that is automatically created when needed using your configuration settings:
|
217
240
|
|
data/lib/ferrum_pdf/version.rb
CHANGED
data/lib/ferrum_pdf.rb
CHANGED
@@ -14,7 +14,10 @@ module FerrumPdf
|
|
14
14
|
|
15
15
|
mattr_accessor :browser_mutex, default: Mutex.new
|
16
16
|
mattr_accessor :config, default: ActiveSupport::OrderedOptions.new.merge(
|
17
|
-
window_size: [ 1920, 1080 ]
|
17
|
+
window_size: [ 1920, 1080 ],
|
18
|
+
page_options: ActiveSupport::OrderedOptions.new,
|
19
|
+
pdf_options: ActiveSupport::OrderedOptions.new,
|
20
|
+
screenshot_options: ActiveSupport::OrderedOptions.new
|
18
21
|
)
|
19
22
|
|
20
23
|
# This doesn't use mattr_accessor because having a `.browser` getter and also
|
@@ -42,7 +45,7 @@ module FerrumPdf
|
|
42
45
|
yield browser
|
43
46
|
else
|
44
47
|
browser_mutex.synchronize do
|
45
|
-
@@browser ||= Ferrum::Browser.new(config)
|
48
|
+
@@browser ||= Ferrum::Browser.new(config.except(:page_options, :pdf_options, :screenshot_options))
|
46
49
|
@@browser.restart unless @@browser.client.present?
|
47
50
|
yield @@browser
|
48
51
|
end
|
@@ -61,7 +64,7 @@ module FerrumPdf
|
|
61
64
|
def render_pdf(pdf_options: {}, **load_page_args)
|
62
65
|
load_page(**load_page_args) do |browser, page|
|
63
66
|
yield browser, page if block_given?
|
64
|
-
page.pdf(**pdf_options.with_defaults(encoding: :binary))
|
67
|
+
page.pdf(**pdf_options.with_defaults(encoding: :binary, **config.pdf_options))
|
65
68
|
end
|
66
69
|
end
|
67
70
|
|
@@ -77,7 +80,7 @@ module FerrumPdf
|
|
77
80
|
def render_screenshot(screenshot_options: {}, **load_page_args)
|
78
81
|
load_page(**load_page_args) do |browser, page|
|
79
82
|
yield browser, page if block_given?
|
80
|
-
page.screenshot(**screenshot_options.with_defaults(encoding: :binary, full: true))
|
83
|
+
page.screenshot(**screenshot_options.with_defaults(encoding: :binary, full: true, **config.screenshot_options))
|
81
84
|
end
|
82
85
|
end
|
83
86
|
|
@@ -85,9 +88,12 @@ module FerrumPdf
|
|
85
88
|
#
|
86
89
|
# This automatically applies HTML preprocessing if `html:` is present
|
87
90
|
#
|
88
|
-
def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: nil, retries:
|
89
|
-
try
|
90
|
-
|
91
|
+
def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: nil, retries: nil)
|
92
|
+
try ||= 0
|
93
|
+
authorize ||= config.dig(:page_options, :authorize)
|
94
|
+
base_url ||= config.dig(:page_options, :base_url)
|
95
|
+
retries ||= config.page_options.fetch(:retries, 1)
|
96
|
+
wait_for_idle_options = config.page_options.fetch(:wait_for_idle_options, {}).merge(wait_for_idle_options || {})
|
91
97
|
|
92
98
|
with_browser(browser) do |browser|
|
93
99
|
# Closes page automatically after block finishes
|
@@ -103,12 +109,12 @@ module FerrumPdf
|
|
103
109
|
end
|
104
110
|
|
105
111
|
# Wait for everything to load
|
106
|
-
page.network.wait_for_idle(**wait_for_idle_options)
|
112
|
+
page.network.wait_for_idle!(**wait_for_idle_options)
|
107
113
|
|
108
114
|
yield browser, page
|
109
115
|
end
|
110
116
|
end
|
111
|
-
rescue Ferrum::DeadBrowserError
|
117
|
+
rescue Ferrum::DeadBrowserError, Ferrum::TimeoutError
|
112
118
|
try += 1
|
113
119
|
if try <= retries
|
114
120
|
with_browser(&:restart)
|
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: 2.
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
@@ -53,7 +53,6 @@ files:
|
|
53
53
|
- lib/ferrum_pdf/html_preprocessor.rb
|
54
54
|
- lib/ferrum_pdf/railtie.rb
|
55
55
|
- lib/ferrum_pdf/version.rb
|
56
|
-
- lib/tasks/ferrum_pdf_tasks.rake
|
57
56
|
homepage: https://github.com/excid3/ferrum_pdf
|
58
57
|
licenses:
|
59
58
|
- MIT
|
@@ -75,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
75
74
|
- !ruby/object:Gem::Version
|
76
75
|
version: '0'
|
77
76
|
requirements: []
|
78
|
-
rubygems_version: 3.7.
|
77
|
+
rubygems_version: 3.7.1
|
79
78
|
specification_version: 4
|
80
79
|
summary: PDFs & screenshots for Rails using Ferrum & headless Chrome
|
81
80
|
test_files: []
|