htmlToPdf 2.0.0 → 3.0.1

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.
@@ -0,0 +1,75 @@
1
+ require "spec_helper"
2
+
3
+ # These exercise the controller hook (include HtmlToPdf + around_action)
4
+ # against a real Rails dispatch, not a stub. An earlier version of this gem
5
+ # hooked in as a before_action that quietly rebuilt the controller and
6
+ # re-ran the action on the copy; Rails' own `response=` setter is documented
7
+ # to mark the response as already rendered the moment it's assigned, and
8
+ # copying the throwaway controller's instance variables back onto the real
9
+ # one carried that flag along, so `send_data` raised DoubleRenderError on
10
+ # every single PDF request once run against a real app rather than a stub.
11
+ # It also meant a before_action declared after html_to_pdf never got to run
12
+ # before the view was captured. These specs pin both down so they can't
13
+ # silently come back.
14
+ RSpec.describe "HtmlToPdf controller integration", type: :request do
15
+ around do |example|
16
+ HtmlToPdf.configure { |c| c.default_options = { print_background: true } }
17
+ example.run
18
+ ensure
19
+ HtmlToPdf::BrowserPool.reset!
20
+ end
21
+
22
+ it "does not raise DoubleRenderError and returns a real PDF" do
23
+ get "/widgets/42", params: { format: :pdf }
24
+
25
+ expect(response).to have_http_status(:ok)
26
+ expect(response.media_type).to eq("application/pdf")
27
+ expect(response.body.byteslice(0, 4)).to eq("%PDF")
28
+ end
29
+
30
+ it "runs a before_action declared after the around_action before capturing the view" do
31
+ get "/widgets/42", params: { format: :pdf }
32
+
33
+ pdf_text = extract_text(response.body)
34
+ expect(pdf_text).to include("Widget #42")
35
+ end
36
+
37
+ it "runs the action body exactly once" do
38
+ WidgetsController.show_invocations = 0
39
+
40
+ get "/widgets/7", params: { format: :pdf }
41
+
42
+ expect(WidgetsController.show_invocations).to eq(1)
43
+ end
44
+
45
+ it "leaves ordinary HTML requests completely unaffected" do
46
+ get "/widgets/42"
47
+
48
+ expect(response).to have_http_status(:ok)
49
+ expect(response.media_type).to eq("text/html")
50
+ expect(response.body).to include("Widget #42")
51
+ end
52
+
53
+ it "honors a custom layout passed via pdf_options" do
54
+ get "/widgets/5", params: { format: :pdf, pdf_options: { layout: "pdf" } }
55
+
56
+ expect(extract_text(response.body)).to include("PDF LAYOUT")
57
+ end
58
+
59
+ it "still runs rescue_from for an exception raised inside the action" do
60
+ get "/booms/1", params: { format: :pdf }
61
+
62
+ expect(response).to have_http_status(:unprocessable_content)
63
+ expect(response.body).to eq("handled")
64
+ end
65
+
66
+ def extract_text(pdf_bytes)
67
+ require "tempfile"
68
+ file = Tempfile.new(["spec", ".pdf"], binmode: true)
69
+ file.write(pdf_bytes)
70
+ file.flush
71
+ `pdftotext #{file.path} -`
72
+ ensure
73
+ file&.close!
74
+ end
75
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ include HtmlToPdf
3
+ end
@@ -0,0 +1,16 @@
1
+ class BoomsController < ApplicationController
2
+ class Boom < StandardError; end
3
+
4
+ around_action :html_to_pdf, only: [:show]
5
+ rescue_from Boom, with: :handle_boom
6
+
7
+ def show
8
+ raise Boom, "deliberate failure for spec coverage"
9
+ end
10
+
11
+ private
12
+
13
+ def handle_boom
14
+ render plain: "handled", status: :unprocessable_content
15
+ end
16
+ end
@@ -0,0 +1,25 @@
1
+ class WidgetsController < ApplicationController
2
+ around_action :html_to_pdf, only: [:show]
3
+
4
+ # Declared after the around_action, matching the real-world ordering that
5
+ # exposed the original gem's bug: anything this sets must still be visible
6
+ # to the view when the request is being converted to a PDF.
7
+ before_action :set_widget, only: [:show]
8
+
9
+ cattr_accessor :show_invocations
10
+ self.show_invocations = 0
11
+
12
+ def index
13
+ render plain: "widgets index"
14
+ end
15
+
16
+ def show
17
+ self.class.show_invocations += 1
18
+ end
19
+
20
+ private
21
+
22
+ def set_widget
23
+ @widget = "Widget ##{params[:id]}"
24
+ end
25
+ end
@@ -0,0 +1,4 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <body><%= yield %></body>
4
+ </html>
@@ -0,0 +1,4 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <body><strong>PDF LAYOUT</strong><%= yield %></body>
4
+ </html>
@@ -0,0 +1,2 @@
1
+ <h1><%= @widget %></h1>
2
+ <p>Rendered at <%= Time.now.to_i %></p>
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.routes.draw do
4
+ resources :widgets, only: [:index, :show]
5
+ resources :booms, only: [:show]
6
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,31 @@
1
+ require "combustion"
2
+ Combustion.initialize! :action_controller, :action_view
3
+
4
+ require "rspec/rails"
5
+ require "htmlToPdf"
6
+
7
+ RSpec.configure do |config|
8
+ config.expect_with :rspec do |c|
9
+ c.syntax = :expect
10
+ end
11
+
12
+ config.before do
13
+ HtmlToPdf.reset_configuration!
14
+ end
15
+
16
+ # Specs tagged :chrome exercise a real headless Chrome/Chromium process.
17
+ # Skip them with a clear reason rather than a confusing Ferrum crash when
18
+ # no browser is installed, so running the suite stays contributor-friendly
19
+ # on a machine that just doesn't have Chrome.
20
+ chrome_available =
21
+ begin
22
+ browser = Ferrum::Browser.new
23
+ true
24
+ rescue Ferrum::BinaryNotFoundError
25
+ false
26
+ ensure
27
+ browser&.quit
28
+ end
29
+
30
+ config.filter_run_excluding(chrome: true) unless chrome_available
31
+ end
metadata CHANGED
@@ -1,15 +1,54 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmlToPdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ram Laxman Yadav
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2026-07-11 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: ferrum
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '0.13'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '1.0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '0.13'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '1.0'
32
+ - !ruby/object:Gem::Dependency
33
+ name: activesupport
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '7.0'
39
+ - - "<"
40
+ - !ruby/object:Gem::Version
41
+ version: '9'
42
+ type: :runtime
43
+ prerelease: false
44
+ version_requirements: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '7.0'
49
+ - - "<"
50
+ - !ruby/object:Gem::Version
51
+ version: '9'
13
52
  - !ruby/object:Gem::Dependency
14
53
  name: bundler
15
54
  requirement: !ruby/object:Gem::Requirement
@@ -17,6 +56,9 @@ dependencies:
17
56
  - - ">="
18
57
  - !ruby/object:Gem::Version
19
58
  version: '2.4'
59
+ - - "<"
60
+ - !ruby/object:Gem::Version
61
+ version: '5'
20
62
  type: :development
21
63
  prerelease: false
22
64
  version_requirements: !ruby/object:Gem::Requirement
@@ -24,6 +66,9 @@ dependencies:
24
66
  - - ">="
25
67
  - !ruby/object:Gem::Version
26
68
  version: '2.4'
69
+ - - "<"
70
+ - !ruby/object:Gem::Version
71
+ version: '5'
27
72
  - !ruby/object:Gem::Dependency
28
73
  name: rake
29
74
  requirement: !ruby/object:Gem::Requirement
@@ -31,6 +76,9 @@ dependencies:
31
76
  - - ">="
32
77
  - !ruby/object:Gem::Version
33
78
  version: '13.0'
79
+ - - "<"
80
+ - !ruby/object:Gem::Version
81
+ version: '14'
34
82
  type: :development
35
83
  prerelease: false
36
84
  version_requirements: !ruby/object:Gem::Requirement
@@ -38,29 +86,139 @@ dependencies:
38
86
  - - ">="
39
87
  - !ruby/object:Gem::Version
40
88
  version: '13.0'
89
+ - - "<"
90
+ - !ruby/object:Gem::Version
91
+ version: '14'
92
+ - !ruby/object:Gem::Dependency
93
+ name: rspec
94
+ requirement: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '3.13'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - "~>"
104
+ - !ruby/object:Gem::Version
105
+ version: '3.13'
106
+ - !ruby/object:Gem::Dependency
107
+ name: rspec-rails
108
+ requirement: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - "~>"
111
+ - !ruby/object:Gem::Version
112
+ version: '7.0'
113
+ type: :development
114
+ prerelease: false
115
+ version_requirements: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - "~>"
118
+ - !ruby/object:Gem::Version
119
+ version: '7.0'
120
+ - !ruby/object:Gem::Dependency
121
+ name: combustion
122
+ requirement: !ruby/object:Gem::Requirement
123
+ requirements:
124
+ - - "~>"
125
+ - !ruby/object:Gem::Version
126
+ version: '1.5'
127
+ type: :development
128
+ prerelease: false
129
+ version_requirements: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - "~>"
132
+ - !ruby/object:Gem::Version
133
+ version: '1.5'
134
+ - !ruby/object:Gem::Dependency
135
+ name: rails
136
+ requirement: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '7.0'
141
+ - - "<"
142
+ - !ruby/object:Gem::Version
143
+ version: '9'
144
+ type: :development
145
+ prerelease: false
146
+ version_requirements: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '7.0'
151
+ - - "<"
152
+ - !ruby/object:Gem::Version
153
+ version: '9'
154
+ - !ruby/object:Gem::Dependency
155
+ name: bundler-audit
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: '0.9'
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: '0.9'
41
168
  description: HtmlToPdf lets any Rails controller action be downloaded as a PDF. Request
42
169
  the action with a PDF format and the gem renders the view exactly as the browser
43
- would, then converts it to PDF via wkhtmltopdf. Supports Rails 7.x and 8.x.
170
+ would, then converts it to PDF via a pooled headless Chrome instance (through the
171
+ ferrum gem). Supports Rails 7.x and 8.x.
44
172
  email:
45
173
  - yadavramlaxman@gmail.com
46
174
  executables: []
47
175
  extensions: []
48
176
  extra_rdoc_files: []
49
177
  files:
178
+ - ".github/workflows/ci.yml"
179
+ - ".github/workflows/release.yml"
180
+ - ".gitignore"
181
+ - ".rspec"
182
+ - CHANGELOG.md
50
183
  - Gemfile
51
184
  - Gemfile.lock
52
185
  - LICENSE.txt
53
186
  - README.md
54
187
  - Rakefile
188
+ - config.ru
189
+ - docs/architecture.md
190
+ - docs/contributing.md
191
+ - docs/rails-integration.md
192
+ - docs/security.md
55
193
  - htmlToPdf.gemspec
56
194
  - lib/htmlToPdf.rb
195
+ - lib/htmlToPdf/browser_pool.rb
57
196
  - lib/htmlToPdf/configuration.rb
197
+ - lib/htmlToPdf/print_options.rb
58
198
  - lib/htmlToPdf/version.rb
199
+ - spec/htmlToPdf/browser_pool_spec.rb
200
+ - spec/htmlToPdf/configuration_spec.rb
201
+ - spec/htmlToPdf/print_options_spec.rb
202
+ - spec/integration/controller_spec.rb
203
+ - spec/internal/app/controllers/application_controller.rb
204
+ - spec/internal/app/controllers/booms_controller.rb
205
+ - spec/internal/app/controllers/widgets_controller.rb
206
+ - spec/internal/app/views/layouts/application.html.erb
207
+ - spec/internal/app/views/layouts/pdf.html.erb
208
+ - spec/internal/app/views/widgets/show.html.erb
209
+ - spec/internal/config/routes.rb
210
+ - spec/internal/log/.gitignore
211
+ - spec/internal/public/favicon.ico
212
+ - spec/spec_helper.rb
59
213
  homepage: https://github.com/ramlaxmanyadav/htmlToPdf
60
214
  licenses:
61
215
  - MIT
62
- metadata: {}
63
- post_install_message:
216
+ metadata:
217
+ source_code_uri: https://github.com/ramlaxmanyadav/htmlToPdf
218
+ changelog_uri: https://github.com/ramlaxmanyadav/htmlToPdf/blob/master/CHANGELOG.md
219
+ bug_tracker_uri: https://github.com/ramlaxmanyadav/htmlToPdf/issues
220
+ documentation_uri: https://ramlaxmanyadav.github.io/htmlToPdf/
221
+ rubygems_mfa_required: 'true'
64
222
  rdoc_options: []
65
223
  require_paths:
66
224
  - lib
@@ -75,8 +233,21 @@ required_rubygems_version: !ruby/object:Gem::Requirement
75
233
  - !ruby/object:Gem::Version
76
234
  version: '0'
77
235
  requirements: []
78
- rubygems_version: 3.4.10
79
- signing_key:
236
+ rubygems_version: 3.6.9
80
237
  specification_version: 4
81
- summary: Render Rails controller actions to PDF using wkhtmltopdf
82
- test_files: []
238
+ summary: Render Rails controller actions to PDF using headless Chrome
239
+ test_files:
240
+ - spec/htmlToPdf/browser_pool_spec.rb
241
+ - spec/htmlToPdf/configuration_spec.rb
242
+ - spec/htmlToPdf/print_options_spec.rb
243
+ - spec/integration/controller_spec.rb
244
+ - spec/internal/app/controllers/application_controller.rb
245
+ - spec/internal/app/controllers/booms_controller.rb
246
+ - spec/internal/app/controllers/widgets_controller.rb
247
+ - spec/internal/app/views/layouts/application.html.erb
248
+ - spec/internal/app/views/layouts/pdf.html.erb
249
+ - spec/internal/app/views/widgets/show.html.erb
250
+ - spec/internal/config/routes.rb
251
+ - spec/internal/log/.gitignore
252
+ - spec/internal/public/favicon.ico
253
+ - spec/spec_helper.rb