wicked_pdf 2.0.2 → 2.8.2
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 +4 -4
- data/.github/workflows/ci.yml +56 -0
- data/.rubocop.yml +57 -1
- data/.rubocop_todo.yml +29 -117
- data/CHANGELOG.md +110 -15
- data/README.md +62 -19
- data/Rakefile +3 -10
- data/gemfiles/5.0.gemfile +2 -2
- data/gemfiles/5.1.gemfile +2 -2
- data/gemfiles/5.2.gemfile +3 -4
- data/gemfiles/6.0.gemfile +3 -4
- data/gemfiles/6.1.gemfile +11 -0
- data/gemfiles/7.0.gemfile +11 -0
- data/generators/wicked_pdf/templates/wicked_pdf.rb +14 -5
- data/lib/wicked_pdf/binary.rb +65 -0
- data/lib/wicked_pdf/option_parser.rb +230 -0
- data/lib/wicked_pdf/pdf_helper.rb +29 -44
- data/lib/wicked_pdf/progress.rb +2 -2
- data/lib/wicked_pdf/railtie.rb +3 -12
- data/lib/wicked_pdf/tempfile.rb +33 -3
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +164 -18
- data/lib/wicked_pdf.rb +51 -261
- data/test/fixtures/subdirectory/nested.js +1 -0
- data/test/functional/pdf_helper_test.rb +1 -1
- data/test/functional/wicked_pdf_helper_assets_test.rb +129 -0
- data/test/test_helper.rb +7 -0
- data/test/unit/wicked_pdf_binary_test.rb +26 -0
- data/test/unit/wicked_pdf_option_parser_test.rb +133 -0
- data/test/unit/wicked_pdf_test.rb +16 -156
- data/test/unit/wkhtmltopdf_location_test.rb +0 -2
- data/wicked_pdf.gemspec +5 -5
- metadata +59 -25
- data/.travis.yml +0 -79
- data/gemfiles/4.0.gemfile +0 -7
- data/gemfiles/4.1.gemfile +0 -7
- data/gemfiles/4.2.gemfile +0 -9
- data/gemfiles/rails_edge.gemfile +0 -9
@@ -4,17 +4,146 @@ 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
|
+
|
15
|
+
# @see freerange/mocha#331
|
16
|
+
Rails.application.unstub(:assets)
|
17
|
+
Rails.application.unstub(:assets_manifest)
|
18
|
+
end
|
19
|
+
|
7
20
|
if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
|
8
21
|
test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
|
9
22
|
assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
|
10
23
|
end
|
11
24
|
|
25
|
+
test 'wicked_pdf_asset_base64 works without file extension when using sprockets' do
|
26
|
+
assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('wicked')
|
27
|
+
end
|
28
|
+
|
29
|
+
test 'wicked_pdf_asset_base64 works with nested files and without file extension when using sprockets' do
|
30
|
+
assert_match %r{data:application\/javascript;base64,.+}, wicked_pdf_asset_base64('subdirectory/nested')
|
31
|
+
end
|
32
|
+
|
33
|
+
test 'wicked_pdf_asset_base64 works without file extension when using asset manifest' do
|
34
|
+
stub_manifest = OpenStruct.new(
|
35
|
+
:dir => Rails.root.join('app/assets'),
|
36
|
+
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
|
37
|
+
)
|
38
|
+
Rails.application.stubs(:assets).returns(nil)
|
39
|
+
Rails.application.stubs(:assets_manifest).returns(stub_manifest)
|
40
|
+
|
41
|
+
assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked')
|
42
|
+
end
|
43
|
+
|
44
|
+
test 'wicked_pdf_asset_base64 works with nested files and without file extension when using asset manifest' do
|
45
|
+
stub_manifest = OpenStruct.new(
|
46
|
+
:dir => Rails.root.join('app/assets'),
|
47
|
+
:assets => { 'subdirectory/nested.js' => 'javascripts/subdirectory/nested.js' }
|
48
|
+
)
|
49
|
+
Rails.application.stubs(:assets).returns(nil)
|
50
|
+
Rails.application.stubs(:assets_manifest).returns(stub_manifest)
|
51
|
+
|
52
|
+
assert_match %r{data:text\/javascript;base64,.+}, wicked_pdf_asset_base64('subdirectory/nested')
|
53
|
+
end
|
54
|
+
|
12
55
|
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
|
13
56
|
Rails.configuration.assets.expects(:compile => true)
|
14
57
|
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
|
15
58
|
wicked_pdf_stylesheet_link_tag('wicked')
|
16
59
|
end
|
17
60
|
|
61
|
+
test 'wicked_pdf_stylesheet_link_tag should work without file extension when using sprockets' do
|
62
|
+
Rails.configuration.assets.expects(:compile => true)
|
63
|
+
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
|
64
|
+
wicked_pdf_stylesheet_link_tag('wicked')
|
65
|
+
end
|
66
|
+
|
67
|
+
test 'wicked_pdf_stylesheet_link_tag should work without file extension when using asset manifest' do
|
68
|
+
stub_manifest = OpenStruct.new(
|
69
|
+
:dir => Rails.root.join('app/assets'),
|
70
|
+
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
|
71
|
+
)
|
72
|
+
|
73
|
+
Rails.application.stubs(:assets).returns(nil)
|
74
|
+
Rails.application.stubs(:assets_manifest).returns(stub_manifest)
|
75
|
+
|
76
|
+
assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
|
77
|
+
wicked_pdf_stylesheet_link_tag('wicked')
|
78
|
+
end
|
79
|
+
|
80
|
+
test 'wicked_pdf_stylesheet_link_tag should raise if the stylesheet is not available and config is set' do
|
81
|
+
Rails.configuration.assets.expects(:compile => true)
|
82
|
+
WickedPdf.config[:raise_on_missing_assets] = true
|
83
|
+
assert_raise WickedPdf::WickedPdfHelper::Assets::MissingLocalAsset do
|
84
|
+
wicked_pdf_stylesheet_link_tag('non_existent')
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
test 'wicked_pdf_stylesheet_link_tag should return empty if the stylesheet is not available' do
|
89
|
+
Rails.configuration.assets.expects(:compile => true)
|
90
|
+
assert_equal "<style type='text/css'></style>",
|
91
|
+
wicked_pdf_stylesheet_link_tag('non_existent')
|
92
|
+
end
|
93
|
+
|
94
|
+
test 'wicked_pdf_stylesheet_link_tag should raise if the absolute path stylesheet is not available and config is set' do
|
95
|
+
Rails.configuration.assets.expects(:compile => true)
|
96
|
+
WickedPdf.config[:raise_on_missing_assets] = true
|
97
|
+
expects(:precompiled_or_absolute_asset? => true).twice
|
98
|
+
assert_raise WickedPdf::WickedPdfHelper::Assets::MissingLocalAsset do
|
99
|
+
wicked_pdf_stylesheet_link_tag('/non_existent')
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
test 'wicked_pdf_stylesheet_link_tag should return empty if the absolute path stylesheet is not available' do
|
104
|
+
Rails.configuration.assets.expects(:compile => true).twice
|
105
|
+
assert_equal "<style type='text/css'></style>",
|
106
|
+
wicked_pdf_stylesheet_link_tag('/non_existent')
|
107
|
+
end
|
108
|
+
|
109
|
+
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in when assets are remote' do
|
110
|
+
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 200, :body => '/* Wicked styles */')
|
111
|
+
expects(:precompiled_or_absolute_asset? => true).twice
|
112
|
+
assert_equal "<style type='text/css'>/* Wicked styles */</style>",
|
113
|
+
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
|
114
|
+
end
|
115
|
+
|
116
|
+
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in when assets are remote and using asset manifest' do
|
117
|
+
stub_manifest = OpenStruct.new(
|
118
|
+
:dir => Rails.root.join('app/assets'),
|
119
|
+
:assets => { 'wicked.css' => 'stylesheets/wicked.css', 'wicked.js' => 'javascripts/wicked.js' }
|
120
|
+
)
|
121
|
+
|
122
|
+
Rails.application.stubs(:assets).returns(nil)
|
123
|
+
Rails.application.stubs(:assets_manifest).returns(stub_manifest)
|
124
|
+
|
125
|
+
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 200, :body => '/* Wicked styles */')
|
126
|
+
expects(:precompiled_or_absolute_asset? => true).twice
|
127
|
+
assert_equal "<style type='text/css'>/* Wicked styles */</style>",
|
128
|
+
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
|
129
|
+
end
|
130
|
+
|
131
|
+
test 'wicked_pdf_stylesheet_link_tag should raise if remote assets are not available and config is set' do
|
132
|
+
WickedPdf.config[:raise_on_missing_assets] = true
|
133
|
+
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
|
134
|
+
expects(:precompiled_or_absolute_asset? => true).twice
|
135
|
+
assert_raise WickedPdf::WickedPdfHelper::Assets::MissingRemoteAsset do
|
136
|
+
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
test 'wicked_pdf_stylesheet_link_tag should return empty if remote assets are not available' do
|
141
|
+
stub_request(:get, 'https://www.example.com/wicked.css').to_return(:status => 404, :body => 'File not found')
|
142
|
+
expects(:precompiled_or_absolute_asset? => true).twice
|
143
|
+
assert_equal "<style type='text/css'></style>",
|
144
|
+
wicked_pdf_stylesheet_link_tag('https://www.example.com/wicked.css')
|
145
|
+
end
|
146
|
+
|
18
147
|
test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
|
19
148
|
Rails.configuration.assets.expects(:compile => true)
|
20
149
|
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
|
@@ -25,6 +27,11 @@ if (assets_dir = Rails.root.join('app/assets')) && File.directory?(assets_dir)
|
|
25
27
|
source = File.read('test/fixtures/wicked.js')
|
26
28
|
File.open(destination, 'w') { |f| f.write(source) }
|
27
29
|
|
30
|
+
Dir.mkdir(js_dir.join('subdirectory')) unless File.directory?(js_dir.join('subdirectory'))
|
31
|
+
destination = js_dir.join('subdirectory/nested.js')
|
32
|
+
source = File.read('test/fixtures/subdirectory/nested.js')
|
33
|
+
File.open(destination, 'w') { |f| f.write(source) }
|
34
|
+
|
28
35
|
config_dir = assets_dir.join('config')
|
29
36
|
Dir.mkdir(config_dir) unless File.directory?(config_dir)
|
30
37
|
source = File.read('test/fixtures/manifest.js')
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class WickedPdfBinaryTest < ActiveSupport::TestCase
|
4
|
+
test 'should extract old wkhtmltopdf version' do
|
5
|
+
version_info_sample = "Name:\n wkhtmltopdf 0.9.9\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
|
6
|
+
assert_equal WickedPdf::DEFAULT_BINARY_VERSION, binary.parse_version_string(version_info_sample)
|
7
|
+
end
|
8
|
+
|
9
|
+
test 'should extract new wkhtmltopdf version' do
|
10
|
+
version_info_sample = "Name:\n wkhtmltopdf 0.11.0 rc2\n\nLicense:\n Copyright (C) 2010 wkhtmltopdf/wkhtmltoimage Authors.\n\n\n\n License LGPLv3+: GNU Lesser General Public License version 3 or later\n <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to\n change and redistribute it. There is NO WARRANTY, to the extent permitted by\n law.\n\nAuthors:\n Written by Jan Habermann, Christian Sciberras and Jakob Truelsen. Patches by\n Mehdi Abbad, Lyes Amazouz, Pascal Bach, Emmanuel Bouthenot, Benoit Garret and\n Mario Silva."
|
11
|
+
assert_equal Gem::Version.new('0.11.0'), binary.parse_version_string(version_info_sample)
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'should extract wkhtmltopdf version with nondigit symbols' do
|
15
|
+
version_info_sample = "Name:\n wkhtmltopdf 0.10.4b\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
|
16
|
+
assert_equal Gem::Version.new('0.10.4b'), binary.parse_version_string(version_info_sample)
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'should fallback to default version on parse error' do
|
20
|
+
assert_equal WickedPdf::DEFAULT_BINARY_VERSION, binary.parse_version_string('')
|
21
|
+
end
|
22
|
+
|
23
|
+
def binary(path = nil)
|
24
|
+
WickedPdf::Binary.new(path)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class WickedPdfOptionParserTest < ActiveSupport::TestCase
|
4
|
+
test 'should parse header and footer options' do
|
5
|
+
%i[header footer].each do |hf|
|
6
|
+
%i[center font_name left right].each do |o|
|
7
|
+
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} header_footer",
|
8
|
+
parse_options(hf => { o => 'header_footer' }).strip
|
9
|
+
end
|
10
|
+
|
11
|
+
%i[font_size spacing].each do |o|
|
12
|
+
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} 12",
|
13
|
+
parse_options(hf => { o => '12' }).strip
|
14
|
+
end
|
15
|
+
|
16
|
+
assert_equal "--#{hf}-line",
|
17
|
+
parse_options(hf => { :line => true }).strip
|
18
|
+
assert_equal "--#{hf}-html http://www.abc.com",
|
19
|
+
parse_options(hf => { :html => { :url => 'http://www.abc.com' } }).strip
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
test 'should parse toc options' do
|
24
|
+
toc_option = option_parser.valid_option('toc')
|
25
|
+
|
26
|
+
%i[font_name header_text].each do |o|
|
27
|
+
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} toc",
|
28
|
+
parse_options(:toc => { o => 'toc' }).strip
|
29
|
+
end
|
30
|
+
|
31
|
+
%i[
|
32
|
+
depth header_fs l1_font_size l2_font_size l3_font_size l4_font_size
|
33
|
+
l5_font_size l6_font_size l7_font_size l1_indentation l2_indentation
|
34
|
+
l3_indentation l4_indentation l5_indentation l6_indentation l7_indentation
|
35
|
+
].each do |o|
|
36
|
+
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} 5",
|
37
|
+
parse_options(:toc => { o => 5 }).strip
|
38
|
+
end
|
39
|
+
|
40
|
+
%i[no_dots disable_links disable_back_links].each do |o|
|
41
|
+
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')}",
|
42
|
+
parse_options(:toc => { o => true }).strip
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
test 'should parse outline options' do
|
47
|
+
assert_equal '--outline', parse_options(:outline => { :outline => true }).strip
|
48
|
+
assert_equal '--outline-depth 5', parse_options(:outline => { :outline_depth => 5 }).strip
|
49
|
+
end
|
50
|
+
|
51
|
+
test 'should parse no_images option' do
|
52
|
+
assert_equal '--no-images', parse_options(:no_images => true).strip
|
53
|
+
assert_equal '--images', parse_options(:images => true).strip
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'should parse margins options' do
|
57
|
+
%i[top bottom left right].each do |o|
|
58
|
+
assert_equal "--margin-#{o} 12", parse_options(:margin => { o => '12' }).strip
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
test 'should parse cover' do
|
63
|
+
cover_option = option_parser.valid_option('cover')
|
64
|
+
|
65
|
+
pathname = Rails.root.join('app', 'views', 'pdf', 'file.html')
|
66
|
+
assert_equal "#{cover_option} http://example.org", parse_options(:cover => 'http://example.org').strip, 'URL'
|
67
|
+
assert_equal "#{cover_option} #{pathname}", parse_options(:cover => pathname).strip, 'Pathname'
|
68
|
+
assert_match(/#{cover_option} .+wicked_cover_pdf.+\.html/, parse_options(:cover => '<html><body>HELLO</body></html>').strip, 'HTML')
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'should parse other options' do
|
72
|
+
%i[
|
73
|
+
orientation page_size proxy username password dpi
|
74
|
+
encoding user_style_sheet
|
75
|
+
].each do |o|
|
76
|
+
assert_equal "--#{o.to_s.tr('_', '-')} opts", parse_options(o => 'opts').strip
|
77
|
+
end
|
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
|
+
|
84
|
+
%i[cookie post].each do |o|
|
85
|
+
assert_equal "--#{o.to_s.tr('_', '-')} name value", parse_options(o => 'name value').strip
|
86
|
+
|
87
|
+
nv_formatter = proc { |number| "--#{o.to_s.tr('_', '-')} par#{number} val#{number}" }
|
88
|
+
assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", parse_options(o => ['par1 val1', 'par2 val2']).strip
|
89
|
+
end
|
90
|
+
|
91
|
+
%i[redirect_delay zoom page_offset].each do |o|
|
92
|
+
assert_equal "--#{o.to_s.tr('_', '-')} 5", parse_options(o => 5).strip
|
93
|
+
end
|
94
|
+
|
95
|
+
%i[
|
96
|
+
book default_header disable_javascript grayscale lowquality
|
97
|
+
enable_plugins disable_internal_links disable_external_links
|
98
|
+
print_media_type disable_smart_shrinking use_xserver no_background disable_local_file_access
|
99
|
+
].each do |o|
|
100
|
+
assert_equal "--#{o.to_s.tr('_', '-')}", parse_options(o => true).strip
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
test 'should not use double dash options for version without dashes' do
|
105
|
+
op = option_parser(WickedPdf::OptionParser::BINARY_VERSION_WITHOUT_DASHES)
|
106
|
+
|
107
|
+
%w[toc cover].each do |name|
|
108
|
+
assert_equal op.valid_option(name), name
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
test 'should use double dash options for version with dashes' do
|
113
|
+
op = option_parser(Gem::Version.new('0.11.0'))
|
114
|
+
|
115
|
+
%w[toc cover].each do |name|
|
116
|
+
assert_equal op.valid_option(name), "--#{name}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
test '-- options should not be given after object' do
|
121
|
+
options = { :header => { :center => 3 }, :cover => 'http://example.org', :disable_javascript => true }
|
122
|
+
cover_option = option_parser.valid_option('cover')
|
123
|
+
assert_equal parse_options(options), "--disable-javascript --header-center 3 #{cover_option} http://example.org"
|
124
|
+
end
|
125
|
+
|
126
|
+
def parse_options(options, version = WickedPdf::DEFAULT_BINARY_VERSION)
|
127
|
+
option_parser(version).parse(options).join(' ')
|
128
|
+
end
|
129
|
+
|
130
|
+
def option_parser(version = WickedPdf::DEFAULT_BINARY_VERSION)
|
131
|
+
WickedPdf::OptionParser.new(version)
|
132
|
+
end
|
133
|
+
end
|
@@ -2,26 +2,26 @@ require 'test_helper'
|
|
2
2
|
WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' }
|
3
3
|
HTML_DOCUMENT = '<html><body>Hello World</body></html>'.freeze
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
class WickedPdf
|
9
|
-
undef :binary_version
|
10
|
-
undef :binary_version=
|
11
|
-
attr_accessor :binary_version
|
12
|
-
|
13
|
-
def get_parsed_options(opts)
|
14
|
-
parse_options(opts).join(' ')
|
5
|
+
class WickedPdfTest < ActiveSupport::TestCase
|
6
|
+
def setup
|
7
|
+
@wp = WickedPdf.new
|
15
8
|
end
|
16
9
|
|
17
|
-
|
18
|
-
|
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' }
|
19
16
|
end
|
20
|
-
end
|
21
17
|
|
22
|
-
class
|
23
|
-
|
24
|
-
|
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
25
|
end
|
26
26
|
|
27
27
|
test 'should generate PDF from html document' do
|
@@ -86,146 +86,6 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
|
-
test 'should parse header and footer options' do
|
90
|
-
[:header, :footer].each do |hf|
|
91
|
-
[:center, :font_name, :left, :right].each do |o|
|
92
|
-
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} header_footer",
|
93
|
-
@wp.get_parsed_options(hf => { o => 'header_footer' }).strip
|
94
|
-
end
|
95
|
-
|
96
|
-
[:font_size, :spacing].each do |o|
|
97
|
-
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} 12",
|
98
|
-
@wp.get_parsed_options(hf => { o => '12' }).strip
|
99
|
-
end
|
100
|
-
|
101
|
-
assert_equal "--#{hf}-line",
|
102
|
-
@wp.get_parsed_options(hf => { :line => true }).strip
|
103
|
-
assert_equal "--#{hf}-html http://www.abc.com",
|
104
|
-
@wp.get_parsed_options(hf => { :html => { :url => 'http://www.abc.com' } }).strip
|
105
|
-
end
|
106
|
-
end
|
107
|
-
|
108
|
-
test 'should parse toc options' do
|
109
|
-
toc_option = @wp.get_valid_option('toc')
|
110
|
-
|
111
|
-
[:font_name, :header_text].each do |o|
|
112
|
-
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} toc",
|
113
|
-
@wp.get_parsed_options(:toc => { o => 'toc' }).strip
|
114
|
-
end
|
115
|
-
|
116
|
-
[
|
117
|
-
:depth, :header_fs, :l1_font_size, :l2_font_size, :l3_font_size, :l4_font_size,
|
118
|
-
:l5_font_size, :l6_font_size, :l7_font_size, :l1_indentation, :l2_indentation,
|
119
|
-
:l3_indentation, :l4_indentation, :l5_indentation, :l6_indentation, :l7_indentation
|
120
|
-
].each do |o|
|
121
|
-
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} 5",
|
122
|
-
@wp.get_parsed_options(:toc => { o => 5 }).strip
|
123
|
-
end
|
124
|
-
|
125
|
-
[:no_dots, :disable_links, :disable_back_links].each do |o|
|
126
|
-
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')}",
|
127
|
-
@wp.get_parsed_options(:toc => { o => true }).strip
|
128
|
-
end
|
129
|
-
end
|
130
|
-
|
131
|
-
test 'should parse outline options' do
|
132
|
-
assert_equal '--outline', @wp.get_parsed_options(:outline => { :outline => true }).strip
|
133
|
-
assert_equal '--outline-depth 5', @wp.get_parsed_options(:outline => { :outline_depth => 5 }).strip
|
134
|
-
end
|
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
|
-
|
141
|
-
test 'should parse margins options' do
|
142
|
-
[:top, :bottom, :left, :right].each do |o|
|
143
|
-
assert_equal "--margin-#{o} 12", @wp.get_parsed_options(:margin => { o => '12' }).strip
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
test 'should parse cover' do
|
148
|
-
cover_option = @wp.get_valid_option('cover')
|
149
|
-
|
150
|
-
pathname = Rails.root.join('app', 'views', 'pdf', 'file.html')
|
151
|
-
assert_equal "#{cover_option} http://example.org", @wp.get_parsed_options(:cover => 'http://example.org').strip, 'URL'
|
152
|
-
assert_equal "#{cover_option} #{pathname}", @wp.get_parsed_options(:cover => pathname).strip, 'Pathname'
|
153
|
-
assert_match %r{#{cover_option} .+wicked_cover_pdf.+\.html}, @wp.get_parsed_options(:cover => '<html><body>HELLO</body></html>').strip, 'HTML'
|
154
|
-
end
|
155
|
-
|
156
|
-
test 'should parse other options' do
|
157
|
-
[
|
158
|
-
:orientation, :page_size, :proxy, :username, :password, :dpi,
|
159
|
-
:encoding, :user_style_sheet
|
160
|
-
].each do |o|
|
161
|
-
assert_equal "--#{o.to_s.tr('_', '-')} opts", @wp.get_parsed_options(o => 'opts').strip
|
162
|
-
end
|
163
|
-
|
164
|
-
[:cookie, :post].each do |o|
|
165
|
-
assert_equal "--#{o.to_s.tr('_', '-')} name value", @wp.get_parsed_options(o => 'name value').strip
|
166
|
-
|
167
|
-
nv_formatter = proc { |number| "--#{o.to_s.tr('_', '-')} par#{number} val#{number}" }
|
168
|
-
assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", @wp.get_parsed_options(o => ['par1 val1', 'par2 val2']).strip
|
169
|
-
end
|
170
|
-
|
171
|
-
[:redirect_delay, :zoom, :page_offset].each do |o|
|
172
|
-
assert_equal "--#{o.to_s.tr('_', '-')} 5", @wp.get_parsed_options(o => 5).strip
|
173
|
-
end
|
174
|
-
|
175
|
-
[
|
176
|
-
:book, :default_header, :disable_javascript, :grayscale, :lowquality,
|
177
|
-
:enable_plugins, :disable_internal_links, :disable_external_links,
|
178
|
-
:print_media_type, :disable_smart_shrinking, :use_xserver, :no_background
|
179
|
-
].each do |o|
|
180
|
-
assert_equal "--#{o.to_s.tr('_', '-')}", @wp.get_parsed_options(o => true).strip
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
test 'should extract old wkhtmltopdf version' do
|
185
|
-
version_info_sample = "Name:\n wkhtmltopdf 0.9.9\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
|
186
|
-
assert_equal WickedPdf::DEFAULT_BINARY_VERSION, @wp.send(:parse_version, version_info_sample)
|
187
|
-
end
|
188
|
-
|
189
|
-
test 'should extract new wkhtmltopdf version' do
|
190
|
-
version_info_sample = "Name:\n wkhtmltopdf 0.11.0 rc2\n\nLicense:\n Copyright (C) 2010 wkhtmltopdf/wkhtmltoimage Authors.\n\n\n\n License LGPLv3+: GNU Lesser General Public License version 3 or later\n <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to\n change and redistribute it. There is NO WARRANTY, to the extent permitted by\n law.\n\nAuthors:\n Written by Jan Habermann, Christian Sciberras and Jakob Truelsen. Patches by\n Mehdi Abbad, Lyes Amazouz, Pascal Bach, Emmanuel Bouthenot, Benoit Garret and\n Mario Silva."
|
191
|
-
assert_equal Gem::Version.new('0.11.0'), @wp.send(:parse_version, version_info_sample)
|
192
|
-
end
|
193
|
-
|
194
|
-
test 'should extract wkhtmltopdf version with nondigit symbols' do
|
195
|
-
version_info_sample = "Name:\n wkhtmltopdf 0.10.4b\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
|
196
|
-
assert_equal Gem::Version.new('0.10.4b'), @wp.send(:parse_version, version_info_sample)
|
197
|
-
end
|
198
|
-
|
199
|
-
test 'should fallback to default version on parse error' do
|
200
|
-
assert_equal WickedPdf::DEFAULT_BINARY_VERSION, @wp.send(:parse_version, '')
|
201
|
-
end
|
202
|
-
|
203
|
-
test 'should set version on initialize' do
|
204
|
-
assert_not_equal @wp.send(:binary_version), ''
|
205
|
-
end
|
206
|
-
|
207
|
-
test 'should not use double dash options for version without dashes' do
|
208
|
-
@wp.binary_version = WickedPdf::BINARY_VERSION_WITHOUT_DASHES
|
209
|
-
|
210
|
-
%w[toc cover].each do |name|
|
211
|
-
assert_equal @wp.get_valid_option(name), name
|
212
|
-
end
|
213
|
-
end
|
214
|
-
|
215
|
-
test 'should use double dash options for version with dashes' do
|
216
|
-
@wp.binary_version = Gem::Version.new('0.11.0')
|
217
|
-
|
218
|
-
%w[toc cover].each do |name|
|
219
|
-
assert_equal @wp.get_valid_option(name), "--#{name}"
|
220
|
-
end
|
221
|
-
end
|
222
|
-
|
223
|
-
test '-- options should not be given after object' do
|
224
|
-
options = { :header => { :center => 3 }, :cover => 'http://example.org', :disable_javascript => true }
|
225
|
-
cover_option = @wp.get_valid_option('cover')
|
226
|
-
assert_equal @wp.get_parsed_options(options), "--disable-javascript --header-center 3 #{cover_option} http://example.org"
|
227
|
-
end
|
228
|
-
|
229
89
|
test 'should output progress when creating pdfs on compatible hosts' do
|
230
90
|
wp = WickedPdf.new
|
231
91
|
output = []
|
data/wicked_pdf.gemspec
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
3
|
require 'wicked_pdf/version'
|
@@ -24,19 +23,20 @@ DESC
|
|
24
23
|
|
25
24
|
spec.required_ruby_version = Gem::Requirement.new('>= 2.2')
|
26
25
|
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
|
27
|
-
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
28
26
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
29
27
|
spec.require_paths = ['lib']
|
30
28
|
|
31
29
|
spec.requirements << 'wkhtmltopdf'
|
32
30
|
|
33
31
|
spec.add_dependency 'activesupport'
|
32
|
+
spec.add_dependency 'ostruct'
|
34
33
|
|
35
|
-
spec.add_development_dependency 'rails'
|
36
34
|
spec.add_development_dependency 'bundler'
|
35
|
+
spec.add_development_dependency 'mocha', '= 1.3'
|
36
|
+
spec.add_development_dependency 'rails'
|
37
37
|
spec.add_development_dependency 'rake'
|
38
|
-
spec.add_development_dependency 'rubocop', '~>
|
38
|
+
spec.add_development_dependency 'rubocop', '~> 1.46'
|
39
39
|
spec.add_development_dependency 'sqlite3', '~> 1.3'
|
40
|
-
spec.add_development_dependency 'mocha', '= 1.3'
|
41
40
|
spec.add_development_dependency 'test-unit'
|
41
|
+
spec.add_development_dependency 'webmock', '~> 3.19'
|
42
42
|
end
|