wicked_pdf 2.7.0 → 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: 120e7e1b14e3e17ac6bc407dd6c7d27438543e87a979d684bab919440ebd7879
4
- data.tar.gz: f9340f26589900f6f30c19f5631ca2104b48685b9ac102c3bdc2e9d168c0c847
3
+ metadata.gz: ad0741c1e5c8b253601a248d448f53fe359547238517a6340c7ac340f4bf2e15
4
+ data.tar.gz: 1da781d4aa577dd9afb1e90c734c1507d4458d85d7a381dc0f005582dcd064a5
5
5
  SHA512:
6
- metadata.gz: f26972b5aa38ac4785ffddd283e787ec49361ae1891d66b8bfe92d3e9b196d4215a03cec3ffced97ec5269f0316f53f0a24374c26a1df255898a01241b85ce57
7
- data.tar.gz: 0d00bcb4d776acb3a469dfb55e0994ca22eab620694b92f7433a4a93aa91a07653848bef7bd3eaff709d861fdf6bf1260cd8ad048be9e671f8f26d78f37d0300
6
+ metadata.gz: dd1a3651d3ad14d2c0c8515e81f9cc75bbe617633e956127f77e6d287a761402f5b8d1448641772bf7e69cb16fefd5b38547da9628df43fdb0e6623b11527e9d
7
+ data.tar.gz: f4d1cd6cd91e60f4874e4ad269056a5af32da101e0e03a54231d701958d304f14beacfa9b262559fb56c5375b6899046c8a23090afde226721613d4e0db4f118
data/CHANGELOG.md CHANGED
@@ -1,9 +1,20 @@
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)
7
18
 
8
19
  ## [2.7.0]
9
20
  ### New Features
@@ -231,13 +242,3 @@ This project adheres to [Semantic Versioning](http://semver.org/).
231
242
  - Issues with `url()` references and embedded `data:base64` assets in stylesheets rendered with `wicked_pdf_stylesheet_link_tag`
232
243
  - Asset helpers no longer add a file extension if it already is specified with one
233
244
 
234
- # Compare Releases
235
- - [2.1.0...HEAD (unreleased changes)](https://github.com/mileszs/wicked_pdf/compare/2.1.0...HEAD)
236
- - [2.0.2...2.1.0](https://github.com/mileszs/wicked_pdf/compare/2.0.2...2.1.0)
237
- - [2.0.0...2.0.2](https://github.com/mileszs/wicked_pdf/compare/2.0.0...2.0.2)
238
- - [1.4.0...2.0.0](https://github.com/mileszs/wicked_pdf/compare/1.4.0...2.0.0)
239
- - [1.3.0...1.4.0](https://github.com/mileszs/wicked_pdf/compare/1.3.0...1.4.0)
240
- - [1.2.0...1.3.0](https://github.com/mileszs/wicked_pdf/compare/1.2.0...1.3.0)
241
- - [1.1.0...1.2.0](https://github.com/mileszs/wicked_pdf/compare/1.1.0...1.2.0)
242
- - [1.0.0...1.1.0](https://github.com/mileszs/wicked_pdf/compare/1.0.0...1.0.0)
243
- - [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
 
@@ -201,6 +201,7 @@ class ThingsController < ApplicationController
201
201
  viewport_size: 'TEXT', # available only with use_xserver or patched QT
202
202
  extra: '', # directly inserted into the command to wkhtmltopdf
203
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
204
205
  log_level: 'info', # Available values: none, error, warn, or info - only available with wkhtmltopdf 0.12.5+
205
206
  quiet: false, # `false` is same as `log_level: 'info'`, `true` is same as `log_level: 'none'`
206
207
  outline: { outline: true,
@@ -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
@@ -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,3 +1,3 @@
1
1
  class WickedPdf
2
- VERSION = '2.7.0'.freeze
2
+ VERSION = '2.8.0'.freeze
3
3
  end
@@ -7,6 +7,27 @@ class WickedPdf
7
7
  module Assets
8
8
  ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
9
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
+
10
31
  class PropshaftAsset < SimpleDelegator
11
32
  def content_type
12
33
  super.to_s
@@ -21,9 +42,39 @@ class WickedPdf
21
42
  end
22
43
  end
23
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
+
24
75
  def wicked_pdf_asset_base64(path)
25
76
  asset = find_asset(path)
26
- raise "Could not find asset '#{path}'" if asset.nil?
77
+ raise MissingLocalAsset, path if asset.nil?
27
78
 
28
79
  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
29
80
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
@@ -150,8 +201,11 @@ class WickedPdf
150
201
  Rails.application.assets.find_asset(path, :base_path => Rails.application.root.to_s)
151
202
  elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
152
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)
153
207
  else
154
- 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)
155
209
  end
156
210
  end
157
211
 
@@ -175,20 +229,35 @@ class WickedPdf
175
229
  end
176
230
 
177
231
  def read_asset(source)
178
- if precompiled_or_absolute_asset?(source)
179
- pathname = asset_pathname(source)
180
- if pathname =~ URI_REGEXP
181
- read_from_uri(pathname)
182
- elsif File.file?(pathname)
183
- IO.read(pathname)
184
- end
185
- else
186
- 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]
187
248
  end
188
249
  end
189
250
 
190
251
  def read_from_uri(uri)
191
- 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
192
261
  asset.force_encoding('UTF-8') if asset
193
262
  asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
194
263
  asset
@@ -232,8 +301,10 @@ class WickedPdf
232
301
 
233
302
  def webpacker_version
234
303
  if defined?(Shakapacker)
304
+ require 'shakapacker/version'
235
305
  Shakapacker::VERSION
236
306
  elsif defined?(Webpacker)
307
+ require 'webpacker/version'
237
308
  Webpacker::VERSION
238
309
  end
239
310
  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
@@ -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
@@ -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
@@ -37,4 +37,5 @@ DESC
37
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.7.0
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: 2023-08-24 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
@@ -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.3.7
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