ferrum_pdf 0.5.0 → 2.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 +4 -4
- data/README.md +21 -22
- data/lib/ferrum_pdf/assets_helper.rb +35 -35
- data/lib/ferrum_pdf/railtie.rb +18 -2
- data/lib/ferrum_pdf/version.rb +1 -1
- data/lib/ferrum_pdf.rb +2 -4
- metadata +1 -2
- data/lib/ferrum_pdf/controller.rb +0 -27
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9ad1aec4cf5dc3fd72ee568cc2e963d14be0d1d7934efc082dba0ec92368fd16
|
4
|
+
data.tar.gz: 80a623342478906fc8b2fff9ac7067218387271ac4416c3492f8a1d759c7ec0d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3ffa22c676e622ee3361ef927c87f3ff91f73e74465f69c63252406cdd27714356fadd66c960b5731ede0262e7af7f4c1649704328768d4e89baab2c6031d508
|
7
|
+
data.tar.gz: e7e1f120c6739e88927ee62dba5974ef480ca715e846835b1d307a16f577667733e53cc6a9afb84cf172616f319a134aeb26a290fb1a6790d1cef4555d2fb293
|
data/README.md
CHANGED
@@ -6,9 +6,7 @@ Inspired by [Grover](https://github.com/Studiosity/grover), but without the Node
|
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
9
|
-
First, make sure Chrome is installed.
|
10
|
-
|
11
|
-
Run the following or add the gem to your Gemfile:
|
9
|
+
First, make sure Chrome is installed. Then run the following or add the gem to your Gemfile:
|
12
10
|
|
13
11
|
```ruby
|
14
12
|
bundle add "ferrum_pdf"
|
@@ -22,21 +20,18 @@ You can use FerrumPdf to render [PDFs](#-pdfs) and [Screenshots](#-screenshots)
|
|
22
20
|
|
23
21
|
There are two ways to render PDFs:
|
24
22
|
|
23
|
+
* [`render ferrum_pdf: {}` in Rails](#render-pdfs-from-rails-controllers)
|
25
24
|
* [FerrumPdf.render_pdf](#render-pdfs)
|
26
|
-
* [render_pdf in Rails](#render-pdfs-from-rails-controllers)
|
27
25
|
|
28
26
|
#### Render PDFs from Rails controllers
|
29
27
|
|
30
|
-
Use the `
|
28
|
+
Use the `ferrum_pdf` renderer in Rails controllers to render a PDF from the current action.
|
31
29
|
|
32
30
|
```ruby
|
33
31
|
def show
|
34
32
|
respond_to do |format|
|
35
33
|
format.html
|
36
|
-
format.pdf {
|
37
|
-
pdf = render_pdf()
|
38
|
-
send_data pdf, disposition: :inline, filename: "example.pdf"
|
39
|
-
}
|
34
|
+
format.pdf { render ferrum_pdf: {}, disposition: :inline, filename: "example.pdf" }
|
40
35
|
end
|
41
36
|
end
|
42
37
|
```
|
@@ -44,14 +39,17 @@ end
|
|
44
39
|
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:
|
45
40
|
|
46
41
|
```ruby
|
47
|
-
|
48
|
-
layout: "pdf,
|
42
|
+
render ferrum_pdf: {
|
49
43
|
pdf_options: {
|
50
44
|
display_header_footer: true,
|
51
45
|
header_template: FerrumPdf::DEFAULT_HEADER_TEMPLATE,
|
52
46
|
footer_template: FerrumPdf::DEFAULT_FOOTER_TEMPLATE
|
53
|
-
}
|
54
|
-
|
47
|
+
},
|
48
|
+
layout: "pdf",
|
49
|
+
template: "pdf",
|
50
|
+
disposition: :inline,
|
51
|
+
filename: "example.pdf"
|
52
|
+
}
|
55
53
|
```
|
56
54
|
|
57
55
|
#### Render PDFs
|
@@ -112,21 +110,18 @@ See [Chrome DevTools Protocol docs](https://chromedevtools.github.io/devtools-pr
|
|
112
110
|
|
113
111
|
There are two ways to render Screenshots:
|
114
112
|
|
113
|
+
* [`render ferrum_screenshot: {}` in Rails](#render-screenshots-from-rails-controllers)
|
115
114
|
* [FerrumPdf.render_screenshot](#render-screenshots)
|
116
|
-
* [render_screenshot in Rails](#render-screenshots-from-rails-controllers)
|
117
115
|
|
118
116
|
#### Render Screenshots from Rails controllers
|
119
117
|
|
120
|
-
Use the `
|
118
|
+
Use the `ferrum_screenshot` renderer in Rails controllers to render a PDF from the current action.
|
121
119
|
|
122
120
|
```ruby
|
123
121
|
def show
|
124
122
|
respond_to do |format|
|
125
123
|
format.html
|
126
|
-
format.png {
|
127
|
-
screenshot = render_screenshot()
|
128
|
-
send_data screenshot, disposition: :inline, filename: "example.png"
|
129
|
-
}
|
124
|
+
format.png { render ferrum_screenshot: {}, disposition: :inline, filename: "example.png" }
|
130
125
|
end
|
131
126
|
end
|
132
127
|
```
|
@@ -134,7 +129,7 @@ end
|
|
134
129
|
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.
|
135
130
|
|
136
131
|
```ruby
|
137
|
-
|
132
|
+
render ferrum_screenshot: {
|
138
133
|
screenshot_options: {
|
139
134
|
format: "png" # or "jpeg"
|
140
135
|
quality: nil # Integer 0-100 works for jpeg only
|
@@ -143,8 +138,12 @@ render_screenshot(
|
|
143
138
|
area: nil # Hash area for screenshot, optional. {x: 0, y: 0, width: 100, height: 100}
|
144
139
|
scale: nil # Float zoom in/out
|
145
140
|
background_color: nil # Ferrum::RGBA.new(0, 0, 0, 0.0)
|
146
|
-
}
|
147
|
-
|
141
|
+
},
|
142
|
+
layout: "example",
|
143
|
+
template: "example"
|
144
|
+
disposition: :inline,
|
145
|
+
filename: "example.png"
|
146
|
+
}
|
148
147
|
```
|
149
148
|
|
150
149
|
See [Ferrum screenshot docs](https://github.com/rubycdp/ferrum?tab=readme-ov-file#screenshotoptions--string--integer) for the full set of options.
|
@@ -1,53 +1,53 @@
|
|
1
1
|
module FerrumPdf
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
@asset = asset
|
6
|
-
end
|
2
|
+
class BaseAsset
|
3
|
+
def initialize(asset)
|
4
|
+
@asset = asset
|
7
5
|
end
|
6
|
+
end
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
8
|
+
class PropshaftAsset < BaseAsset
|
9
|
+
def content_type
|
10
|
+
@asset.content_type.to_s
|
11
|
+
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
end
|
13
|
+
def content
|
14
|
+
@asset.content
|
17
15
|
end
|
16
|
+
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
class SprocketsAsset < BaseAsset
|
19
|
+
def content_type
|
20
|
+
@asset.content_type
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
end
|
23
|
+
def content
|
24
|
+
@asset.source
|
27
25
|
end
|
26
|
+
end
|
28
27
|
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
end
|
28
|
+
class AssetFinder
|
29
|
+
class << self
|
30
|
+
def find(path)
|
31
|
+
if Rails.application.assets.respond_to?(:load_path)
|
32
|
+
propshaft_asset(path)
|
33
|
+
elsif Rails.application.assets.respond_to?(:find_asset)
|
34
|
+
sprockets_asset(path)
|
35
|
+
else
|
36
|
+
nil
|
39
37
|
end
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
def propshaft_asset(path)
|
41
|
+
(asset = Rails.application.assets.load_path.find(path)) ? PropshaftAsset.new(asset) : nil
|
42
|
+
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
end
|
44
|
+
def sprockets_asset(path)
|
45
|
+
(asset = Rails.application.assets.find_asset(path)) ? SprocketsAsset.new(asset) : nil
|
48
46
|
end
|
49
47
|
end
|
48
|
+
end
|
50
49
|
|
50
|
+
module AssetsHelper
|
51
51
|
def ferrum_pdf_inline_stylesheet(path)
|
52
52
|
(asset = AssetFinder.find(path)) ? "<style>#{asset.content}</style>".html_safe : nil
|
53
53
|
end
|
data/lib/ferrum_pdf/railtie.rb
CHANGED
@@ -2,13 +2,29 @@ module FerrumPdf
|
|
2
2
|
class Railtie < ::Rails::Railtie
|
3
3
|
initializer "ferrum_pdf.assets_helper" do
|
4
4
|
ActiveSupport.on_load(:action_view) do
|
5
|
-
include FerrumPdf::AssetsHelper
|
5
|
+
include FerrumPdf::AssetsHelper
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
initializer "ferrum_pdf.controller" do
|
10
10
|
ActiveSupport.on_load(:action_controller) do
|
11
|
-
|
11
|
+
# render ferrum_pdf: { pdf options }, template: "whatever", disposition: :inline, filename: "example.pdf"
|
12
|
+
ActionController.add_renderer :ferrum_pdf do |pdf_options, options|
|
13
|
+
send_data_options = options.extract!(:disposition, :filename, :status)
|
14
|
+
url = pdf_options.delete(:url)
|
15
|
+
html = render_to_string(**options.with_defaults(formats: [ :html ])) if url.blank?
|
16
|
+
pdf = FerrumPdf.render_pdf(html: html, base_url: request.base_url, url: url, pdf_options: pdf_options)
|
17
|
+
send_data(pdf, **send_data_options.with_defaults(type: :pdf))
|
18
|
+
end
|
19
|
+
|
20
|
+
# render ferrum_screenshot: { pdf options }, template: "whatever", disposition: :inline, filename: "example.png"
|
21
|
+
ActionController.add_renderer :ferrum_screenshot do |screenshot_options, options|
|
22
|
+
send_data_options = options.extract!(:disposition, :filename, :status)
|
23
|
+
url = screenshot_options.delete(:url)
|
24
|
+
html = render_to_string(**options.with_defaults(formats: [ :html ])) if url.blank?
|
25
|
+
screenshot = FerrumPdf.render_screenshot(url: url, html: html, base_url: request.base_url, screenshot_options: screenshot_options)
|
26
|
+
send_data(screenshot, **send_data_options.with_defaults(type: screenshot_options.fetch(:format, :png)))
|
27
|
+
end
|
12
28
|
end
|
13
29
|
end
|
14
30
|
end
|
data/lib/ferrum_pdf/version.rb
CHANGED
data/lib/ferrum_pdf.rb
CHANGED
@@ -10,12 +10,9 @@ module FerrumPdf
|
|
10
10
|
HTML
|
11
11
|
|
12
12
|
autoload :AssetsHelper, "ferrum_pdf/assets_helper"
|
13
|
-
autoload :Controller, "ferrum_pdf/controller"
|
14
13
|
autoload :HTMLPreprocessor, "ferrum_pdf/html_preprocessor"
|
15
14
|
|
16
15
|
mattr_accessor :browser_mutex, default: Mutex.new
|
17
|
-
mattr_accessor :include_assets_helper_module, default: true
|
18
|
-
mattr_accessor :include_controller_module, default: true
|
19
16
|
mattr_accessor :config, default: ActiveSupport::OrderedOptions.new.merge(
|
20
17
|
window_size: [ 1920, 1080 ]
|
21
18
|
)
|
@@ -40,7 +37,7 @@ module FerrumPdf
|
|
40
37
|
end
|
41
38
|
|
42
39
|
# Provides thread-safe access to the browser instance
|
43
|
-
def with_browser(browser=nil)
|
40
|
+
def with_browser(browser = nil)
|
44
41
|
if browser
|
45
42
|
yield browser
|
46
43
|
else
|
@@ -90,6 +87,7 @@ module FerrumPdf
|
|
90
87
|
#
|
91
88
|
def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: nil, retries: 1)
|
92
89
|
try = 0
|
90
|
+
wait_for_idle_options ||= {}
|
93
91
|
|
94
92
|
with_browser(browser) do |browser|
|
95
93
|
# Closes page automatically after block finishes
|
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: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
@@ -50,7 +50,6 @@ files:
|
|
50
50
|
- Rakefile
|
51
51
|
- lib/ferrum_pdf.rb
|
52
52
|
- lib/ferrum_pdf/assets_helper.rb
|
53
|
-
- lib/ferrum_pdf/controller.rb
|
54
53
|
- lib/ferrum_pdf/html_preprocessor.rb
|
55
54
|
- lib/ferrum_pdf/railtie.rb
|
56
55
|
- lib/ferrum_pdf/version.rb
|
@@ -1,27 +0,0 @@
|
|
1
|
-
module FerrumPdf
|
2
|
-
module Controller
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
def render_pdf(pdf_options: {}, **rendering, &block)
|
6
|
-
content = render_to_string(**rendering.with_defaults(formats: [ :html ]))
|
7
|
-
|
8
|
-
FerrumPdf.render_pdf(
|
9
|
-
html: content,
|
10
|
-
base_url: request.base_url,
|
11
|
-
pdf_options: pdf_options,
|
12
|
-
&block
|
13
|
-
)
|
14
|
-
end
|
15
|
-
|
16
|
-
def render_screenshot(screenshot_options: {}, **rendering, &block)
|
17
|
-
content = render_to_string(**rendering.with_defaults(formats: [ :html ]))
|
18
|
-
|
19
|
-
FerrumPdf.render_screenshot(
|
20
|
-
html: content,
|
21
|
-
base_url: request.base_url,
|
22
|
-
screenshot_options: screenshot_options,
|
23
|
-
&block
|
24
|
-
)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|