ferrum_pdf 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -12
- data/lib/ferrum_pdf/version.rb +1 -1
- data/lib/ferrum_pdf.rb +17 -13
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f7537d25768b2cf12df0322c63ad643c5a56dfbce85d812db0cab2f46e5f280a
|
4
|
+
data.tar.gz: b36728c8686eacf59543f756ff2c3daddc20a4d9eb806580dd87faa732471cf0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8f1b5d33362700a1ed42ff99068b968dc696fdb6f573b5c59f1f7be0207dc891cad8dd13994d8881fcb8a49ed774ed7aa7312445ccc15955f243dea30a8b66ce
|
7
|
+
data.tar.gz: 702c650ee2f84c7eee9562cd989cb938dac2e7ca3dfe2d0e800b9315882e6309817743c639e1f8b9b47ecb2d775a1cb023a33cc68f4ef84f526aa62247358e3d
|
data/README.md
CHANGED
@@ -1,28 +1,73 @@
|
|
1
1
|
# FerrumPdf
|
2
|
-
Short description and motivation.
|
3
2
|
|
4
|
-
|
5
|
-
How to use my plugin.
|
3
|
+
PDFs for Rails using [Ferrum](https://github.com/rubycdp/ferrum) & headless Chrome
|
6
4
|
|
7
5
|
## Installation
|
8
|
-
|
6
|
+
|
7
|
+
Run the following or add the gem to your Gemfile:
|
9
8
|
|
10
9
|
```ruby
|
11
|
-
|
10
|
+
bundle add "ferrum_pdf"
|
12
11
|
```
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
### Rails controllers
|
16
|
+
|
17
|
+
Use the `render_pdf` helper in Rails controllers to render a PDF from the current action.
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
def show
|
21
|
+
respond_to do |format|
|
22
|
+
format.html
|
23
|
+
format.pdf { send_data render_pdf, disposition: :inline, filename: "example.pdf" }
|
24
|
+
end
|
25
|
+
end
|
17
26
|
```
|
18
27
|
|
19
|
-
|
20
|
-
|
21
|
-
|
28
|
+
You can also customize which template is rendered:
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
render_pdf(name = action_name, formats: [ :html ], pdf_options: {})
|
22
32
|
```
|
23
33
|
|
34
|
+
This will render the template to string with `render_to_string` in Rails, then pass it along to FerrumPdf.
|
35
|
+
|
36
|
+
For example, you can add headers and footers using `pdf_options`
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
render_pdf(pdf_options: {
|
40
|
+
display_header_footer: true,
|
41
|
+
header_template: FerrumPdf::DEFAULT_HEADER_TEMPLATE,
|
42
|
+
footer_template: FerrumPdf::DEFAULT_FOOTER_TEMPLATE
|
43
|
+
})
|
44
|
+
```
|
45
|
+
|
46
|
+
### Directly with HTML
|
47
|
+
|
48
|
+
FerrumPdf can generate a PDF from HTML directly:
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
FerrumPdf.render_pdf(html: content)
|
52
|
+
```
|
53
|
+
|
54
|
+
You can also pass host and protocol to convert any relative paths to full URLs. This is helpful for converting relative asset paths to full URLs.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
FerrumPdf.render_pdf(
|
58
|
+
html: content,
|
59
|
+
host: request.host_with_port,
|
60
|
+
protocol: request.protocol,
|
61
|
+
pdf_options: {}
|
62
|
+
)
|
63
|
+
```
|
64
|
+
|
65
|
+
See Chrome DevTools Protocol docs: https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
|
66
|
+
And Ferrum's `#pdf` docs: https://github.com/rubycdp/ferrum?tab=readme-ov-file#pdfoptions--string--boolean
|
67
|
+
|
24
68
|
## Contributing
|
25
|
-
|
69
|
+
|
70
|
+
If you have an issue you'd like to submit, please do so using the issue tracker in GitHub. In order for us to help you in the best way possible, please be as detailed as you can.
|
26
71
|
|
27
72
|
## License
|
28
73
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/ferrum_pdf/version.rb
CHANGED
data/lib/ferrum_pdf.rb
CHANGED
@@ -4,22 +4,25 @@ require "ferrum_pdf/railtie"
|
|
4
4
|
require "ferrum"
|
5
5
|
|
6
6
|
module FerrumPdf
|
7
|
+
DEFAULT_HEADER_TEMPLATE = "<div class='date text left'></div><div class='title text center'></div>"
|
8
|
+
DEFAULT_FOOTER_TEMPLATE = <<~HTML
|
9
|
+
<div class='url text left grow'></div>
|
10
|
+
<div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
|
11
|
+
HTML
|
12
|
+
|
7
13
|
def self.browser(**options)
|
8
14
|
@browser ||= Ferrum::Browser.new(options)
|
9
15
|
end
|
10
16
|
|
11
|
-
def self.render_pdf(host:, protocol:, html: nil, url: nil)
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
page.go_to(url)
|
19
|
-
end
|
20
|
-
page.pdf(path: file.path)
|
21
|
-
file.read
|
17
|
+
def self.render_pdf(host:, protocol:, html: nil, url: nil, pdf_options: {})
|
18
|
+
browser.create_page do |page|
|
19
|
+
if html
|
20
|
+
page.content = FerrumPdf::HTMLPreprocessor.process(html, host, protocol)
|
21
|
+
page.network.wait_for_idle
|
22
|
+
else
|
23
|
+
page.go_to(url)
|
22
24
|
end
|
25
|
+
page.pdf(**pdf_options.with_defaults(encoding: :binary))
|
23
26
|
end
|
24
27
|
end
|
25
28
|
|
@@ -51,13 +54,14 @@ module FerrumPdf
|
|
51
54
|
module Controller
|
52
55
|
extend ActiveSupport::Concern
|
53
56
|
|
54
|
-
def render_pdf(name = action_name, formats: [ :html ])
|
57
|
+
def render_pdf(name = action_name, formats: [ :html ], pdf_options: {})
|
55
58
|
content = render_to_string(name, formats: formats)
|
56
59
|
|
57
60
|
FerrumPdf.render_pdf(
|
58
61
|
html: content,
|
59
62
|
host: request.host_with_port,
|
60
|
-
protocol: request.protocol
|
63
|
+
protocol: request.protocol,
|
64
|
+
pdf_options: pdf_options
|
61
65
|
)
|
62
66
|
end
|
63
67
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ferrum_pdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Oliver
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-09-
|
11
|
+
date: 2024-09-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 6.0.0
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 6.0.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: ferrum
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -74,7 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
requirements: []
|
77
|
-
rubygems_version: 3.5.
|
77
|
+
rubygems_version: 3.5.16
|
78
78
|
signing_key:
|
79
79
|
specification_version: 4
|
80
80
|
summary: PDFs for Rails using Ferrum & headless Chrome
|