wicked_pdf 2.6.3 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efc167edf11352423548ba65167d53beb2e8e60691fb788df361d19c636e7f45
4
- data.tar.gz: e4c6e40dbbb8ba3867e3fbfa1aadbd151c87355f898dbad629f58a2e39f8a4c4
3
+ metadata.gz: ad0741c1e5c8b253601a248d448f53fe359547238517a6340c7ac340f4bf2e15
4
+ data.tar.gz: 1da781d4aa577dd9afb1e90c734c1507d4458d85d7a381dc0f005582dcd064a5
5
5
  SHA512:
6
- metadata.gz: 345c533e38faa42be7700d76e6ff761583c1856f30bf12c55fbff410c77d224f5ed0f7698006316e3565d656a9e4db5cef1936f5ec718957e3652cce85e35b03
7
- data.tar.gz: 00b0cb2ee4c76840d71d984f39987dc04ea23a854d2f38c01b3f21f18ecb7f60c980f477e3eb94a8c1b2b79cda43cb050f521d0003d9306b31cebd8b273dd5ea
6
+ metadata.gz: dd1a3651d3ad14d2c0c8515e81f9cc75bbe617633e956127f77e6d287a761402f5b8d1448641772bf7e69cb16fefd5b38547da9628df43fdb0e6623b11527e9d
7
+ data.tar.gz: f4d1cd6cd91e60f4874e4ad269056a5af32da101e0e03a54231d701958d304f14beacfa9b262559fb56c5375b6899046c8a23090afde226721613d4e0db4f118
@@ -28,29 +28,29 @@ jobs:
28
28
  - gemfile: "6.1"
29
29
  ruby-version: 2.7
30
30
  - gemfile: "6.1"
31
- ruby-version: 3.0
31
+ ruby-version: '3.0'
32
32
  - gemfile: "7.0"
33
33
  ruby-version: 3.1
34
+ - gemfile: "7.0"
35
+ ruby-version: 3.2
34
36
 
35
37
  env:
36
- BUNDLE_GEMFILE: /home/runner/work/wicked_pdf/wicked_pdf/gemfiles/${{ matrix.gemfile }}.gemfile
38
+ BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
37
39
  WKHTMLTOPDF_BIN: /usr/bin/wkhtmltopdf
38
40
 
39
41
  steps:
40
- - uses: actions/checkout@v2
41
-
42
- - name: Install Ruby ${{ matrix.ruby-version }}
43
- uses: ruby/setup-ruby@v1
44
- with:
45
- ruby-version: ${{ matrix.ruby-version }}
42
+ - uses: actions/checkout@v3
46
43
 
47
44
  - name: Install OS dependencies
48
45
  run: |
49
46
  sudo apt-get update -y -qq
50
47
  sudo apt-get install -y wkhtmltopdf
51
48
 
52
- - name: Install gem dependencies
53
- run: bundle install
49
+ - name: Install Ruby ${{ matrix.ruby-version }}
50
+ uses: ruby/setup-ruby@v1
51
+ with:
52
+ ruby-version: ${{ matrix.ruby-version }}
53
+ bundler-cache: true
54
54
 
55
55
  - name: Run tests with Ruby ${{ matrix.ruby-version }} and Rails ${{ matrix.gemfile }}
56
56
  run: bundle exec rake
data/.rubocop.yml CHANGED
@@ -2,10 +2,12 @@ inherit_from: .rubocop_todo.yml
2
2
 
3
3
  AllCops:
4
4
  NewCops: disable
5
+ SuggestExtensions: false
5
6
  TargetRubyVersion: 2.6
6
7
  Exclude:
7
8
  - 'gemfiles/bin/*'
8
9
  - 'test/dummy/**/*'
10
+ - 'vendor/**/*'
9
11
 
10
12
  Metrics/PerceivedComplexity:
11
13
  Enabled: false
data/.rubocop_todo.yml CHANGED
@@ -1,45 +1,39 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2019-10-09 14:16:04 +0200 using RuboCop version 0.68.1.
3
+ # on 2023-01-24 11:24:49 UTC using RuboCop version 1.44.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
7
7
  # versions of RuboCop, may require this file to be generated again.
8
8
 
9
- # Offense count: 11
10
- Metrics/AbcSize:
11
- Max: 45
12
-
13
9
  # Offense count: 2
14
- # Configuration parameters: CountComments.
10
+ # Configuration parameters: CountComments, CountAsOne.
15
11
  Metrics/ClassLength:
16
- Max: 250
12
+ Max: 203
17
13
 
18
14
  # Offense count: 7
15
+ # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
19
16
  Metrics/CyclomaticComplexity:
20
17
  Max: 13
21
18
 
22
19
  # Offense count: 17
23
- # Configuration parameters: CountComments, ExcludedMethods.
20
+ # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
24
21
  Metrics/MethodLength:
25
- Max: 40
22
+ Max: 34
26
23
 
27
- # Offense count: 2
28
- # Configuration parameters: CountComments.
24
+ # Offense count: 1
25
+ # Configuration parameters: CountComments, CountAsOne.
29
26
  Metrics/ModuleLength:
30
- Max: 147
27
+ Max: 104
31
28
 
32
- # Offense count: 6
33
- Metrics/PerceivedComplexity:
34
- Max: 13
35
-
36
- # Offense count: 2
29
+ # Offense count: 1
37
30
  Naming/AccessorMethodName:
38
31
  Exclude:
39
32
  - 'lib/wicked_pdf/middleware.rb'
40
- - 'lib/wicked_pdf/pdf_helper.rb'
41
33
 
42
34
  # Offense count: 1
35
+ # This cop supports safe autocorrection (--autocorrect).
36
+ # Configuration parameters: AllowOnConstant, AllowOnSelfClass.
43
37
  Style/CaseEquality:
44
38
  Exclude:
45
39
  - 'lib/wicked_pdf/wicked_pdf_helper.rb'
@@ -49,66 +43,63 @@ Style/ClassVars:
49
43
  Exclude:
50
44
  - 'lib/wicked_pdf.rb'
51
45
 
52
- # Offense count: 10
46
+ # Offense count: 13
47
+ # Configuration parameters: AllowedConstants.
53
48
  Style/Documentation:
54
49
  Exclude:
55
50
  - 'spec/**/*'
56
51
  - 'test/**/*'
57
52
  - 'generators/wicked_pdf/wicked_pdf_generator.rb'
58
53
  - 'lib/wicked_pdf.rb'
54
+ - 'lib/wicked_pdf/binary.rb'
59
55
  - 'lib/wicked_pdf/middleware.rb'
56
+ - 'lib/wicked_pdf/option_parser.rb'
60
57
  - 'lib/wicked_pdf/pdf_helper.rb'
61
58
  - 'lib/wicked_pdf/progress.rb'
62
59
  - 'lib/wicked_pdf/railtie.rb'
63
- - 'lib/wicked_pdf/binary.rb'
64
- - 'lib/wicked_pdf/option_parser.rb'
65
60
  - 'lib/wicked_pdf/tempfile.rb'
66
61
  - 'lib/wicked_pdf/wicked_pdf_helper.rb'
67
62
  - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
68
63
 
69
64
  # Offense count: 2
70
- # Cop supports --auto-correct.
65
+ # This cop supports safe autocorrection (--autocorrect).
71
66
  Style/ExpandPathArguments:
72
67
  Exclude:
73
68
  - 'test/test_helper.rb'
74
69
  - 'wicked_pdf.gemspec'
75
70
 
76
- # Offense count: 8
77
- # Cop supports --auto-correct.
78
- # Configuration parameters: EnforcedStyle, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
71
+ # Offense count: 4
72
+ # This cop supports safe autocorrection (--autocorrect).
73
+ # Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
79
74
  # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
75
+ # SupportedShorthandSyntax: always, never, either, consistent
80
76
  Style/HashSyntax:
81
77
  Exclude:
82
- - 'gemfiles/4.0.gemfile'
83
- - 'gemfiles/4.1.gemfile'
84
- - 'gemfiles/4.2.gemfile'
85
78
  - 'gemfiles/5.0.gemfile'
86
79
  - 'gemfiles/5.1.gemfile'
87
80
  - 'gemfiles/5.2.gemfile'
88
81
  - 'gemfiles/6.0.gemfile'
89
- - 'gemfiles/rails_edge.gemfile'
90
82
 
91
83
  # Offense count: 2
92
- # Cop supports --auto-correct.
84
+ # This cop supports unsafe autocorrection (--autocorrect-all).
93
85
  # Configuration parameters: EnforcedStyle.
94
86
  # SupportedStyles: literals, strict
95
87
  Style/MutableConstant:
96
88
  Exclude:
97
89
  - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
98
90
 
99
- # Offense count: 6
100
- # Cop supports --auto-correct.
91
+ # Offense count: 5
92
+ # This cop supports safe autocorrection (--autocorrect).
101
93
  # Configuration parameters: EnforcedStyle, AllowInnerSlashes.
102
94
  # SupportedStyles: slashes, percent_r, mixed
103
95
  Style/RegexpLiteral:
104
96
  Exclude:
105
97
  - 'lib/wicked_pdf/middleware.rb'
106
98
  - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
107
- - 'test/unit/wicked_pdf_test.rb'
108
99
 
109
- # Offense count: 111
110
- # Cop supports --auto-correct.
111
- # Configuration parameters: AutoCorrect, AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
100
+ # Offense count: 18
101
+ # This cop supports safe autocorrection (--autocorrect).
102
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
112
103
  # URISchemes: http, https
113
- Metrics/LineLength:
104
+ Layout/LineLength:
114
105
  Max: 563
data/CHANGELOG.md CHANGED
@@ -1,9 +1,33 @@
1
1
  # Change Log
2
- All notable changes to this project will be documented in this file.
3
- This project adheres to [Semantic Versioning](http://semver.org/).
2
+ All notable changes to this project should be documented in this file.
3
+ This project attempts to adhere to [Semantic Versioning](http://semver.org/).
4
4
 
5
- ## [master branch] - Unreleased
5
+ ## [[master branch] - Unreleased changes](https://github.com/mileszs/wicked_pdf/compare/2.8.0...HEAD)
6
6
  ### Breaking Changes
7
+ ### New Features
8
+ ### Fixes
9
+
10
+ ## [2.8.0]
11
+ ### New Features
12
+ - [Add New config option `raise_on_missing_assets`](https://github.com/mileszs/wicked_pdf/pull/1094)
13
+ - [Add support for truffleruby (22.1.0) on Linux](https://github.com/mileszs/wicked_pdf/pull/1028)
14
+
15
+ ### Fixes
16
+ - [Fix Propshaft encoding issue](https://github.com/mileszs/wicked_pdf/pull/1096)
17
+ - [Fix Webpacker & Shakapacker compatibility issue](https://github.com/mileszs/wicked_pdf/pull/1099)
18
+
19
+ ## [2.7.0]
20
+ ### New Features
21
+ - [Support Shakapacker 7](https://github.com/mileszs/wicked_pdf/pull/1067)
22
+ - [Add option `delete_temporary_files` to keep the temporary files generated by `pdf_from_string` method](https://github.com/mileszs/wicked_pdf/pull/1068)
23
+ - [Add support for --allow flag](https://github.com/mileszs/wicked_pdf/pull/1030)
24
+
25
+ ## Fixes
26
+ - [Add require for `stringio`, which is no longer loaded by default in Ruby 3.1+](https://github.com/mileszs/wicked_pdf/pull/1062)
27
+ - [Fix CI build.](https://github.com/mileszs/wicked_pdf/pull/1055)
28
+ - [Fix Header/footer temporary file is removed before `wkhtmltopdf` is called](https://github.com/mileszs/wicked_pdf/pull/1039)
29
+ - [Bump rubocop to 1.46](https://github.com/mileszs/wicked_pdf/pull/1051)
30
+ - [Add Ruby 3.2 to the test matrix](https://github.com/mileszs/wicked_pdf/pull/1046)
7
31
 
8
32
  ## [2.6.3]
9
33
  ### Fixes
@@ -218,13 +242,3 @@ This project adheres to [Semantic Versioning](http://semver.org/).
218
242
  - Issues with `url()` references and embedded `data:base64` assets in stylesheets rendered with `wicked_pdf_stylesheet_link_tag`
219
243
  - Asset helpers no longer add a file extension if it already is specified with one
220
244
 
221
- # Compare Releases
222
- - [2.1.0...HEAD (unreleased changes)](https://github.com/mileszs/wicked_pdf/compare/2.1.0...HEAD)
223
- - [2.0.2...2.1.0](https://github.com/mileszs/wicked_pdf/compare/2.0.2...2.1.0)
224
- - [2.0.0...2.0.2](https://github.com/mileszs/wicked_pdf/compare/2.0.0...2.0.2)
225
- - [1.4.0...2.0.0](https://github.com/mileszs/wicked_pdf/compare/1.4.0...2.0.0)
226
- - [1.3.0...1.4.0](https://github.com/mileszs/wicked_pdf/compare/1.3.0...1.4.0)
227
- - [1.2.0...1.3.0](https://github.com/mileszs/wicked_pdf/compare/1.2.0...1.3.0)
228
- - [1.1.0...1.2.0](https://github.com/mileszs/wicked_pdf/compare/1.1.0...1.2.0)
229
- - [1.0.0...1.1.0](https://github.com/mileszs/wicked_pdf/compare/1.0.0...1.0.0)
230
- - [0.11.0...1.0.0](https://github.com/mileszs/wicked_pdf/compare/0.11.0...1.0.0)
data/README.md CHANGED
@@ -42,9 +42,9 @@ You can see what flags are supported for the current version in [wkhtmltopdf's a
42
42
  If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer:
43
43
 
44
44
  ```ruby
45
- WickedPdf.config = {
46
- exe_path: '/usr/local/bin/wkhtmltopdf',
47
- enable_local_file_access: true
45
+ WickedPdf.configure do |c|
46
+ c.exe_path = '/usr/local/bin/wkhtmltopdf',
47
+ c.enable_local_file_access = true
48
48
  }
49
49
  ```
50
50
 
@@ -191,6 +191,7 @@ class ThingsController < ApplicationController
191
191
  # define as true the key 'disable_local_file_access' or 'enable_local_file_access', not both
192
192
  disable_local_file_access: true,
193
193
  enable_local_file_access: false, # must be true when using wkhtmltopdf > 0.12.6
194
+ allow: ["#{Rails.root}/public"], # could be an array or a single string
194
195
 
195
196
  disable_smart_shrinking: true,
196
197
  use_xserver: true,
@@ -200,6 +201,7 @@ class ThingsController < ApplicationController
200
201
  viewport_size: 'TEXT', # available only with use_xserver or patched QT
201
202
  extra: '', # directly inserted into the command to wkhtmltopdf
202
203
  raise_on_all_errors: nil, # raise error for any stderr output. Such as missing media, image assets
204
+ raise_on_missing_assets: nil, # raise when trying to access a missing asset
203
205
  log_level: 'info', # Available values: none, error, warn, or info - only available with wkhtmltopdf 0.12.5+
204
206
  quiet: false, # `false` is same as `log_level: 'info'`, `true` is same as `log_level: 'none'`
205
207
  outline: { outline: true,
@@ -258,7 +260,8 @@ class ThingsController < ApplicationController
258
260
  disable_toc_links: true,
259
261
  disable_back_links:true,
260
262
  xsl_style_sheet: 'file.xsl'}, # optional XSLT stylesheet to use for styling table of contents
261
- progress: proc { |output| puts output } # proc called when console output changes
263
+ progress: proc { |output| puts output }, # proc called when console output changes
264
+ delete_temporary_files: true # explicitly delete temporary files, default false
262
265
  end
263
266
  end
264
267
  end
@@ -293,14 +296,14 @@ pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')
293
296
  # create a pdf from a URL
294
297
  pdf = WickedPdf.new.pdf_from_url('https://github.com/mileszs/wicked_pdf')
295
298
 
296
- # create a pdf from string using templates, layouts and content option for header or footer
299
+ # create a pdf from string using templates, layouts, and content option for header or footer
297
300
  pdf = WickedPdf.new.pdf_from_string(
298
301
  render_to_string('templates/pdf', layout: 'pdfs/layout_pdf.html'),
299
302
  footer: {
300
303
  content: render_to_string(
301
- 'templates/footer',
302
- layout: 'pdfs/layout_pdf.html'
303
- )
304
+ 'templates/footer',
305
+ layout: 'pdfs/layout_pdf.html'
306
+ )
304
307
  }
305
308
  )
306
309
 
data/gemfiles/6.1.gemfile CHANGED
@@ -7,6 +7,5 @@ gem 'webpacker'
7
7
  gem 'rdoc'
8
8
  gem 'sprockets', '~>3.0'
9
9
  gem 'sqlite3', '~> 1.4'
10
- gem 'rubocop', '1.11.0'
11
10
 
12
11
  gemspec :path => '../'
data/gemfiles/7.0.gemfile CHANGED
@@ -7,6 +7,5 @@ gem 'sprockets-rails'
7
7
  gem 'rdoc'
8
8
  gem 'sprockets', '~>3.0'
9
9
  gem 'sqlite3', '~> 1.4'
10
- gem 'rubocop', '1.11.0'
11
10
 
12
11
  gemspec :path => '../'
@@ -8,23 +8,23 @@
8
8
  #
9
9
  # https://github.com/mileszs/wicked_pdf/blob/master/README.md
10
10
 
11
- WickedPdf.config = {
11
+ WickedPdf.configure do |config|
12
12
  # Path to the wkhtmltopdf executable: This usually isn't needed if using
13
13
  # one of the wkhtmltopdf-binary family of gems.
14
- # exe_path: '/usr/local/bin/wkhtmltopdf',
14
+ # config.exe_path = '/usr/local/bin/wkhtmltopdf',
15
15
  # or
16
- # exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
16
+ # config.exe_path = Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
17
17
 
18
18
  # Needed for wkhtmltopdf 0.12.6+ to use many wicked_pdf asset helpers
19
- # enable_local_file_access: true,
19
+ # config.enable_local_file_access = true,
20
20
 
21
21
  # Layout file to be used for all PDFs
22
22
  # (but can be overridden in `render :pdf` calls)
23
- # layout: 'pdf.html',
23
+ # config.layout = 'pdf.html',
24
24
 
25
25
  # Using wkhtmltopdf without an X server can be achieved by enabling the
26
26
  # 'use_xvfb' flag. This will wrap all wkhtmltopdf commands around the
27
27
  # 'xvfb-run' command, in order to simulate an X server.
28
28
  #
29
- # use_xvfb: true,
30
- }
29
+ # config.use_xvfb = true,
30
+ end
@@ -2,7 +2,7 @@ class WickedPdf
2
2
  class OptionParser
3
3
  BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
4
4
 
5
- attr_reader :binary_version
5
+ attr_reader :binary_version, :hf_tempfiles
6
6
 
7
7
  def initialize(binary_version = WickedPdf::DEFAULT_BINARY_VERSION)
8
8
  @binary_version = binary_version
@@ -61,7 +61,7 @@ class WickedPdf
61
61
  r += make_options(opt_hf, [:line], hf.to_s, :boolean)
62
62
  if options[hf] && options[hf][:content]
63
63
  @hf_tempfiles = [] unless defined?(@hf_tempfiles)
64
- @hf_tempfiles.push(tf = WickedPdf::Tempfile.new("wicked_#{hf}_pdf.html"))
64
+ @hf_tempfiles.push(tf = File.new(Dir::Tmpname.create(["wicked_#{hf}_pdf", '.html']) {}, 'w'))
65
65
  tf.write options[hf][:content]
66
66
  tf.flush
67
67
  options[hf][:html] = {}
@@ -168,7 +168,8 @@ class WickedPdf
168
168
  encoding
169
169
  user_style_sheet
170
170
  viewport_size
171
- window_status])
171
+ window_status
172
+ allow])
172
173
  r += make_options(options, %i[cookie
173
174
  post], '', :name_value)
174
175
  r += make_options(options, %i[redirect_delay
@@ -1,10 +1,10 @@
1
1
  class WickedPdf
2
2
  module Progress
3
- require 'pty' if RbConfig::CONFIG['target_os'] !~ /mswin|mingw/ # no support for windows
3
+ require 'pty' if RbConfig::CONFIG['target_os'] !~ /mswin|mingw/ && RUBY_ENGINE != 'truffleruby' # no support for windows and truffleruby
4
4
  require 'English'
5
5
 
6
6
  def track_progress?(options)
7
- options[:progress] && !on_windows?
7
+ options[:progress] && !(on_windows? || RUBY_ENGINE == 'truffleruby')
8
8
  end
9
9
 
10
10
  def invoke_with_progress(command, options)
@@ -1,4 +1,5 @@
1
1
  require 'tempfile'
2
+ require 'stringio'
2
3
 
3
4
  class WickedPdf
4
5
  class Tempfile < ::Tempfile
@@ -1,3 +1,3 @@
1
1
  class WickedPdf
2
- VERSION = '2.6.3'.freeze
2
+ VERSION = '2.8.0'.freeze
3
3
  end
@@ -1,11 +1,33 @@
1
1
  require 'net/http'
2
2
  require 'delegate'
3
+ require 'stringio'
3
4
 
4
5
  class WickedPdf
5
6
  module WickedPdfHelper
6
7
  module Assets
7
8
  ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
8
9
 
10
+ class MissingAsset < StandardError; end
11
+
12
+ class MissingLocalAsset < MissingAsset
13
+ attr_reader :path
14
+
15
+ def initialize(path)
16
+ @path = path
17
+ super("Could not find asset '#{path}'")
18
+ end
19
+ end
20
+
21
+ class MissingRemoteAsset < MissingAsset
22
+ attr_reader :url, :response
23
+
24
+ def initialize(url, response)
25
+ @url = url
26
+ @response = response
27
+ super("Could not fetch asset '#{url}': server responded with #{response.code} #{response.message}")
28
+ end
29
+ end
30
+
9
31
  class PropshaftAsset < SimpleDelegator
10
32
  def content_type
11
33
  super.to_s
@@ -20,9 +42,39 @@ class WickedPdf
20
42
  end
21
43
  end
22
44
 
45
+ class SprocketsEnvironment
46
+ def self.instance
47
+ @instance ||= Sprockets::Railtie.build_environment(Rails.application)
48
+ end
49
+
50
+ def self.find_asset(*args)
51
+ instance.find_asset(*args)
52
+ end
53
+ end
54
+
55
+ class LocalAsset
56
+ attr_reader :path
57
+
58
+ def initialize(path)
59
+ @path = path
60
+ end
61
+
62
+ def content_type
63
+ Mime::Type.lookup_by_extension(File.extname(path).delete('.'))
64
+ end
65
+
66
+ def to_s
67
+ IO.read(path)
68
+ end
69
+
70
+ def filename
71
+ path.to_s
72
+ end
73
+ end
74
+
23
75
  def wicked_pdf_asset_base64(path)
24
76
  asset = find_asset(path)
25
- raise "Could not find asset '#{path}'" if asset.nil?
77
+ raise MissingLocalAsset, path if asset.nil?
26
78
 
27
79
  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
28
80
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
@@ -149,8 +201,11 @@ class WickedPdf
149
201
  Rails.application.assets.find_asset(path, :base_path => Rails.application.root.to_s)
150
202
  elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
151
203
  PropshaftAsset.new(Rails.application.assets.load_path.find(path))
204
+ elsif Rails.application.respond_to?(:assets_manifest)
205
+ asset_path = File.join(Rails.application.assets_manifest.dir, Rails.application.assets_manifest.assets[path])
206
+ LocalAsset.new(asset_path) if File.file?(asset_path)
152
207
  else
153
- Sprockets::Railtie.build_environment(Rails.application).find_asset(path, :base_path => Rails.application.root.to_s)
208
+ SprocketsEnvironment.find_asset(path, :base_path => Rails.application.root.to_s)
154
209
  end
155
210
  end
156
211
 
@@ -174,20 +229,35 @@ class WickedPdf
174
229
  end
175
230
 
176
231
  def read_asset(source)
177
- if precompiled_or_absolute_asset?(source)
178
- pathname = asset_pathname(source)
179
- if pathname =~ URI_REGEXP
180
- read_from_uri(pathname)
181
- elsif File.file?(pathname)
182
- IO.read(pathname)
183
- end
184
- else
185
- find_asset(source).to_s
232
+ asset = find_asset(source)
233
+ return asset.to_s.force_encoding('UTF-8') if asset
234
+
235
+ unless precompiled_or_absolute_asset?(source)
236
+ raise MissingLocalAsset, source if WickedPdf.config[:raise_on_missing_assets]
237
+
238
+ return
239
+ end
240
+
241
+ pathname = asset_pathname(source)
242
+ if pathname =~ URI_REGEXP
243
+ read_from_uri(pathname)
244
+ elsif File.file?(pathname)
245
+ IO.read(pathname)
246
+ elsif WickedPdf.config[:raise_on_missing_assets]
247
+ raise MissingLocalAsset, pathname if WickedPdf.config[:raise_on_missing_assets]
186
248
  end
187
249
  end
188
250
 
189
251
  def read_from_uri(uri)
190
- asset = Net::HTTP.get(URI(uri))
252
+ response = Net::HTTP.get_response(URI(uri))
253
+
254
+ unless response.is_a?(Net::HTTPSuccess)
255
+ raise MissingRemoteAsset.new(uri, response) if WickedPdf.config[:raise_on_missing_assets]
256
+
257
+ return
258
+ end
259
+
260
+ asset = response.body
191
261
  asset.force_encoding('UTF-8') if asset
192
262
  asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
193
263
  asset
@@ -230,12 +300,13 @@ class WickedPdf
230
300
  end
231
301
 
232
302
  def webpacker_version
233
- return unless defined?(Webpacker)
234
-
235
- # If webpacker is used, need to check for version
236
- require 'webpacker/version'
237
-
238
- Webpacker::VERSION
303
+ if defined?(Shakapacker)
304
+ require 'shakapacker/version'
305
+ Shakapacker::VERSION
306
+ elsif defined?(Webpacker)
307
+ require 'webpacker/version'
308
+ Webpacker::VERSION
309
+ end
239
310
  end
240
311
  end
241
312
  end
data/lib/wicked_pdf.rb CHANGED
@@ -20,10 +20,27 @@ require 'wicked_pdf/progress'
20
20
  class WickedPdf
21
21
  DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
22
22
  @@config = {}
23
- cattr_accessor :config
23
+ cattr_accessor :config, :silence_deprecations
24
24
 
25
25
  include Progress
26
26
 
27
+ def self.config=(config)
28
+ ::Kernel.warn 'WickedPdf.config= is deprecated and will be removed in future versions. Use WickedPdf.configure instead.' unless @@silence_deprecations
29
+
30
+ @@config = config
31
+ end
32
+
33
+ def self.configure
34
+ config = OpenStruct.new(@@config)
35
+ yield config
36
+
37
+ @@config.merge! config.to_h
38
+ end
39
+
40
+ def self.clear_config
41
+ @@config = {}
42
+ end
43
+
27
44
  def initialize(wkhtmltopdf_binary_path = nil)
28
45
  @binary = Binary.new(wkhtmltopdf_binary_path, DEFAULT_BINARY_VERSION)
29
46
  end
@@ -43,16 +60,20 @@ class WickedPdf
43
60
  string_file.write_in_chunks(string)
44
61
  pdf_from_html_file(string_file.path, options)
45
62
  ensure
46
- string_file.close if string_file
63
+ if options[:delete_temporary_files] && string_file
64
+ string_file.close!
65
+ elsif string_file
66
+ string_file.close
67
+ end
47
68
  end
48
69
 
49
- def pdf_from_url(url, options = {})
70
+ def pdf_from_url(url, options = {}) # rubocop:disable Metrics/CyclomaticComplexity
50
71
  # merge in global config options
51
72
  options.merge!(WickedPdf.config) { |_key, option, _config| option }
52
73
  generated_pdf_file = WickedPdf::Tempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
53
74
  command = [@binary.path]
54
75
  command.unshift(@binary.xvfb_run_path) if options[:use_xvfb]
55
- command += parse_options(options)
76
+ command += option_parser.parse(options)
56
77
  command << url
57
78
  command << generated_pdf_file.path.to_s
58
79
 
@@ -61,9 +82,8 @@ class WickedPdf
61
82
  if track_progress?(options)
62
83
  invoke_with_progress(command, options)
63
84
  else
64
- err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
65
- stderr.read
66
- end
85
+ _out, err, status = Open3.capture3(*command)
86
+ err = [status.to_s, err].join("\n") if !err.empty? || !status.success?
67
87
  end
68
88
  if options[:return_file]
69
89
  return_file = options.delete(:return_file)
@@ -79,6 +99,7 @@ class WickedPdf
79
99
  rescue StandardError => e
80
100
  raise "Failed to execute:\n#{command}\nError: #{e}"
81
101
  ensure
102
+ clean_temp_files
82
103
  generated_pdf_file.close! if generated_pdf_file && !return_file
83
104
  end
84
105
 
@@ -98,7 +119,13 @@ class WickedPdf
98
119
  Rails.logger.debug '[wicked_pdf]: ' + cmd
99
120
  end
100
121
 
101
- def parse_options(options)
102
- OptionParser.new(binary_version).parse(options)
122
+ def option_parser
123
+ @option_parser ||= OptionParser.new(binary_version)
124
+ end
125
+
126
+ def clean_temp_files
127
+ return unless option_parser.hf_tempfiles.present?
128
+
129
+ option_parser.hf_tempfiles.each { |file| File.delete(file) }
103
130
  end
104
131
  end
@@ -4,6 +4,15 @@ require 'action_view/test_case'
4
4
  class WickedPdfHelperAssetsTest < ActionView::TestCase
5
5
  include WickedPdf::WickedPdfHelper::Assets
6
6
 
7
+ setup do
8
+ @saved_config = WickedPdf.config
9
+ WickedPdf.config = {}
10
+ end
11
+
12
+ teardown do
13
+ WickedPdf.config = @saved_config
14
+ end
15
+
7
16
  if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
8
17
  test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
9
18
  assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
@@ -15,6 +24,58 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
15
24
  wicked_pdf_stylesheet_link_tag('wicked')
16
25
  end
17
26
 
27
+ test 'wicked_pdf_stylesheet_link_tag should raise if the stylesheet is not available and config is set' do
28
+ Rails.configuration.assets.expects(:compile => true)
29
+ WickedPdf.config[:raise_on_missing_assets] = true
30
+ assert_raise WickedPdf::WickedPdfHelper::Assets::MissingLocalAsset do
31
+ wicked_pdf_stylesheet_link_tag('non_existent')
32
+ end
33
+ end
34
+
35
+ test 'wicked_pdf_stylesheet_link_tag should return empty if the stylesheet is not available' do
36
+ Rails.configuration.assets.expects(:compile => true)
37
+ assert_equal "<style type='text/css'></style>",
38
+ wicked_pdf_stylesheet_link_tag('non_existent')
39
+ end
40
+
41
+ test 'wicked_pdf_stylesheet_link_tag should raise if the absolute path stylesheet is not available and config is set' do
42
+ Rails.configuration.assets.expects(:compile => true)
43
+ WickedPdf.config[:raise_on_missing_assets] = true
44
+ expects(:precompiled_or_absolute_asset? => true).twice
45
+ assert_raise WickedPdf::WickedPdfHelper::Assets::MissingLocalAsset do
46
+ wicked_pdf_stylesheet_link_tag('/non_existent')
47
+ end
48
+ end
49
+
50
+ test 'wicked_pdf_stylesheet_link_tag should return empty if the absolute path stylesheet is not available' do
51
+ Rails.configuration.assets.expects(:compile => true).twice
52
+ assert_equal "<style type='text/css'></style>",
53
+ wicked_pdf_stylesheet_link_tag('/non_existent')
54
+ end
55
+
56
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in when assets are remote' do
57
+ stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 200, :body => '/* Wicked styles */')
58
+ expects(:precompiled_or_absolute_asset? => true).twice
59
+ assert_equal "<style type='text/css'>/* Wicked styles */</style>",
60
+ wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
61
+ end
62
+
63
+ test 'wicked_pdf_stylesheet_link_tag should raise if remote assets are not available and config is set' do
64
+ WickedPdf.config[:raise_on_missing_assets] = true
65
+ stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
66
+ expects(:precompiled_or_absolute_asset? => true).twice
67
+ assert_raise WickedPdf::WickedPdfHelper::Assets::MissingRemoteAsset do
68
+ wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
69
+ end
70
+ end
71
+
72
+ test 'wicked_pdf_stylesheet_link_tag should return empty if remote assets are not available' do
73
+ stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
74
+ expects(:precompiled_or_absolute_asset? => true).twice
75
+ assert_equal "<style type='text/css'></style>",
76
+ wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
77
+ end
78
+
18
79
  test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
19
80
  Rails.configuration.assets.expects(:compile => true)
20
81
  assert_equal image_tag("file:///#{Rails.root.join('public', 'pdf')}"),
data/test/test_helper.rb CHANGED
@@ -7,10 +7,12 @@ require 'test/unit'
7
7
  require 'mocha'
8
8
  require 'rails/test_help'
9
9
  require 'mocha/test_unit'
10
+ require 'webmock/minitest'
10
11
 
11
12
  require 'wicked_pdf'
12
13
 
13
14
  Rails.backtrace_cleaner.remove_silencers!
15
+ WickedPdf.silence_deprecations = true
14
16
 
15
17
  if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
16
18
  # Copy CSS file
@@ -76,6 +76,11 @@ class WickedPdfOptionParserTest < ActiveSupport::TestCase
76
76
  assert_equal "--#{o.to_s.tr('_', '-')} opts", parse_options(o => 'opts').strip
77
77
  end
78
78
 
79
+ %i[allow].each do |o|
80
+ assert_equal "--#{o.to_s.tr('_', '-')} opts", parse_options(o => 'opts').strip
81
+ assert_equal "--#{o.to_s.tr('_', '-')} opts1 --#{o.to_s.tr('_', '-')} opts2", parse_options(o => %w[opts1 opts2]).strip
82
+ end
83
+
79
84
  %i[cookie post].each do |o|
80
85
  assert_equal "--#{o.to_s.tr('_', '-')} name value", parse_options(o => 'name value').strip
81
86
 
@@ -90,7 +95,7 @@ class WickedPdfOptionParserTest < ActiveSupport::TestCase
90
95
  %i[
91
96
  book default_header disable_javascript grayscale lowquality
92
97
  enable_plugins disable_internal_links disable_external_links
93
- print_media_type disable_smart_shrinking use_xserver no_background
98
+ print_media_type disable_smart_shrinking use_xserver no_background disable_local_file_access
94
99
  ].each do |o|
95
100
  assert_equal "--#{o.to_s.tr('_', '-')}", parse_options(o => true).strip
96
101
  end
@@ -7,6 +7,23 @@ class WickedPdfTest < ActiveSupport::TestCase
7
7
  @wp = WickedPdf.new
8
8
  end
9
9
 
10
+ test 'should update config through .configure class method' do
11
+ WickedPdf.configure do |c|
12
+ c.test = 'foobar'
13
+ end
14
+
15
+ assert WickedPdf.config == { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf', :test => 'foobar' }
16
+ end
17
+
18
+ test 'should clear config through .clear_config class method' do
19
+ backup_config = WickedPdf.config
20
+
21
+ WickedPdf.clear_config
22
+ assert WickedPdf.config == {}
23
+
24
+ WickedPdf.config = backup_config
25
+ end
26
+
10
27
  test 'should generate PDF from html document' do
11
28
  pdf = @wp.pdf_from_string HTML_DOCUMENT
12
29
  assert pdf.start_with?('%PDF-1.4')
data/wicked_pdf.gemspec CHANGED
@@ -34,7 +34,8 @@ DESC
34
34
  spec.add_development_dependency 'mocha', '= 1.3'
35
35
  spec.add_development_dependency 'rails'
36
36
  spec.add_development_dependency 'rake'
37
- spec.add_development_dependency 'rubocop', '~> 1.24'
37
+ spec.add_development_dependency 'rubocop', '~> 1.46'
38
38
  spec.add_development_dependency 'sqlite3', '~> 1.3'
39
39
  spec.add_development_dependency 'test-unit'
40
+ spec.add_development_dependency 'webmock', '~> 3.19'
40
41
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wicked_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.6.3
4
+ version: 2.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Miles Z. Sterrett
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-05-19 00:00:00.000000000 Z
12
+ date: 2024-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -87,14 +87,14 @@ dependencies:
87
87
  requirements:
88
88
  - - "~>"
89
89
  - !ruby/object:Gem::Version
90
- version: '1.24'
90
+ version: '1.46'
91
91
  type: :development
92
92
  prerelease: false
93
93
  version_requirements: !ruby/object:Gem::Requirement
94
94
  requirements:
95
95
  - - "~>"
96
96
  - !ruby/object:Gem::Version
97
- version: '1.24'
97
+ version: '1.46'
98
98
  - !ruby/object:Gem::Dependency
99
99
  name: sqlite3
100
100
  requirement: !ruby/object:Gem::Requirement
@@ -123,6 +123,20 @@ dependencies:
123
123
  - - ">="
124
124
  - !ruby/object:Gem::Version
125
125
  version: '0'
126
+ - !ruby/object:Gem::Dependency
127
+ name: webmock
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: '3.19'
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: '3.19'
126
140
  description: |
127
141
  Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
128
142
  In other words, rather than dealing with a PDF generation DSL of some sort,
@@ -200,7 +214,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
200
214
  version: '0'
201
215
  requirements:
202
216
  - wkhtmltopdf
203
- rubygems_version: 3.2.3
217
+ rubygems_version: 3.4.19
204
218
  signing_key:
205
219
  specification_version: 4
206
220
  summary: PDF generator (from HTML) gem for Ruby on Rails