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 +4 -4
- data/README.md +9 -6
- data/lib/ferrum_pdf/version.rb +1 -1
- data/lib/ferrum_pdf.rb +26 -3
- 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: 768ac72c382db28aed8f4d0957053bfe432bbdcf6d418f98a6a2940c6b0998cc
|
|
4
|
+
data.tar.gz: 23902eae3d0173d8db1a165f796d0e9a8437ebf037c623ad5b504d776a523343
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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, #
|
|
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
|
|
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://
|
|
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, #
|
|
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://
|
|
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|
|
data/lib/ferrum_pdf/version.rb
CHANGED
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
|
|
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 ||
|
|
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.
|
|
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.
|
|
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: []
|