gotenberg-rails 0.1.0 → 0.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 +30 -0
- data/lib/gotenberg/rails/client.rb +19 -6
- data/lib/gotenberg/rails/renderer.rb +4 -2
- data/lib/gotenberg/rails/version.rb +1 -1
- data/lib/gotenberg/rails.rb +2 -2
- 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: c625732d01298352cc92f7a6a60fa76fc0b51e3e2b5601f4c35221969d40f211
|
|
4
|
+
data.tar.gz: f38ad53bde1bb0ff8c2b588a1ae5af2909ee374e36c567f081a93bfd917c7087
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 973908367941e234bf99c30cfe2b94a0fcbc9b6756563e9da2a8aff9969b48e707939e7e53f47f2c3d947501a3abcfe721bbb5dc5576f87a2c7ec61dbe6e2920
|
|
7
|
+
data.tar.gz: 32518987a77ecda2954d6b43fe58cecc6d16d7b806862ac3300892d76db2bcc57a283ecc1a555e69ffd304b8b6dce64dffa1d604921e9648a58ab9bd8d0ff666
|
data/README.md
CHANGED
|
@@ -59,6 +59,36 @@ Gotenberg::Rails.render_pdf(html: html, display_url: "https://example.com/invoic
|
|
|
59
59
|
Gotenberg::Rails.render_pdf(url: "https://example.com/invoice")
|
|
60
60
|
```
|
|
61
61
|
|
|
62
|
+
Add a header or footer to every page by passing complete HTML documents:
|
|
63
|
+
|
|
64
|
+
```ruby
|
|
65
|
+
Gotenberg::Rails.render_pdf(
|
|
66
|
+
html: html,
|
|
67
|
+
header_html: "<html><body>Invoice</body></html>",
|
|
68
|
+
footer_html: '<html><body>Page <span class="pageNumber"></span> of <span class="totalPages"></span></body></html>',
|
|
69
|
+
pdf_options: {
|
|
70
|
+
margin_top: "1in",
|
|
71
|
+
margin_bottom: "1in"
|
|
72
|
+
}
|
|
73
|
+
)
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
From a controller, Rails templates can be rendered separately for the header and footer:
|
|
77
|
+
|
|
78
|
+
```ruby
|
|
79
|
+
header_html = render_to_string(template: "invoices/header", layout: false)
|
|
80
|
+
footer_html = render_to_string(template: "invoices/footer", layout: false)
|
|
81
|
+
|
|
82
|
+
render gotenberg_pdf: {
|
|
83
|
+
header_html: header_html,
|
|
84
|
+
footer_html: footer_html,
|
|
85
|
+
margin_top: "1in",
|
|
86
|
+
margin_bottom: "1in"
|
|
87
|
+
}, template: "invoices/show", layout: "pdf"
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
Header and footer templates must each be complete HTML documents. They are rendered separately from the main page, so JavaScript and external stylesheets or images are unavailable. Embed images as Base64 data URLs and leave enough top and bottom margin to avoid clipping.
|
|
91
|
+
|
|
62
92
|
When rendering HTML, `display_url` is used to rewrite relative image, link, JavaScript, stylesheet, and CSS `url(...)` references to absolute URLs before sending the HTML to Gotenberg. Controller rendering uses `request.original_url` automatically.
|
|
63
93
|
|
|
64
94
|
Options are sent to Gotenberg as Chromium form fields. Ruby-style snake case keys are converted to Gotenberg camel case keys:
|
|
@@ -20,13 +20,13 @@ module Gotenberg
|
|
|
20
20
|
@headers = headers
|
|
21
21
|
end
|
|
22
22
|
|
|
23
|
-
def render_pdf(html: nil, url: nil, pdf_options: {}, filename: nil, trace: nil)
|
|
23
|
+
def render_pdf(html: nil, url: nil, header_html: nil, footer_html: nil, pdf_options: {}, filename: nil, trace: nil)
|
|
24
24
|
if html && url
|
|
25
25
|
raise ArgumentError, "Provide either :html or :url, not both"
|
|
26
26
|
elsif html
|
|
27
|
-
post(HTML_PATH, html_form(html, pdf_options), filename:, trace:)
|
|
27
|
+
post(HTML_PATH, html_form(html, header_html, footer_html, pdf_options), filename:, trace:)
|
|
28
28
|
elsif url
|
|
29
|
-
post(URL_PATH, url_form(url, pdf_options), filename:, trace:)
|
|
29
|
+
post(URL_PATH, url_form(url, header_html, footer_html, pdf_options), filename:, trace:)
|
|
30
30
|
else
|
|
31
31
|
raise ArgumentError, "Provide :html or :url"
|
|
32
32
|
end
|
|
@@ -34,20 +34,33 @@ module Gotenberg
|
|
|
34
34
|
|
|
35
35
|
private
|
|
36
36
|
|
|
37
|
-
def html_form(html, pdf_options)
|
|
37
|
+
def html_form(html, header_html, footer_html, pdf_options)
|
|
38
38
|
[
|
|
39
|
-
|
|
39
|
+
html_file(html, "index.html"),
|
|
40
|
+
*header_footer_files(header_html, footer_html),
|
|
40
41
|
*option_fields(pdf_options)
|
|
41
42
|
]
|
|
42
43
|
end
|
|
43
44
|
|
|
44
|
-
def url_form(url, pdf_options)
|
|
45
|
+
def url_form(url, header_html, footer_html, pdf_options)
|
|
45
46
|
[
|
|
46
47
|
["url", url.to_s],
|
|
48
|
+
*header_footer_files(header_html, footer_html),
|
|
47
49
|
*option_fields(pdf_options)
|
|
48
50
|
]
|
|
49
51
|
end
|
|
50
52
|
|
|
53
|
+
def header_footer_files(header_html, footer_html)
|
|
54
|
+
{
|
|
55
|
+
"header.html" => header_html,
|
|
56
|
+
"footer.html" => footer_html
|
|
57
|
+
}.compact.map { |filename, content| html_file(content, filename) }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def html_file(content, filename)
|
|
61
|
+
["files", StringIO.new(content.to_s), { filename:, content_type: "text/html" }]
|
|
62
|
+
end
|
|
63
|
+
|
|
51
64
|
def option_fields(options)
|
|
52
65
|
options.compact.map do |key, value|
|
|
53
66
|
[camelize_option(key), encode_option(value)]
|
|
@@ -5,13 +5,15 @@ module Gotenberg
|
|
|
5
5
|
module Renderer
|
|
6
6
|
def self.register
|
|
7
7
|
ActionController::Renderers.add :gotenberg_pdf do |options, render_options|
|
|
8
|
-
pdf_options = options || {}
|
|
8
|
+
pdf_options = (options || {}).dup
|
|
9
9
|
filename = render_options[:filename] || pdf_options.delete(:filename) || "#{controller_name}.pdf"
|
|
10
10
|
disposition = render_options[:disposition] || pdf_options.delete(:disposition) || "attachment"
|
|
11
11
|
display_url = render_options[:display_url] || pdf_options.delete(:display_url) || request.original_url
|
|
12
|
+
header_html = pdf_options.delete(:header_html)
|
|
13
|
+
footer_html = pdf_options.delete(:footer_html)
|
|
12
14
|
|
|
13
15
|
html = render_to_string(render_options.except(:display_url, :filename, :disposition).merge(formats: [:html]))
|
|
14
|
-
pdf = Gotenberg::Rails.render_pdf(html:, display_url:, pdf_options:, filename:)
|
|
16
|
+
pdf = Gotenberg::Rails.render_pdf(html:, display_url:, header_html:, footer_html:, pdf_options:, filename:)
|
|
15
17
|
|
|
16
18
|
send_data pdf,
|
|
17
19
|
filename: filename,
|
data/lib/gotenberg/rails.rb
CHANGED
|
@@ -32,11 +32,11 @@ module Gotenberg
|
|
|
32
32
|
)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
-
def render_pdf(html: nil, url: nil, display_url: nil, pdf_options: {}, **options)
|
|
35
|
+
def render_pdf(html: nil, url: nil, display_url: nil, header_html: nil, footer_html: nil, pdf_options: {}, **options)
|
|
36
36
|
merged_options = configuration.pdf_options.merge(pdf_options || {})
|
|
37
37
|
html = HtmlPreprocessor.new(html, display_url:).call if html
|
|
38
38
|
|
|
39
|
-
client.render_pdf(html:, url:, pdf_options: merged_options, **options)
|
|
39
|
+
client.render_pdf(html:, url:, header_html:, footer_html:, pdf_options: merged_options, **options)
|
|
40
40
|
end
|
|
41
41
|
|
|
42
42
|
def reset_configuration!
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: gotenberg-rails
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Shinichi Maeshima
|
|
@@ -119,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
119
119
|
- !ruby/object:Gem::Version
|
|
120
120
|
version: '0'
|
|
121
121
|
requirements: []
|
|
122
|
-
rubygems_version: 4.0.
|
|
122
|
+
rubygems_version: 4.0.10
|
|
123
123
|
specification_version: 4
|
|
124
124
|
summary: Render Rails HTML as PDFs with Gotenberg.
|
|
125
125
|
test_files: []
|