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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6afda17f3ec1d2563dd371cfa5a36c9e368dc507062db2aa1e3dd8ae10b0b5f2
4
- data.tar.gz: c988d6aa39af30f7f1e0c52b8348e483a33665aa6e55f5b00c094160de55e252
3
+ metadata.gz: c625732d01298352cc92f7a6a60fa76fc0b51e3e2b5601f4c35221969d40f211
4
+ data.tar.gz: f38ad53bde1bb0ff8c2b588a1ae5af2909ee374e36c567f081a93bfd917c7087
5
5
  SHA512:
6
- metadata.gz: 87351a0801cbf3e392f29e8824ce1dcae7b743efb1afd23ddeef0f67a4fc1f4420c44761eaa6f2aca319862b628c487fbbc7b5a7bb30f440da0f26ab6c1d3479
7
- data.tar.gz: 290ef76a4572942e746029014b411241bf64eed842c309cdcb9b45a6a9ed43e3f697c014bcbfd98203824046ee060f2ed5f2ad641b61f450814c7f932a89b13c
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
- ["files", StringIO.new(html.to_s), { filename: "index.html", content_type: "text/html" }],
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,
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Gotenberg
4
4
  module Rails
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  end
7
7
  end
@@ -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.1.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.6
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: []