html2pdf-rails 0.1.1 → 0.4.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: cd4efa601a6172a9280fd4348c33c24a2bc2f9af4e3ddcbfcc5e563d949dd6ac
4
- data.tar.gz: 728b7376604073203dc6a545bcdb99a2133bb52e77cc4c419b18e567a4925642
3
+ metadata.gz: 0f1f3b07b6c9e9ff8ca9080b8148465ac6fa3fefb1e5e7b2722f0b2b23c09264
4
+ data.tar.gz: ddf875845930cb93cac1acf2026db6d32e7e9a2aaf812775e212b10fa28cd1c0
5
5
  SHA512:
6
- metadata.gz: a263cc651ad79ea756351e0aef44d6f2ece5c1e1d0577a5470fbba7217a2b45f2528fb2ecc5b962fae71952bc81067cf9e8905b55d1bdbac2b5f3ecc0c0f957d
7
- data.tar.gz: d5becc033f6225ab5953f87b60cbae17a2c92259675ccd74930537536776e74f713e76145d8bdb27e96a839b1eafd412198952cf3d4600d7ec8b9c79673daeb5
6
+ metadata.gz: e1cb31fe1b8cf4d32fd7a34e3f93cc31775238c43728eee7b4c3d2339b3741ee0d2b354c1f1c6b3a32e95361724642d55966d64c8d5ebde6b359812cc300eda3
7
+ data.tar.gz: e2f92706c6383b135d55e03b3b9aec5b94b4c7e04f1980eaa643c6857db56c908dfcc82064ab9cacb3e23527813d4d26f2b38765d2b5bddc774dafa622a73988
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ Gemfile.lock
data/README.md CHANGED
@@ -51,6 +51,14 @@ Layout
51
51
  #content= yield
52
52
  ```
53
53
 
54
+ ### Put PDF to Cloud Storage and return signed url
55
+ You can get signed url of Cloud Storage if your Cloud Funciton code support it.
56
+
57
+ ```ruby
58
+ pdf_url = render_pdf_and_get_url pdf: 'file_name'
59
+ redirect_to pdf_url
60
+ ```
61
+
54
62
  ### Advanced Usage with all available options
55
63
 
56
64
  ```ruby
@@ -91,7 +99,7 @@ const runOptions = {
91
99
  exports.html2pdf = functions
92
100
  .runWith(runOptions)
93
101
  .https.onRequest(
94
- async ({ method, body: { html = "", pdfOptions = {} } }, res) => {
102
+ async ({ method, body: { html = "", putToStorage = false, pdfOptions = {} } }, res) => {
95
103
  const browser = await puppeteer.launch({
96
104
  headless: true,
97
105
  args: ["--no-sandbox"]
@@ -102,8 +110,12 @@ exports.html2pdf = functions
102
110
  waitUntil: "networkidle0"
103
111
  });
104
112
  const pdf = await page.pdf(pdfOptions);
105
- res.header({ "Content-Type": "application/pdf" });
106
- res.send(pdf);
113
+ if (putToStorage) {
114
+ // Code for Cloud Storage is omitted.
115
+ } else {
116
+ res.header({ "Content-Type": "application/pdf" });
117
+ res.send(pdf);
118
+ }
107
119
  }
108
120
  );
109
121
  ```
@@ -24,7 +24,8 @@ Gem::Specification.new do |spec|
24
24
  spec.require_paths = ["lib"]
25
25
 
26
26
  spec.add_dependency "rails", ">= 5.2.0"
27
- spec.add_development_dependency "bundler", "~> 1.16"
28
- spec.add_development_dependency "rake", "~> 10.0"
29
- spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_dependency "retryable"
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "rake"
30
+ spec.add_development_dependency "rspec"
30
31
  end
@@ -1,21 +1,40 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'html2pdf/rails/errors'
4
+
3
5
  module Html2Pdf
4
6
  module Rails
5
7
  class Client
6
- def self.post(*args)
7
- self.new(Html2Pdf.config.endpoint).post(*args)
8
+ def self.post(**options)
9
+ self.new(Html2Pdf.config.endpoint).post(**options)
8
10
  end
9
11
 
10
12
  def initialize(endpoint)
11
13
  @uri = URI.parse(endpoint)
12
14
  end
13
15
 
14
- def post(html, pdf_options = {})
16
+ def post(html:, put_to_storage: false, file_name: nil, disposition: nil, pdf_options: {})
15
17
  http = Net::HTTP.new(@uri.host, @uri.port).tap { |h| h.use_ssl = @uri.scheme == 'https' }
16
18
  request = Net::HTTP::Post.new(@uri.request_uri, headers)
17
- request.body = { html: html, pdfOptions: pdf_options }.to_json
18
- http.request(request)
19
+ request.body = {
20
+ html: html,
21
+ putToStorage: put_to_storage,
22
+ fileName: file_name,
23
+ responseDisposition: disposition,
24
+ pdfOptions: pdf_options
25
+ }.to_json
26
+ response = http.request(request)
27
+
28
+ case response.code
29
+ when '200'
30
+ response.body
31
+ when '503'
32
+ raise Html2Pdf::Rails::ServiceUnavailableError.new(response)
33
+ else
34
+ raise Html2Pdf::Rails::RequestError.new(response)
35
+ end
36
+ rescue Net::ReadTimeout
37
+ raise Html2Pdf::Rails::NetworkError
19
38
  end
20
39
 
21
40
  private
@@ -0,0 +1,19 @@
1
+ module Html2Pdf
2
+ module Rails
3
+ class NetworkError < StandardError
4
+ end
5
+
6
+ class RequestError < StandardError
7
+ attr_reader :response
8
+
9
+ def initialize(response, msg = nil)
10
+ msg ||= "html2pdf request failed and got HTTP status #{response.code}"
11
+ super(msg)
12
+ @response = response
13
+ end
14
+ end
15
+
16
+ class ServiceUnavailableError < RequestError
17
+ end
18
+ end
19
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'retryable'
3
4
  require 'html2pdf/rails/client'
4
5
 
5
6
  module Html2Pdf
@@ -9,27 +10,54 @@ module Html2Pdf
9
10
  _html2pdf_make_and_send_pdf(options.delete(:pdf), options)
10
11
  end
11
12
 
13
+ def render_pdf_and_get_url(options)
14
+ _html2_pdf_render_pdf_and_get_url(options.delete(:pdf), options)
15
+ end
16
+
12
17
  private
13
18
 
19
+ def _html2pdf_default_options(pdf_name, options)
20
+ new_options = options.dup
21
+ new_options[:layout] ||= false
22
+ new_options[:template] ||= File.join(controller_path, action_name)
23
+ new_options[:pdf_options] ||= {}
24
+ new_options[:file_name] = "#{pdf_name}.pdf"
25
+ new_options[:disposition] ||= 'inline'
26
+ new_options
27
+ end
28
+
29
+ def _html2_pdf_render_pdf_and_get_url(pdf_name, options = {})
30
+ options = _html2pdf_default_options(pdf_name, options)
31
+ options[:put_to_storage] = true
32
+ json = JSON.parse(_html2pdf_make_pdf(options))
33
+ json['url']
34
+ end
35
+
14
36
  def _html2pdf_make_and_send_pdf(pdf_name, options = {})
15
- options[:layout] ||= false
16
- options[:template] ||= File.join(controller_path, action_name)
17
- options[:disposition] ||= 'inline'
18
- options[:pdf_options] ||= {}
37
+ options = _html2pdf_default_options(pdf_name, options)
19
38
 
20
39
  if options[:show_as_html]
21
40
  render_opts = options.slice(:template, :layout, :formats, :handlers)
22
41
  render(render_opts.merge({ content_type: 'text/html' }))
23
42
  else
24
43
  pdf_content = _html2pdf_make_pdf(options)
25
- send_data(pdf_content, filename: pdf_name + '.pdf', type: 'application/pdf', disposition: options[:disposition])
44
+ send_data(pdf_content, filename: options[:file_name], type: 'application/pdf', disposition: options[:disposition])
26
45
  end
27
46
  end
28
47
 
29
48
  def _html2pdf_make_pdf(options = {})
30
49
  render_opts = options.slice(:template, :layout, :formats, :handlers)
31
- html = render_to_string(render_opts)
32
- Client.post(html, options[:pdf_options]).body
50
+ html = render_to_string(**render_opts)
51
+
52
+ Retryable.retryable(tries: 3, on: Html2Pdf::Rails::ServiceUnavailableError) do
53
+ Client.post(
54
+ html: html,
55
+ put_to_storage: options[:put_to_storage],
56
+ file_name: options[:file_name],
57
+ disposition: options[:disposition],
58
+ pdf_options: options[:pdf_options]
59
+ )
60
+ end
33
61
  end
34
62
  end
35
63
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Html2Pdf
4
4
  module Rails
5
- VERSION = '0.1.1'
5
+ VERSION = '0.4.0'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: html2pdf-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - aki77
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-06-11 00:00:00.000000000 Z
11
+ date: 2021-10-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -24,48 +24,62 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 5.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: retryable
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - "~>"
45
+ - - ">="
32
46
  - !ruby/object:Gem::Version
33
- version: '1.16'
47
+ version: '0'
34
48
  type: :development
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - "~>"
52
+ - - ">="
39
53
  - !ruby/object:Gem::Version
40
- version: '1.16'
54
+ version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rake
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - "~>"
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
- version: '10.0'
61
+ version: '0'
48
62
  type: :development
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - "~>"
66
+ - - ">="
53
67
  - !ruby/object:Gem::Version
54
- version: '10.0'
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rspec
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - "~>"
73
+ - - ">="
60
74
  - !ruby/object:Gem::Version
61
- version: '3.0'
75
+ version: '0'
62
76
  type: :development
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - "~>"
80
+ - - ">="
67
81
  - !ruby/object:Gem::Version
68
- version: '3.0'
82
+ version: '0'
69
83
  description: PDF generator (from HTML) gem for Ruby on Rails
70
84
  email:
71
85
  - aki77@users.noreply.github.com
@@ -79,7 +93,6 @@ files:
79
93
  - ".travis.yml"
80
94
  - CODE_OF_CONDUCT.md
81
95
  - Gemfile
82
- - Gemfile.lock
83
96
  - LICENSE.txt
84
97
  - README.md
85
98
  - Rakefile
@@ -88,6 +101,7 @@ files:
88
101
  - html2pdf-rails.gemspec
89
102
  - lib/html2pdf/rails.rb
90
103
  - lib/html2pdf/rails/client.rb
104
+ - lib/html2pdf/rails/errors.rb
91
105
  - lib/html2pdf/rails/helper.rb
92
106
  - lib/html2pdf/rails/railtie.rb
93
107
  - lib/html2pdf/rails/rendering.rb
@@ -111,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
125
  - !ruby/object:Gem::Version
112
126
  version: '0'
113
127
  requirements: []
114
- rubygems_version: 3.0.3
128
+ rubygems_version: 3.1.2
115
129
  signing_key:
116
130
  specification_version: 4
117
131
  summary: PDF generator (from HTML) gem for Ruby on Rails
data/Gemfile.lock DELETED
@@ -1,142 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- html2pdf-rails (0.1.1)
5
- rails (>= 5.2.0)
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- actioncable (5.2.3)
11
- actionpack (= 5.2.3)
12
- nio4r (~> 2.0)
13
- websocket-driver (>= 0.6.1)
14
- actionmailer (5.2.3)
15
- actionpack (= 5.2.3)
16
- actionview (= 5.2.3)
17
- activejob (= 5.2.3)
18
- mail (~> 2.5, >= 2.5.4)
19
- rails-dom-testing (~> 2.0)
20
- actionpack (5.2.3)
21
- actionview (= 5.2.3)
22
- activesupport (= 5.2.3)
23
- rack (~> 2.0)
24
- rack-test (>= 0.6.3)
25
- rails-dom-testing (~> 2.0)
26
- rails-html-sanitizer (~> 1.0, >= 1.0.2)
27
- actionview (5.2.3)
28
- activesupport (= 5.2.3)
29
- builder (~> 3.1)
30
- erubi (~> 1.4)
31
- rails-dom-testing (~> 2.0)
32
- rails-html-sanitizer (~> 1.0, >= 1.0.3)
33
- activejob (5.2.3)
34
- activesupport (= 5.2.3)
35
- globalid (>= 0.3.6)
36
- activemodel (5.2.3)
37
- activesupport (= 5.2.3)
38
- activerecord (5.2.3)
39
- activemodel (= 5.2.3)
40
- activesupport (= 5.2.3)
41
- arel (>= 9.0)
42
- activestorage (5.2.3)
43
- actionpack (= 5.2.3)
44
- activerecord (= 5.2.3)
45
- marcel (~> 0.3.1)
46
- activesupport (5.2.3)
47
- concurrent-ruby (~> 1.0, >= 1.0.2)
48
- i18n (>= 0.7, < 2)
49
- minitest (~> 5.1)
50
- tzinfo (~> 1.1)
51
- arel (9.0.0)
52
- builder (3.2.3)
53
- concurrent-ruby (1.1.5)
54
- crass (1.0.4)
55
- diff-lcs (1.3)
56
- erubi (1.8.0)
57
- globalid (0.4.2)
58
- activesupport (>= 4.2.0)
59
- i18n (1.6.0)
60
- concurrent-ruby (~> 1.0)
61
- loofah (2.2.3)
62
- crass (~> 1.0.2)
63
- nokogiri (>= 1.5.9)
64
- mail (2.7.1)
65
- mini_mime (>= 0.1.1)
66
- marcel (0.3.3)
67
- mimemagic (~> 0.3.2)
68
- method_source (0.9.2)
69
- mimemagic (0.3.3)
70
- mini_mime (1.0.1)
71
- mini_portile2 (2.4.0)
72
- minitest (5.11.3)
73
- nio4r (2.3.1)
74
- nokogiri (1.10.3)
75
- mini_portile2 (~> 2.4.0)
76
- rack (2.0.7)
77
- rack-test (1.1.0)
78
- rack (>= 1.0, < 3)
79
- rails (5.2.3)
80
- actioncable (= 5.2.3)
81
- actionmailer (= 5.2.3)
82
- actionpack (= 5.2.3)
83
- actionview (= 5.2.3)
84
- activejob (= 5.2.3)
85
- activemodel (= 5.2.3)
86
- activerecord (= 5.2.3)
87
- activestorage (= 5.2.3)
88
- activesupport (= 5.2.3)
89
- bundler (>= 1.3.0)
90
- railties (= 5.2.3)
91
- sprockets-rails (>= 2.0.0)
92
- rails-dom-testing (2.0.3)
93
- activesupport (>= 4.2.0)
94
- nokogiri (>= 1.6)
95
- rails-html-sanitizer (1.0.4)
96
- loofah (~> 2.2, >= 2.2.2)
97
- railties (5.2.3)
98
- actionpack (= 5.2.3)
99
- activesupport (= 5.2.3)
100
- method_source
101
- rake (>= 0.8.7)
102
- thor (>= 0.19.0, < 2.0)
103
- rake (10.5.0)
104
- rspec (3.8.0)
105
- rspec-core (~> 3.8.0)
106
- rspec-expectations (~> 3.8.0)
107
- rspec-mocks (~> 3.8.0)
108
- rspec-core (3.8.0)
109
- rspec-support (~> 3.8.0)
110
- rspec-expectations (3.8.2)
111
- diff-lcs (>= 1.2.0, < 2.0)
112
- rspec-support (~> 3.8.0)
113
- rspec-mocks (3.8.0)
114
- diff-lcs (>= 1.2.0, < 2.0)
115
- rspec-support (~> 3.8.0)
116
- rspec-support (3.8.0)
117
- sprockets (3.7.2)
118
- concurrent-ruby (~> 1.0)
119
- rack (> 1, < 3)
120
- sprockets-rails (3.2.1)
121
- actionpack (>= 4.0)
122
- activesupport (>= 4.0)
123
- sprockets (>= 3.0.0)
124
- thor (0.20.3)
125
- thread_safe (0.3.6)
126
- tzinfo (1.2.5)
127
- thread_safe (~> 0.1)
128
- websocket-driver (0.7.1)
129
- websocket-extensions (>= 0.1.0)
130
- websocket-extensions (0.1.4)
131
-
132
- PLATFORMS
133
- ruby
134
-
135
- DEPENDENCIES
136
- bundler (~> 1.16)
137
- html2pdf-rails!
138
- rake (~> 10.0)
139
- rspec (~> 3.0)
140
-
141
- BUNDLED WITH
142
- 1.17.2