wicked_pdf 1.1.0 → 2.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.
- checksums.yaml +5 -5
- data/.github/issue_template.md +15 -0
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +160 -30
- data/.travis.yml +56 -34
- data/CHANGELOG.md +98 -40
- data/README.md +84 -19
- data/Rakefile +14 -4
- data/gemfiles/4.0.gemfile +1 -0
- data/gemfiles/4.1.gemfile +1 -0
- data/gemfiles/4.2.gemfile +3 -0
- data/gemfiles/5.0.gemfile +2 -0
- data/gemfiles/5.1.gemfile +8 -0
- data/gemfiles/5.2.gemfile +10 -0
- data/gemfiles/6.0.gemfile +11 -0
- data/gemfiles/rails_edge.gemfile +5 -2
- data/lib/generators/wicked_pdf_generator.rb +5 -9
- data/lib/wicked_pdf/middleware.rb +1 -1
- data/lib/wicked_pdf/pdf_helper.rb +29 -21
- data/lib/wicked_pdf/progress.rb +33 -0
- data/lib/wicked_pdf/railtie.rb +9 -38
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +62 -7
- data/lib/wicked_pdf/wicked_pdf_helper.rb +2 -1
- data/lib/wicked_pdf.rb +48 -48
- data/test/fixtures/database.yml +4 -0
- data/test/fixtures/manifest.js +3 -0
- data/test/functional/pdf_helper_test.rb +71 -0
- data/test/test_helper.rb +11 -8
- data/test/unit/wicked_pdf_test.rb +22 -3
- data/test/unit/wkhtmltopdf_location_test.rb +50 -0
- data/wicked_pdf.gemspec +20 -12
- metadata +53 -28
- data/gemfiles/2.3.gemfile +0 -10
- data/gemfiles/3.0.gemfile +0 -12
- data/gemfiles/3.1.gemfile +0 -13
- data/gemfiles/3.2.gemfile +0 -12
@@ -1,4 +1,6 @@
|
|
1
|
-
require '
|
1
|
+
require 'net/http'
|
2
|
+
# If webpacker is used, need to check for version
|
3
|
+
require 'webpacker/version' if defined?(Webpacker)
|
2
4
|
|
3
5
|
class WickedPdf
|
4
6
|
module WickedPdfHelper
|
@@ -27,6 +29,32 @@ class WickedPdf
|
|
27
29
|
end.html_safe
|
28
30
|
end
|
29
31
|
|
32
|
+
def wicked_pdf_stylesheet_pack_tag(*sources)
|
33
|
+
return unless defined?(Webpacker)
|
34
|
+
if running_in_development?
|
35
|
+
stylesheet_pack_tag(*sources)
|
36
|
+
else
|
37
|
+
css_text = sources.collect do |source|
|
38
|
+
source = WickedPdfHelper.add_extension(source, 'css')
|
39
|
+
wicked_pdf_stylesheet_link_tag(webpacker_source_url(source))
|
40
|
+
end.join("\n")
|
41
|
+
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def wicked_pdf_javascript_pack_tag(*sources)
|
46
|
+
return unless defined?(Webpacker)
|
47
|
+
|
48
|
+
if running_in_development?
|
49
|
+
javascript_pack_tag(*sources)
|
50
|
+
else
|
51
|
+
sources.collect do |source|
|
52
|
+
source = WickedPdfHelper.add_extension(source, 'js')
|
53
|
+
"<script type='text/javascript'>#{read_asset(webpacker_source_url(source))}</script>"
|
54
|
+
end.join("\n").html_safe
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
30
58
|
def wicked_pdf_image_tag(img, options = {})
|
31
59
|
image_tag wicked_pdf_asset_path(img), options
|
32
60
|
end
|
@@ -68,15 +96,20 @@ class WickedPdf
|
|
68
96
|
end
|
69
97
|
else
|
70
98
|
asset = find_asset(source)
|
71
|
-
|
99
|
+
if asset
|
100
|
+
# older versions need pathname, Sprockets 4 supports only filename
|
101
|
+
asset.respond_to?(:filename) ? asset.filename : asset.pathname
|
102
|
+
else
|
103
|
+
File.join(Rails.public_path, source)
|
104
|
+
end
|
72
105
|
end
|
73
106
|
end
|
74
107
|
|
75
108
|
def find_asset(path)
|
76
109
|
if Rails.application.assets.respond_to?(:find_asset)
|
77
|
-
Rails.application.assets.find_asset(path)
|
110
|
+
Rails.application.assets.find_asset(path, :base_path => Rails.application.root.to_s)
|
78
111
|
else
|
79
|
-
Sprockets::Railtie.build_environment(Rails.application).find_asset(path)
|
112
|
+
Sprockets::Railtie.build_environment(Rails.application).find_asset(path, :base_path => Rails.application.root.to_s)
|
80
113
|
end
|
81
114
|
end
|
82
115
|
|
@@ -93,7 +126,8 @@ class WickedPdf
|
|
93
126
|
end
|
94
127
|
|
95
128
|
def precompiled_or_absolute_asset?(source)
|
96
|
-
Rails.configuration.assets
|
129
|
+
!Rails.configuration.respond_to?(:assets) ||
|
130
|
+
Rails.configuration.assets.compile == false ||
|
97
131
|
source.to_s[0] == '/' ||
|
98
132
|
source.to_s.match(/\Ahttps?\:\/\//)
|
99
133
|
end
|
@@ -112,8 +146,7 @@ class WickedPdf
|
|
112
146
|
end
|
113
147
|
|
114
148
|
def read_from_uri(uri)
|
115
|
-
|
116
|
-
asset = open(uri, "r#{encoding}", &:read)
|
149
|
+
asset = Net::HTTP.get(URI(uri))
|
117
150
|
asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
|
118
151
|
asset
|
119
152
|
end
|
@@ -125,6 +158,28 @@ class WickedPdf
|
|
125
158
|
rescue Zlib::GzipFile::Error
|
126
159
|
nil
|
127
160
|
end
|
161
|
+
|
162
|
+
def webpacker_source_url(source)
|
163
|
+
return unless defined?(Webpacker) && defined?(Webpacker::VERSION)
|
164
|
+
# In Webpacker 3.2.0 asset_pack_url is introduced
|
165
|
+
if Webpacker::VERSION >= '3.2.0'
|
166
|
+
asset_pack_url(source)
|
167
|
+
else
|
168
|
+
source_path = asset_pack_path(source)
|
169
|
+
# Remove last slash from root path
|
170
|
+
root_url[0...-1] + source_path
|
171
|
+
end
|
172
|
+
end
|
173
|
+
|
174
|
+
def running_in_development?
|
175
|
+
return unless defined?(Webpacker)
|
176
|
+
# :dev_server method was added in webpacker 3.0.0
|
177
|
+
if Webpacker.respond_to?(:dev_server)
|
178
|
+
Webpacker.dev_server.running?
|
179
|
+
else
|
180
|
+
Rails.env.development? || Rails.env.test?
|
181
|
+
end
|
182
|
+
end
|
128
183
|
end
|
129
184
|
end
|
130
185
|
end
|
@@ -23,8 +23,9 @@ class WickedPdf
|
|
23
23
|
|
24
24
|
def wicked_pdf_javascript_src_tag(jsfile, options = {})
|
25
25
|
jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
|
26
|
+
type = ::Mime.respond_to?(:[]) ? ::Mime[:js] : ::Mime::JS # ::Mime[:js] cannot be used in Rails 2.3.
|
26
27
|
src = "file:///#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}"
|
27
|
-
content_tag('script', '', { 'type' =>
|
28
|
+
content_tag('script', '', { 'type' => type, 'src' => path_to_javascript(src) }.merge(options))
|
28
29
|
end
|
29
30
|
|
30
31
|
def wicked_pdf_javascript_include_tag(*sources)
|
data/lib/wicked_pdf.rb
CHANGED
@@ -1,32 +1,19 @@
|
|
1
1
|
# wkhtml2pdf Ruby interface
|
2
|
-
# http://
|
2
|
+
# http://wkhtmltopdf.org/
|
3
3
|
|
4
4
|
require 'logger'
|
5
5
|
require 'digest/md5'
|
6
6
|
require 'rbconfig'
|
7
|
+
require 'open3'
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
else
|
11
|
-
require 'open3'
|
12
|
-
end
|
13
|
-
|
14
|
-
begin
|
15
|
-
require 'active_support/core_ext/module/attribute_accessors'
|
16
|
-
rescue LoadError
|
17
|
-
require 'active_support/core_ext/class/attribute_accessors'
|
18
|
-
end
|
19
|
-
|
20
|
-
begin
|
21
|
-
require 'active_support/core_ext/object/blank'
|
22
|
-
rescue LoadError
|
23
|
-
require 'active_support/core_ext/blank'
|
24
|
-
end
|
9
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
10
|
+
require 'active_support/core_ext/object/blank'
|
25
11
|
|
26
12
|
require 'wicked_pdf/version'
|
27
13
|
require 'wicked_pdf/railtie'
|
28
14
|
require 'wicked_pdf/tempfile'
|
29
15
|
require 'wicked_pdf/middleware'
|
16
|
+
require 'wicked_pdf/progress'
|
30
17
|
|
31
18
|
class WickedPdf
|
32
19
|
DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
|
@@ -36,6 +23,8 @@ class WickedPdf
|
|
36
23
|
cattr_accessor :config
|
37
24
|
attr_accessor :binary_version
|
38
25
|
|
26
|
+
include Progress
|
27
|
+
|
39
28
|
def initialize(wkhtmltopdf_binary_path = nil)
|
40
29
|
@exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
|
41
30
|
raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
|
@@ -68,15 +57,18 @@ class WickedPdf
|
|
68
57
|
options.merge!(WickedPdf.config) { |_key, option, _config| option }
|
69
58
|
generated_pdf_file = WickedPdfTempfile.new('wicked_pdf_generated_file.pdf', options[:temp_path])
|
70
59
|
command = [@exe_path]
|
71
|
-
command << '-q' unless on_windows? # suppress errors on stdout
|
72
60
|
command += parse_options(options)
|
73
61
|
command << url
|
74
62
|
command << generated_pdf_file.path.to_s
|
75
63
|
|
76
64
|
print_command(command.inspect) if in_development_mode?
|
77
65
|
|
78
|
-
|
79
|
-
|
66
|
+
if track_progress?(options)
|
67
|
+
invoke_with_progress(command, options)
|
68
|
+
else
|
69
|
+
err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
|
70
|
+
stderr.read
|
71
|
+
end
|
80
72
|
end
|
81
73
|
if options[:return_file]
|
82
74
|
return_file = options.delete(:return_file)
|
@@ -85,9 +77,10 @@ class WickedPdf
|
|
85
77
|
generated_pdf_file.rewind
|
86
78
|
generated_pdf_file.binmode
|
87
79
|
pdf = generated_pdf_file.read
|
80
|
+
raise "Error generating PDF\n Command Error: #{err}" if options[:raise_on_all_errors] && !err.empty?
|
88
81
|
raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
|
89
82
|
pdf
|
90
|
-
rescue => e
|
83
|
+
rescue StandardError => e
|
91
84
|
raise "Failed to execute:\n#{command}\nError: #{e}"
|
92
85
|
ensure
|
93
86
|
generated_pdf_file.close! if generated_pdf_file && !return_file
|
@@ -96,7 +89,7 @@ class WickedPdf
|
|
96
89
|
private
|
97
90
|
|
98
91
|
def in_development_mode?
|
99
|
-
return Rails.env == 'development' if defined?(Rails)
|
92
|
+
return Rails.env == 'development' if defined?(Rails.env)
|
100
93
|
RAILS_ENV == 'development' if defined?(RAILS_ENV)
|
101
94
|
end
|
102
95
|
|
@@ -105,19 +98,19 @@ class WickedPdf
|
|
105
98
|
end
|
106
99
|
|
107
100
|
def print_command(cmd)
|
108
|
-
|
101
|
+
Rails.logger.debug '[wicked_pdf]: ' + cmd
|
109
102
|
end
|
110
103
|
|
111
104
|
def retrieve_binary_version
|
112
105
|
_stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V')
|
113
106
|
@binary_version = parse_version(stdout.gets(nil))
|
114
|
-
rescue
|
107
|
+
rescue StandardError
|
115
108
|
DEFAULT_BINARY_VERSION
|
116
109
|
end
|
117
110
|
|
118
111
|
def parse_version(version_info)
|
119
112
|
match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
|
120
|
-
if match_data && (
|
113
|
+
if match_data && (match_data.length == 2)
|
121
114
|
Gem::Version.new(match_data[1])
|
122
115
|
else
|
123
116
|
DEFAULT_BINARY_VERSION
|
@@ -195,24 +188,26 @@ class WickedPdf
|
|
195
188
|
|
196
189
|
def parse_header_footer(options)
|
197
190
|
r = []
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
191
|
+
unless options.blank?
|
192
|
+
[:header, :footer].collect do |hf|
|
193
|
+
next if options[hf].blank?
|
194
|
+
opt_hf = options[hf]
|
195
|
+
r += make_options(opt_hf, [:center, :font_name, :left, :right], hf.to_s)
|
196
|
+
r += make_options(opt_hf, [:font_size, :spacing], hf.to_s, :numeric)
|
197
|
+
r += make_options(opt_hf, [:line], hf.to_s, :boolean)
|
198
|
+
if options[hf] && options[hf][:content]
|
199
|
+
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
|
200
|
+
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
|
201
|
+
tf.write options[hf][:content]
|
202
|
+
tf.flush
|
203
|
+
options[hf][:html] = {}
|
204
|
+
options[hf][:html][:url] = "file:///#{tf.path}"
|
205
|
+
end
|
206
|
+
unless opt_hf[:html].blank?
|
207
|
+
r += make_option("#{hf}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
|
208
|
+
end
|
214
209
|
end
|
215
|
-
end
|
210
|
+
end
|
216
211
|
r
|
217
212
|
end
|
218
213
|
|
@@ -284,10 +279,12 @@ class WickedPdf
|
|
284
279
|
:dpi,
|
285
280
|
:page_size,
|
286
281
|
:page_width,
|
287
|
-
:title
|
282
|
+
:title,
|
283
|
+
:log_level])
|
288
284
|
r += make_options(options, [:lowquality,
|
289
285
|
:grayscale,
|
290
|
-
:no_pdf_compression
|
286
|
+
:no_pdf_compression,
|
287
|
+
:quiet], '', :boolean)
|
291
288
|
r += make_options(options, [:image_dpi,
|
292
289
|
:image_quality,
|
293
290
|
:page_height], '', :numeric)
|
@@ -322,18 +319,21 @@ class WickedPdf
|
|
322
319
|
:disable_smart_shrinking,
|
323
320
|
:use_xserver,
|
324
321
|
:no_background,
|
322
|
+
:images,
|
323
|
+
:no_images,
|
325
324
|
:no_stop_slow_scripts], '', :boolean)
|
326
325
|
end
|
327
326
|
r
|
328
327
|
end
|
329
328
|
|
330
329
|
def find_wkhtmltopdf_binary_path
|
331
|
-
possible_locations = (ENV['PATH'].split(':') + %w
|
330
|
+
possible_locations = (ENV['PATH'].split(':') + %w[/usr/bin /usr/local/bin]).uniq
|
331
|
+
possible_locations += %w[~/bin] if ENV.key?('HOME')
|
332
332
|
exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
|
333
333
|
exe_path ||= begin
|
334
|
-
detected_path = (defined?(Bundler) ?
|
334
|
+
detected_path = (defined?(Bundler) ? Bundler.which('wkhtmltopdf') : `which wkhtmltopdf`).chomp
|
335
335
|
detected_path.present? && detected_path
|
336
|
-
rescue
|
336
|
+
rescue StandardError
|
337
337
|
nil
|
338
338
|
end
|
339
339
|
exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
|
@@ -5,10 +5,32 @@ module ActionController
|
|
5
5
|
def render_to_string(opts = {})
|
6
6
|
opts.to_s
|
7
7
|
end
|
8
|
+
|
9
|
+
def self.alias_method_chain(_target, _feature); end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ActionControllerMock
|
14
|
+
class Base
|
15
|
+
def render(_)
|
16
|
+
[:base]
|
17
|
+
end
|
18
|
+
|
19
|
+
def render_to_string; end
|
20
|
+
|
21
|
+
def self.after_action(_); end
|
8
22
|
end
|
9
23
|
end
|
10
24
|
|
11
25
|
class PdfHelperTest < ActionController::TestCase
|
26
|
+
module SomePatch
|
27
|
+
def render(_)
|
28
|
+
super.tap do |s|
|
29
|
+
s << :patched
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
12
34
|
def setup
|
13
35
|
@ac = ActionController::Base.new
|
14
36
|
end
|
@@ -22,4 +44,53 @@ class PdfHelperTest < ActionController::TestCase
|
|
22
44
|
:header => { :html => { :template => 'hf.html.erb' } })
|
23
45
|
assert_match %r{^file:\/\/\/.*wicked_header_pdf.*\.html}, options[:header][:html][:url]
|
24
46
|
end
|
47
|
+
|
48
|
+
test 'should not interfere with already prepended patches' do
|
49
|
+
# Emulate railtie
|
50
|
+
if Rails::VERSION::MAJOR >= 5
|
51
|
+
# this spec tests the following:
|
52
|
+
# if another gem prepends a render method to ActionController::Base
|
53
|
+
# before wicked_pdf does, does calling render trigger an infinite loop?
|
54
|
+
# this spec fails with 6392bea1fe3a41682dfd7c20fd9c179b5a758f59 because PdfHelper
|
55
|
+
# aliases the render method prepended by the other gem to render_without_pdf, then
|
56
|
+
# base_evals its own definition of render, which calls render_with_pdf -> render_without_pdf.
|
57
|
+
# If the other gem uses the prepend inhertinance pattern (calling super instead of aliasing),
|
58
|
+
# when it calls super it calls the base_eval'd version of render instead of going up the
|
59
|
+
# inheritance chain, causing an infinite loop.
|
60
|
+
|
61
|
+
# This fiddling with consts is required to get around the fact that PdfHelper checks
|
62
|
+
# that it is being prepended to ActionController::Base
|
63
|
+
OriginalBase = ActionController::Base
|
64
|
+
ActionController.send(:remove_const, :Base)
|
65
|
+
ActionController.const_set(:Base, ActionControllerMock::Base)
|
66
|
+
|
67
|
+
# Emulate another gem being loaded before wicked
|
68
|
+
ActionController::Base.prepend(SomePatch)
|
69
|
+
ActionController::Base.prepend(::WickedPdf::PdfHelper)
|
70
|
+
|
71
|
+
begin
|
72
|
+
# test that wicked's render method is actually called
|
73
|
+
ac = ActionController::Base.new
|
74
|
+
ac.expects(:render_with_wicked_pdf)
|
75
|
+
ac.render(:cats)
|
76
|
+
|
77
|
+
# test that calling render does not trigger infinite loop
|
78
|
+
ac = ActionController::Base.new
|
79
|
+
assert_equal [:base, :patched], ac.render(:cats)
|
80
|
+
rescue SystemStackError
|
81
|
+
assert_equal true, false # force spec failure
|
82
|
+
ensure
|
83
|
+
ActionController.send(:remove_const, :Base)
|
84
|
+
ActionController.const_set(:Base, OriginalBase)
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
test 'should call after_action instead of after_filter when able' do
|
90
|
+
ActionController::Base.expects(:after_filter).with(:clean_temp_files).never
|
91
|
+
ActionController::Base.expects(:after_action).with(:clean_temp_files).once
|
92
|
+
ActionController::Base.class_eval do
|
93
|
+
include ::WickedPdf::PdfHelper
|
94
|
+
end
|
95
|
+
end
|
25
96
|
end
|
data/test/test_helper.rb
CHANGED
@@ -5,13 +5,8 @@ require File.expand_path('../dummy/config/environment.rb', __FILE__)
|
|
5
5
|
|
6
6
|
require 'test/unit'
|
7
7
|
require 'mocha'
|
8
|
-
|
9
|
-
|
10
|
-
require 'test_help'
|
11
|
-
else
|
12
|
-
require 'rails/test_help'
|
13
|
-
require 'mocha/test_unit'
|
14
|
-
end
|
8
|
+
require 'rails/test_help'
|
9
|
+
require 'mocha/test_unit'
|
15
10
|
|
16
11
|
require 'wicked_pdf'
|
17
12
|
|
@@ -24,7 +19,15 @@ if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
|
|
24
19
|
File.open(destination, 'w') { |f| f.write(source) }
|
25
20
|
|
26
21
|
# Copy JS file
|
27
|
-
|
22
|
+
js_dir = assets_dir.join('javascripts')
|
23
|
+
Dir.mkdir(js_dir) unless File.directory?(js_dir)
|
24
|
+
destination = js_dir.join('wicked.js')
|
28
25
|
source = File.read('test/fixtures/wicked.js')
|
29
26
|
File.open(destination, 'w') { |f| f.write(source) }
|
27
|
+
|
28
|
+
config_dir = assets_dir.join('config')
|
29
|
+
Dir.mkdir(config_dir) unless File.directory?(config_dir)
|
30
|
+
source = File.read('test/fixtures/manifest.js')
|
31
|
+
destination = config_dir.join('manifest.js')
|
32
|
+
File.open(destination, 'w') { |f| f.write(source) }
|
30
33
|
end
|
@@ -1,5 +1,4 @@
|
|
1
1
|
require 'test_helper'
|
2
|
-
|
3
2
|
WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' }
|
4
3
|
HTML_DOCUMENT = '<html><body>Hello World</body></html>'.freeze
|
5
4
|
|
@@ -7,7 +6,10 @@ HTML_DOCUMENT = '<html><body>Hello World</body></html>'.freeze
|
|
7
6
|
# Also, smash the returned array of options into a single string for
|
8
7
|
# convenience in testing below.
|
9
8
|
class WickedPdf
|
9
|
+
undef :binary_version
|
10
|
+
undef :binary_version=
|
10
11
|
attr_accessor :binary_version
|
12
|
+
|
11
13
|
def get_parsed_options(opts)
|
12
14
|
parse_options(opts).join(' ')
|
13
15
|
end
|
@@ -131,6 +133,11 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
131
133
|
assert_equal '--outline-depth 5', @wp.get_parsed_options(:outline => { :outline_depth => 5 }).strip
|
132
134
|
end
|
133
135
|
|
136
|
+
test 'should parse no_images option' do
|
137
|
+
assert_equal '--no-images', @wp.get_parsed_options(:no_images => true).strip
|
138
|
+
assert_equal '--images', @wp.get_parsed_options(:images => true).strip
|
139
|
+
end
|
140
|
+
|
134
141
|
test 'should parse margins options' do
|
135
142
|
[:top, :bottom, :left, :right].each do |o|
|
136
143
|
assert_equal "--margin-#{o} 12", @wp.get_parsed_options(:margin => { o => '12' }).strip
|
@@ -200,7 +207,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
200
207
|
test 'should not use double dash options for version without dashes' do
|
201
208
|
@wp.binary_version = WickedPdf::BINARY_VERSION_WITHOUT_DASHES
|
202
209
|
|
203
|
-
%w
|
210
|
+
%w[toc cover].each do |name|
|
204
211
|
assert_equal @wp.get_valid_option(name), name
|
205
212
|
end
|
206
213
|
end
|
@@ -208,7 +215,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
208
215
|
test 'should use double dash options for version with dashes' do
|
209
216
|
@wp.binary_version = Gem::Version.new('0.11.0')
|
210
217
|
|
211
|
-
%w
|
218
|
+
%w[toc cover].each do |name|
|
212
219
|
assert_equal @wp.get_valid_option(name), "--#{name}"
|
213
220
|
end
|
214
221
|
end
|
@@ -218,4 +225,16 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
218
225
|
cover_option = @wp.get_valid_option('cover')
|
219
226
|
assert_equal @wp.get_parsed_options(options), "--disable-javascript --header-center 3 #{cover_option} http://example.org"
|
220
227
|
end
|
228
|
+
|
229
|
+
test 'should output progress when creating pdfs on compatible hosts' do
|
230
|
+
wp = WickedPdf.new
|
231
|
+
output = []
|
232
|
+
options = { :progress => proc { |o| output << o } }
|
233
|
+
wp.pdf_from_string HTML_DOCUMENT, options
|
234
|
+
if RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
|
235
|
+
assert_empty output
|
236
|
+
else
|
237
|
+
assert(output.collect { |l| !l.match(/Loading/).nil? }.include?(true)) # should output something like "Loading pages (1/5)"
|
238
|
+
end
|
239
|
+
end
|
221
240
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
class WkhtmltopdfLocationTest < ActiveSupport::TestCase
|
4
|
+
setup do
|
5
|
+
@saved_config = WickedPdf.config
|
6
|
+
WickedPdf.config = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
teardown do
|
10
|
+
WickedPdf.config = @saved_config
|
11
|
+
end
|
12
|
+
|
13
|
+
test 'should correctly locate wkhtmltopdf without bundler' do
|
14
|
+
bundler_module = Bundler
|
15
|
+
Object.send(:remove_const, :Bundler)
|
16
|
+
|
17
|
+
assert_nothing_raised do
|
18
|
+
WickedPdf.new
|
19
|
+
end
|
20
|
+
|
21
|
+
Object.const_set(:Bundler, bundler_module)
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'should correctly locate wkhtmltopdf with bundler' do
|
25
|
+
assert_nothing_raised do
|
26
|
+
WickedPdf.new
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class LocationNonWritableTest < ActiveSupport::TestCase
|
31
|
+
setup do
|
32
|
+
@saved_config = WickedPdf.config
|
33
|
+
WickedPdf.config = {}
|
34
|
+
|
35
|
+
@old_home = ENV['HOME']
|
36
|
+
ENV['HOME'] = '/not/a/writable/directory'
|
37
|
+
end
|
38
|
+
|
39
|
+
teardown do
|
40
|
+
WickedPdf.config = @saved_config
|
41
|
+
ENV['HOME'] = @old_home
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'should correctly locate wkhtmltopdf with bundler while HOME is set to a non-writable directory' do
|
45
|
+
assert_nothing_raised do
|
46
|
+
WickedPdf.new
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/wicked_pdf.gemspec
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
|
1
|
+
|
2
2
|
lib = File.expand_path('../lib', __FILE__)
|
3
3
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
4
|
require 'wicked_pdf/version'
|
@@ -7,28 +7,36 @@ require 'English'
|
|
7
7
|
Gem::Specification.new do |spec|
|
8
8
|
spec.name = 'wicked_pdf'
|
9
9
|
spec.version = WickedPdf::VERSION
|
10
|
-
spec.authors = ['Miles Z. Sterrett']
|
11
|
-
spec.email = 'miles.sterrett@gmail.com'
|
10
|
+
spec.authors = ['Miles Z. Sterrett', 'David Jones']
|
11
|
+
spec.email = ['miles.sterrett@gmail.com', 'unixmonkey1@gmail.com']
|
12
12
|
spec.summary = 'PDF generator (from HTML) gem for Ruby on Rails'
|
13
13
|
spec.homepage = 'https://github.com/mileszs/wicked_pdf'
|
14
14
|
spec.license = 'MIT'
|
15
15
|
spec.date = Time.now.strftime('%Y-%m-%d')
|
16
|
-
spec.description = <<
|
17
|
-
Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
|
18
|
-
In other words, rather than dealing with a PDF generation DSL of some sort,
|
19
|
-
you simply write an HTML view as you would normally, and let Wicked take care of the hard stuff.
|
20
|
-
|
16
|
+
spec.description = <<DESC.gsub(/^\s+/, '')
|
17
|
+
Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
|
18
|
+
In other words, rather than dealing with a PDF generation DSL of some sort,
|
19
|
+
you simply write an HTML view as you would normally, and let Wicked take care of the hard stuff.
|
20
|
+
DESC
|
21
|
+
spec.metadata = {
|
22
|
+
'changelog_uri' => 'https://github.com/mileszs/wicked_pdf/blob/master/CHANGELOG.md'
|
23
|
+
}
|
21
24
|
|
25
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.2')
|
22
26
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
23
27
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
24
28
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
25
29
|
spec.require_paths = ['lib']
|
26
30
|
|
31
|
+
spec.requirements << 'wkhtmltopdf'
|
32
|
+
|
33
|
+
spec.add_dependency 'activesupport'
|
34
|
+
|
27
35
|
spec.add_development_dependency 'rails'
|
28
|
-
spec.add_development_dependency 'bundler'
|
36
|
+
spec.add_development_dependency 'bundler'
|
29
37
|
spec.add_development_dependency 'rake'
|
30
|
-
spec.add_development_dependency 'rubocop'
|
31
|
-
spec.add_development_dependency 'sqlite3'
|
32
|
-
spec.add_development_dependency 'mocha'
|
38
|
+
spec.add_development_dependency 'rubocop', '~> 0.68.0'
|
39
|
+
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
40
|
+
spec.add_development_dependency 'mocha', '= 1.3'
|
33
41
|
spec.add_development_dependency 'test-unit'
|
34
42
|
end
|