html2pdf-rails 0.1.1 → 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: cd4efa601a6172a9280fd4348c33c24a2bc2f9af4e3ddcbfcc5e563d949dd6ac
4
- data.tar.gz: 728b7376604073203dc6a545bcdb99a2133bb52e77cc4c419b18e567a4925642
3
+ metadata.gz: b22eda92269c6803fe795dc243de5bbfa03ca708ca3ce4080512ffbf6c04af07
4
+ data.tar.gz: 752a299df7c7e9fb3dec6b8f2632d4c6479ff250b40837aa95069b112dcb55c5
5
5
  SHA512:
6
- metadata.gz: a263cc651ad79ea756351e0aef44d6f2ece5c1e1d0577a5470fbba7217a2b45f2528fb2ecc5b962fae71952bc81067cf9e8905b55d1bdbac2b5f3ecc0c0f957d
7
- data.tar.gz: d5becc033f6225ab5953f87b60cbae17a2c92259675ccd74930537536776e74f713e76145d8bdb27e96a839b1eafd412198952cf3d4600d7ec8b9c79673daeb5
6
+ metadata.gz: e269c0797a13770bb3f0924fa0e1192d68b0643d6d155a02c80c628d1a49bfbf8d0ab4834b7a5a88569e0f7e7108f1c4cb6d3487a321ffe0ee2da25949d4e572
7
+ data.tar.gz: 673e12fb52ab924da0dfa3e4cc8069d328b5235420a1db440372f5ee1854ec799b760e9e0ed9f061ad08fc9a913bce23a1f53f1eb9ad0a61f5c8114c121b2c38
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,7 @@ 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_development_dependency "bundler"
28
+ spec.add_development_dependency "rake"
29
+ spec.add_development_dependency "rspec"
30
30
  end
@@ -1,5 +1,7 @@
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
@@ -11,11 +13,19 @@ module Html2Pdf
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: null, 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
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
18
26
  http.request(request)
27
+ rescue Net::ReadTimeout
28
+ raise Html2Pdf::Rails::NetworkError
19
29
  end
20
30
 
21
31
  private
@@ -0,0 +1,16 @@
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
+ end
16
+ end
@@ -9,27 +9,57 @@ module Html2Pdf
9
9
  _html2pdf_make_and_send_pdf(options.delete(:pdf), options)
10
10
  end
11
11
 
12
+ def render_pdf_and_get_url(options)
13
+ _html2_pdf_render_pdf_and_get_url(options.delete(:pdf), options)
14
+ end
15
+
12
16
  private
13
17
 
18
+ def _html2pdf_default_options(pdf_name, options)
19
+ new_options = options.dup
20
+ new_options[:layout] ||= false
21
+ new_options[:template] ||= File.join(controller_path, action_name)
22
+ new_options[:pdf_options] ||= {}
23
+ new_options[:file_name] = "#{pdf_name}.pdf"
24
+ new_options[:disposition] ||= 'inline'
25
+ new_options
26
+ end
27
+
28
+ def _html2_pdf_render_pdf_and_get_url(pdf_name, options = {})
29
+ options = _html2pdf_default_options(pdf_name, options)
30
+ options[:put_to_storage] = true
31
+ json = JSON.parse _html2pdf_make_pdf(options)
32
+ json['url']
33
+ end
34
+
14
35
  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] ||= {}
36
+ options = _html2pdf_default_options(pdf_name, options)
19
37
 
20
38
  if options[:show_as_html]
21
39
  render_opts = options.slice(:template, :layout, :formats, :handlers)
22
40
  render(render_opts.merge({ content_type: 'text/html' }))
23
41
  else
24
42
  pdf_content = _html2pdf_make_pdf(options)
25
- send_data(pdf_content, filename: pdf_name + '.pdf', type: 'application/pdf', disposition: options[:disposition])
43
+ send_data(pdf_content, filename: options[:file_name], type: 'application/pdf', disposition: options[:disposition])
26
44
  end
27
45
  end
28
46
 
29
47
  def _html2pdf_make_pdf(options = {})
30
48
  render_opts = options.slice(:template, :layout, :formats, :handlers)
31
49
  html = render_to_string(render_opts)
32
- Client.post(html, options[:pdf_options]).body
50
+ response = Client.post(
51
+ html: html,
52
+ put_to_storage: options[:put_to_storage],
53
+ file_name: options[:file_name],
54
+ disposition: options[:disposition],
55
+ pdf_options: options[:pdf_options]
56
+ )
57
+ case response.code
58
+ when '200'
59
+ response.body
60
+ else
61
+ raise Html2Pdf::Rails::RequestError.new(response)
62
+ end
33
63
  end
34
64
  end
35
65
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Html2Pdf
4
4
  module Rails
5
- VERSION = '0.1.1'
5
+ VERSION = '0.2.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.2.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: 2020-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -28,44 +28,44 @@ dependencies:
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '1.16'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '1.16'
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - "~>"
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
- version: '10.0'
47
+ version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - "~>"
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
- version: '10.0'
54
+ version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rspec
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - "~>"
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: '3.0'
61
+ version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - "~>"
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
- version: '3.0'
68
+ version: '0'
69
69
  description: PDF generator (from HTML) gem for Ruby on Rails
70
70
  email:
71
71
  - aki77@users.noreply.github.com
@@ -79,7 +79,6 @@ files:
79
79
  - ".travis.yml"
80
80
  - CODE_OF_CONDUCT.md
81
81
  - Gemfile
82
- - Gemfile.lock
83
82
  - LICENSE.txt
84
83
  - README.md
85
84
  - Rakefile
@@ -88,6 +87,7 @@ files:
88
87
  - html2pdf-rails.gemspec
89
88
  - lib/html2pdf/rails.rb
90
89
  - lib/html2pdf/rails/client.rb
90
+ - lib/html2pdf/rails/errors.rb
91
91
  - lib/html2pdf/rails/helper.rb
92
92
  - lib/html2pdf/rails/railtie.rb
93
93
  - lib/html2pdf/rails/rendering.rb
@@ -111,7 +111,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
111
  - !ruby/object:Gem::Version
112
112
  version: '0'
113
113
  requirements: []
114
- rubygems_version: 3.0.3
114
+ rubygems_version: 3.1.2
115
115
  signing_key:
116
116
  specification_version: 4
117
117
  summary: PDF generator (from HTML) gem for Ruby on Rails
@@ -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