wicked_pdf 1.0.4 → 1.0.5
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/.rubocop_todo.yml +1 -8
- data/.travis.yml +7 -2
- data/CHANGELOG.md +40 -8
- data/Rakefile +1 -0
- data/lib/wicked_pdf.rb +16 -17
- data/lib/wicked_pdf/middleware.rb +2 -2
- data/lib/wicked_pdf/pdf_helper.rb +6 -7
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +7 -4
- data/test/functional/pdf_helper_test.rb +1 -1
- data/test/functional/wicked_pdf_helper_assets_test.rb +6 -6
- data/test/test_helper.rb +1 -1
- data/test/unit/wicked_pdf_test.rb +33 -44
- data/wicked_pdf.gemspec +6 -7
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 47fe2d6d38cc265b92a54aaba23756bd4ecfcf30
|
|
4
|
+
data.tar.gz: 914991dd1656d0fb326ebf2e38a8f013ce8357b4
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 166e1621757930e9ac10846569c28cc6e215eb4e81f2a8280789f9b2ffa8ba4d4a7fc53140f898dcb591c0890561e25e5f996af5c1f70a0ff81e4625a565ab7b
|
|
7
|
+
data.tar.gz: 58c3dfe3876a9941da002de9aca24283a217ebbc1da024f1caef83c92a9c1be845186382a2e7955d539c3e1bde73486219dd66eab26bf16d61be3c053365f119
|
data/.rubocop_todo.yml
CHANGED
|
@@ -5,13 +5,6 @@
|
|
|
5
5
|
# Note that changes in the inspected code, or installation of new
|
|
6
6
|
# versions of RuboCop, may require this file to be generated again.
|
|
7
7
|
|
|
8
|
-
# Offense count: 2
|
|
9
|
-
Lint/AmbiguousRegexpLiteral:
|
|
10
|
-
Enabled: false
|
|
11
|
-
|
|
12
|
-
# Offense count: 2
|
|
13
|
-
Lint/HandleExceptions:
|
|
14
|
-
Enabled: false
|
|
15
8
|
|
|
16
9
|
# Offense count: 9
|
|
17
10
|
Metrics/AbcSize:
|
|
@@ -44,7 +37,7 @@ Metrics/MethodLength:
|
|
|
44
37
|
Metrics/PerceivedComplexity:
|
|
45
38
|
Max: 12
|
|
46
39
|
|
|
47
|
-
# Offense count:
|
|
40
|
+
# Offense count: 2
|
|
48
41
|
Style/AccessorMethodName:
|
|
49
42
|
Enabled: false
|
|
50
43
|
|
data/.travis.yml
CHANGED
|
@@ -11,6 +11,7 @@ rvm:
|
|
|
11
11
|
- 2.0
|
|
12
12
|
- 2.1
|
|
13
13
|
- 2.2
|
|
14
|
+
- 2.3.0
|
|
14
15
|
- ruby-head
|
|
15
16
|
gemfile:
|
|
16
17
|
- gemfiles/3.0.gemfile
|
|
@@ -33,8 +34,6 @@ matrix:
|
|
|
33
34
|
- gem update --system 1.8.25
|
|
34
35
|
- gem --version
|
|
35
36
|
gemfile: gemfiles/2.3.gemfile
|
|
36
|
-
- rvm: 1.9.3
|
|
37
|
-
gemfile: gemfiles/4.0.gemfile
|
|
38
37
|
- rvm: 1.9.3
|
|
39
38
|
gemfile: gemfiles/rails_edge.gemfile
|
|
40
39
|
- rvm: 2.0.0
|
|
@@ -47,11 +46,17 @@ matrix:
|
|
|
47
46
|
gemfile: gemfiles/4.2.gemfile
|
|
48
47
|
- rvm: 2.2.0
|
|
49
48
|
gemfile: gemfiles/rails_edge.gemfile
|
|
49
|
+
- rvm: 2.3.0
|
|
50
|
+
gemfile: gemfiles/rails_edge.gemfile
|
|
50
51
|
allow_failures:
|
|
52
|
+
- rvm: 1.9.2
|
|
53
|
+
gemfile: gemfiles/3.0.gemfile
|
|
51
54
|
- rvm: 1.9.2
|
|
52
55
|
gemfile: gemfiles/3.1.gemfile
|
|
53
56
|
- rvm: 1.9.2
|
|
54
57
|
gemfile: gemfiles/3.2.gemfile
|
|
58
|
+
- rvm: 1.9.3
|
|
59
|
+
gemfile: gemfiles/4.0.gemfile
|
|
55
60
|
- rvm: 1.9.3
|
|
56
61
|
gemfile: gemfiles/rails_edge.gemfile
|
|
57
62
|
- rvm: 2.0.0
|
data/CHANGELOG.md
CHANGED
|
@@ -2,10 +2,38 @@
|
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
|
4
4
|
|
|
5
|
-
## [
|
|
5
|
+
## [1.0.5] - 2016-03-28
|
|
6
|
+
### Changed
|
|
7
|
+
- Numerous RuboCop style violation fixes, spelling errors, and test-setup issues from [indyrb.org](http://indyrb.org/) hack night. Thank you all for your contributions!
|
|
8
|
+
|
|
9
|
+
### Fixed
|
|
10
|
+
- Shellescape options. A stray quote in `header` or `footer` would cause PDF to fail to generate, and this should close down many potential attack vectors if you allow user-supplied values to be passed into `wicked_pdf` render options.
|
|
11
|
+
|
|
12
|
+
## [1.0.4] - 2016-01-26
|
|
13
|
+
### Changed
|
|
14
|
+
- Check that logger responds to info before calling it. It was possible to have a `logger` method defined as a controller helper that would override `Rails.logger`.
|
|
15
|
+
|
|
16
|
+
### Fixed
|
|
17
|
+
- [Issue with Sprockets 3.0](https://github.com/mileszs/wicked_pdf/issues/476) where an asset referenced in a stylesheet not existing would raise an exception `read_asset` on nil.
|
|
18
|
+
|
|
19
|
+
## [1.0.3] - 2015-12-02
|
|
20
|
+
### Changed
|
|
21
|
+
- Revert default DPI. Some installs of `wkhtmltopdf` would experience major slowdowns or crashes with it set to 72. It is suggested that a DPI of 75 may be better, but I'm holding off on making it a default without more information.
|
|
22
|
+
|
|
23
|
+
## [1.0.2] - 2015-11-30
|
|
6
24
|
### Changed
|
|
7
25
|
- The default dpi is now 72. Previously the default would be whatever your `wkhtmltopdf` version specified as the default. This change [speeds up generation of documents that contain `border-radius` dramatically](https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1510)
|
|
8
26
|
|
|
27
|
+
## [1.0.1] - 2015-11-19
|
|
28
|
+
### Changed
|
|
29
|
+
- Made minor RuboCop style tweaks.
|
|
30
|
+
|
|
31
|
+
### Added
|
|
32
|
+
- Added default [RuboCop](https://github.com/bbatsov/rubocop) config and run after test suite.
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
- Issue with `nil.basename` from asset helpers.
|
|
36
|
+
|
|
9
37
|
## [1.0.0] - 2015-11-03
|
|
10
38
|
### Changed
|
|
11
39
|
- Accepted that `WickedPDF` cannot guarantee backwards compatibility with older versions of `wkthmltopdf`, and decided to publish a new version with the MAJOR number incremented, signaling that this may have breaking changes for some people, but providing a path forward for progress. This release number also signals that this is a mature (and relatively stable) project, and should be deemed ready for production (since it has been used in production since ~2009, and downloaded over a *million* times on [RubyGems.org](https://rubygems.org/gems/wicked_pdf)).
|
|
@@ -37,10 +65,14 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
|
37
65
|
- Asset helpers no longer add a file extension if it already is specified with one
|
|
38
66
|
|
|
39
67
|
# Compare Releases
|
|
40
|
-
- [1.0.
|
|
41
|
-
- [1.0.
|
|
42
|
-
- [0.
|
|
43
|
-
- [0.
|
|
44
|
-
- [0.
|
|
45
|
-
- [0.
|
|
46
|
-
- [0.
|
|
68
|
+
- [1.0.5...HEAD](https://github.com/mileszs/wicked_pdf/compare/1.0.5...HEAD)
|
|
69
|
+
- [1.0.4...1.0.5](https://github.com/mileszs/wicked_pdf/compare/1.0.4...1.0.5)
|
|
70
|
+
- [1.0.3...1.0.4](https://github.com/mileszs/wicked_pdf/compare/1.0.3...1.0.4)
|
|
71
|
+
- [1.0.2...1.0.3](https://github.com/mileszs/wicked_pdf/compare/1.0.2...1.0.3)
|
|
72
|
+
- [1.0.1...1.0.2](https://github.com/mileszs/wicked_pdf/compare/1.0.1...1.0.2)
|
|
73
|
+
- [1.0.0...1.0.1](https://github.com/mileszs/wicked_pdf/compare/1.0.0...1.0.1)
|
|
74
|
+
- [0.11.0...1.0.0](https://github.com/mileszs/wicked_pdf/compare/0.11.0...1.0.0)
|
|
75
|
+
- [0.10.2...0.11.0](https://github.com/mileszs/wicked_pdf/compare/0.10.2...0.11.0)
|
|
76
|
+
- [0.10.1...0.10.2](https://github.com/mileszs/wicked_pdf/compare/0.10.1...0.10.2)
|
|
77
|
+
- [0.10.0...0.10.1](https://github.com/mileszs/wicked_pdf/compare/0.10.0...0.10.1)
|
|
78
|
+
- [0.9.10...0.10.0](https://github.com/mileszs/wicked_pdf/compare/0.9.10...0.10.0)
|
data/Rakefile
CHANGED
|
@@ -36,6 +36,7 @@ task :dummy_generate do
|
|
|
36
36
|
puts 'Creating dummy application to run tests'
|
|
37
37
|
prefix = (Rails::VERSION::MAJOR == 2) ? '' : 'new '
|
|
38
38
|
system("rails #{prefix}test/dummy")
|
|
39
|
+
system("touch #{prefix}test/dummy/db/schema.rb")
|
|
39
40
|
FileUtils.rm_r Dir.glob('test/dummy/test/*')
|
|
40
41
|
end
|
|
41
42
|
|
data/lib/wicked_pdf.rb
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
require 'logger'
|
|
5
5
|
require 'digest/md5'
|
|
6
6
|
require 'rbconfig'
|
|
7
|
+
require 'shellwords'
|
|
7
8
|
|
|
8
9
|
if (RbConfig::CONFIG['target_os'] =~ /mswin|mingw/) && (RUBY_VERSION < '1.9')
|
|
9
10
|
require 'win32/open3'
|
|
@@ -31,17 +32,18 @@ require 'wicked_pdf/middleware'
|
|
|
31
32
|
class WickedPdf
|
|
32
33
|
DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
|
|
33
34
|
BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
|
|
34
|
-
EXE_NAME = 'wkhtmltopdf'
|
|
35
|
+
EXE_NAME = 'wkhtmltopdf'.freeze
|
|
35
36
|
@@config = {}
|
|
36
37
|
cattr_accessor :config
|
|
38
|
+
attr_accessor :binary_version
|
|
37
39
|
|
|
38
40
|
def initialize(wkhtmltopdf_binary_path = nil)
|
|
39
41
|
@exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
42
|
+
raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
|
|
43
|
+
raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path)
|
|
44
|
+
raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
|
|
43
45
|
|
|
44
|
-
|
|
46
|
+
retrieve_binary_version
|
|
45
47
|
end
|
|
46
48
|
|
|
47
49
|
def pdf_from_html_file(filepath, options = {})
|
|
@@ -86,7 +88,7 @@ class WickedPdf
|
|
|
86
88
|
generated_pdf_file.rewind
|
|
87
89
|
generated_pdf_file.binmode
|
|
88
90
|
pdf = generated_pdf_file.read
|
|
89
|
-
fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.
|
|
91
|
+
fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
|
|
90
92
|
pdf
|
|
91
93
|
rescue => e
|
|
92
94
|
raise "Failed to execute:\n#{command}\nError: #{e}"
|
|
@@ -101,10 +103,6 @@ class WickedPdf
|
|
|
101
103
|
RAILS_ENV == 'development' if defined?(RAILS_ENV)
|
|
102
104
|
end
|
|
103
105
|
|
|
104
|
-
def get_binary_version
|
|
105
|
-
@binary_version
|
|
106
|
-
end
|
|
107
|
-
|
|
108
106
|
def on_windows?
|
|
109
107
|
RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
|
|
110
108
|
end
|
|
@@ -113,10 +111,11 @@ class WickedPdf
|
|
|
113
111
|
p '*' * 15 + cmd + '*' * 15
|
|
114
112
|
end
|
|
115
113
|
|
|
116
|
-
def
|
|
114
|
+
def retrieve_binary_version
|
|
117
115
|
_stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V')
|
|
118
116
|
@binary_version = parse_version(stdout.gets(nil))
|
|
119
117
|
rescue StandardError
|
|
118
|
+
DEFAULT_BINARY_VERSION
|
|
120
119
|
end
|
|
121
120
|
|
|
122
121
|
def parse_version(version_info)
|
|
@@ -163,17 +162,17 @@ class WickedPdf
|
|
|
163
162
|
return value.collect { |v| make_option(name, v, type) }
|
|
164
163
|
end
|
|
165
164
|
if type == :name_value
|
|
166
|
-
parts = value.to_s.split(' ')
|
|
165
|
+
parts = value.to_s.split(' ').map { |part| part.shellescape }
|
|
167
166
|
["--#{name.tr('_', '-')}", *parts]
|
|
168
167
|
elsif type == :boolean
|
|
169
168
|
["--#{name.tr('_', '-')}"]
|
|
170
169
|
else
|
|
171
|
-
["--#{name.tr('_', '-')}", value.to_s]
|
|
170
|
+
["--#{name.tr('_', '-')}", value.to_s.shellescape]
|
|
172
171
|
end
|
|
173
172
|
end
|
|
174
173
|
|
|
175
174
|
def valid_option(name)
|
|
176
|
-
if
|
|
175
|
+
if binary_version < BINARY_VERSION_WITHOUT_DASHES
|
|
177
176
|
"--#{name}"
|
|
178
177
|
else
|
|
179
178
|
name
|
|
@@ -198,9 +197,9 @@ class WickedPdf
|
|
|
198
197
|
[:header, :footer].collect do |hf|
|
|
199
198
|
next if options[hf].blank?
|
|
200
199
|
opt_hf = options[hf]
|
|
201
|
-
r += make_options(opt_hf, [:center, :font_name, :left, :right],
|
|
202
|
-
r += make_options(opt_hf, [:font_size, :spacing],
|
|
203
|
-
r += make_options(opt_hf, [:line],
|
|
200
|
+
r += make_options(opt_hf, [:center, :font_name, :left, :right], hf.to_s)
|
|
201
|
+
r += make_options(opt_hf, [:font_size, :spacing], hf.to_s, :numeric)
|
|
202
|
+
r += make_options(opt_hf, [:line], hf.to_s, :boolean)
|
|
204
203
|
if options[hf] && options[hf][:content]
|
|
205
204
|
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
|
|
206
205
|
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
|
|
@@ -67,8 +67,8 @@ class WickedPdf
|
|
|
67
67
|
rules.map do |pattern|
|
|
68
68
|
if pattern.is_a?(Regexp)
|
|
69
69
|
return false if @request.fullpath =~ pattern
|
|
70
|
-
|
|
71
|
-
return false
|
|
70
|
+
elsif @request.path[0, pattern.length] == pattern
|
|
71
|
+
return false
|
|
72
72
|
end
|
|
73
73
|
end
|
|
74
74
|
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
module PdfHelper
|
|
2
|
-
require 'wicked_pdf'
|
|
3
2
|
require 'tempfile'
|
|
4
3
|
|
|
5
4
|
def self.included(base)
|
|
@@ -59,8 +58,8 @@ module PdfHelper
|
|
|
59
58
|
:formats => options[:formats],
|
|
60
59
|
:handlers => options[:handlers]
|
|
61
60
|
}
|
|
62
|
-
render_opts
|
|
63
|
-
render_opts
|
|
61
|
+
render_opts[:locals] = options[:locals] if options[:locals]
|
|
62
|
+
render_opts[:file] = options[:file] if options[:file]
|
|
64
63
|
html_string = render_to_string(render_opts)
|
|
65
64
|
options = prerender_header_and_footer(options)
|
|
66
65
|
w = WickedPdf.new(options[:wkhtmltopdf])
|
|
@@ -80,8 +79,8 @@ module PdfHelper
|
|
|
80
79
|
:handlers => options[:handlers],
|
|
81
80
|
:content_type => 'text/html'
|
|
82
81
|
}
|
|
83
|
-
render_opts
|
|
84
|
-
render_opts
|
|
82
|
+
render_opts[:locals] = options[:locals] if options[:locals]
|
|
83
|
+
render_opts[:file] = options[:file] if options[:file]
|
|
85
84
|
render(render_opts)
|
|
86
85
|
else
|
|
87
86
|
pdf_content = make_pdf(options)
|
|
@@ -104,8 +103,8 @@ module PdfHelper
|
|
|
104
103
|
:formats => options[hf][:html][:formats],
|
|
105
104
|
:handlers => options[hf][:html][:handlers]
|
|
106
105
|
}
|
|
107
|
-
render_opts
|
|
108
|
-
render_opts
|
|
106
|
+
render_opts[:locals] = options[hf][:html][:locals] if options[hf][:html][:locals]
|
|
107
|
+
render_opts[:file] = options[hf][:html][:file] if options[:file]
|
|
109
108
|
tf.write render_to_string(render_opts)
|
|
110
109
|
tf.flush
|
|
111
110
|
options[hf][:html][:url] = "file:///#{tf.path}"
|
data/lib/wicked_pdf/version.rb
CHANGED
|
@@ -58,7 +58,8 @@ module WickedPdfHelper
|
|
|
58
58
|
def asset_pathname(source)
|
|
59
59
|
if precompiled_or_absolute_asset?(source)
|
|
60
60
|
asset = asset_path(source)
|
|
61
|
-
|
|
61
|
+
pathname = prepend_protocol(asset)
|
|
62
|
+
if pathname =~ URI_REGEXP
|
|
62
63
|
# asset_path returns an absolute URL using asset_host if asset_host is set
|
|
63
64
|
pathname
|
|
64
65
|
else
|
|
@@ -72,7 +73,7 @@ module WickedPdfHelper
|
|
|
72
73
|
|
|
73
74
|
# will prepend a http or default_protocol to a protocol relative URL
|
|
74
75
|
# or when no protcol is set.
|
|
75
|
-
def
|
|
76
|
+
def prepend_protocol(source)
|
|
76
77
|
protocol = WickedPdf.config[:default_protocol] || 'http'
|
|
77
78
|
if source[0, 2] == '//'
|
|
78
79
|
source = [protocol, ':', source].join
|
|
@@ -90,7 +91,8 @@ module WickedPdfHelper
|
|
|
90
91
|
|
|
91
92
|
def read_asset(source)
|
|
92
93
|
if precompiled_or_absolute_asset?(source)
|
|
93
|
-
|
|
94
|
+
pathname = asset_pathname(source)
|
|
95
|
+
if pathname =~ URI_REGEXP
|
|
94
96
|
read_from_uri(pathname)
|
|
95
97
|
elsif File.file?(pathname)
|
|
96
98
|
IO.read(pathname)
|
|
@@ -102,7 +104,7 @@ module WickedPdfHelper
|
|
|
102
104
|
|
|
103
105
|
def read_from_uri(uri)
|
|
104
106
|
encoding = ':UTF-8' if RUBY_VERSION > '1.8'
|
|
105
|
-
asset = open(uri, "r#{encoding}"
|
|
107
|
+
asset = open(uri, "r#{encoding}", &:read)
|
|
106
108
|
asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
|
|
107
109
|
asset
|
|
108
110
|
end
|
|
@@ -112,6 +114,7 @@ module WickedPdfHelper
|
|
|
112
114
|
gzipper = Zlib::GzipReader.new(stringified_asset)
|
|
113
115
|
gzipper.read
|
|
114
116
|
rescue Zlib::GzipFile::Error
|
|
117
|
+
nil
|
|
115
118
|
end
|
|
116
119
|
end
|
|
117
120
|
end
|
|
@@ -21,7 +21,7 @@ class PdfHelperTest < ActionController::TestCase
|
|
|
21
21
|
test 'should prerender header and footer :template options' do
|
|
22
22
|
options = @ac.send(:prerender_header_and_footer,
|
|
23
23
|
:header => { :html => { :template => 'hf.html.erb' } })
|
|
24
|
-
assert_match
|
|
24
|
+
assert_match %r(^file:\/\/\/.*wicked_header_pdf.*\.html), options[:header][:html][:url]
|
|
25
25
|
end
|
|
26
26
|
end
|
|
27
27
|
end
|
|
@@ -6,7 +6,7 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
|
|
|
6
6
|
|
|
7
7
|
if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
|
|
8
8
|
test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
|
|
9
|
-
assert_match
|
|
9
|
+
assert_match %r(data:text\/css;base64,.+), wicked_pdf_asset_base64('wicked.css')
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
test 'wicked_pdf_asset_path should return a url when assets are served by an asset server' do
|
|
@@ -95,11 +95,11 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
|
|
|
95
95
|
assert_no_match WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
|
|
96
96
|
end
|
|
97
97
|
|
|
98
|
-
test '
|
|
99
|
-
assert_equal 'http://assets.domain.com/dummy.png',
|
|
100
|
-
assert_equal '/assets.domain.com/dummy.png',
|
|
101
|
-
assert_equal 'http://assets.domain.com/dummy.png',
|
|
102
|
-
assert_equal 'https://assets.domain.com/dummy.png',
|
|
98
|
+
test 'prepend_protocol should properly set the protocol when the asset is precompiled' do
|
|
99
|
+
assert_equal 'http://assets.domain.com/dummy.png', prepend_protocol('//assets.domain.com/dummy.png')
|
|
100
|
+
assert_equal '/assets.domain.com/dummy.png', prepend_protocol('/assets.domain.com/dummy.png')
|
|
101
|
+
assert_equal 'http://assets.domain.com/dummy.png', prepend_protocol('http://assets.domain.com/dummy.png')
|
|
102
|
+
assert_equal 'https://assets.domain.com/dummy.png', prepend_protocol('https://assets.domain.com/dummy.png')
|
|
103
103
|
end
|
|
104
104
|
end
|
|
105
105
|
end
|
data/test/test_helper.rb
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
require 'test_helper'
|
|
2
2
|
|
|
3
3
|
WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' }
|
|
4
|
-
HTML_DOCUMENT = '<html><body>Hello World</body></html>'
|
|
4
|
+
HTML_DOCUMENT = '<html><body>Hello World</body></html>'.freeze
|
|
5
5
|
|
|
6
6
|
# Provide a public accessor to the normally-private parse_options function.
|
|
7
7
|
# Also, smash the returned array of options into a single string for
|
|
8
8
|
# convenience in testing below.
|
|
9
9
|
class WickedPdf
|
|
10
|
+
attr_accessor :binary_version
|
|
10
11
|
def get_parsed_options(opts)
|
|
11
12
|
parse_options(opts).join(' ')
|
|
12
13
|
end
|
|
@@ -14,10 +15,6 @@ class WickedPdf
|
|
|
14
15
|
def get_valid_option(name)
|
|
15
16
|
valid_option(name)
|
|
16
17
|
end
|
|
17
|
-
|
|
18
|
-
def set_binary_version_to(version)
|
|
19
|
-
@binary_version = version
|
|
20
|
-
end
|
|
21
18
|
end
|
|
22
19
|
|
|
23
20
|
class WickedPdfTest < ActiveSupport::TestCase
|
|
@@ -26,26 +23,23 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
26
23
|
end
|
|
27
24
|
|
|
28
25
|
test 'should generate PDF from html document' do
|
|
29
|
-
|
|
30
|
-
pdf = wp.pdf_from_string HTML_DOCUMENT
|
|
26
|
+
pdf = @wp.pdf_from_string HTML_DOCUMENT
|
|
31
27
|
assert pdf.start_with?('%PDF-1.4')
|
|
32
28
|
assert pdf.rstrip.end_with?('%%EOF')
|
|
33
29
|
assert pdf.length > 100
|
|
34
30
|
end
|
|
35
31
|
|
|
36
32
|
test 'should generate PDF from html document with long lines' do
|
|
37
|
-
wp = WickedPdf.new
|
|
38
33
|
document_with_long_line_file = File.new('test/fixtures/document_with_long_line.html', 'r')
|
|
39
|
-
pdf = wp.pdf_from_string(document_with_long_line_file.read)
|
|
34
|
+
pdf = @wp.pdf_from_string(document_with_long_line_file.read)
|
|
40
35
|
assert pdf.start_with?('%PDF-1.4')
|
|
41
36
|
assert pdf.rstrip.end_with?('%%EOF')
|
|
42
37
|
assert pdf.length > 100
|
|
43
38
|
end
|
|
44
39
|
|
|
45
40
|
test 'should generate PDF from html existing HTML file without converting it to string' do
|
|
46
|
-
wp = WickedPdf.new
|
|
47
41
|
filepath = File.join(Dir.pwd, 'test/fixtures/document_with_long_line.html')
|
|
48
|
-
pdf = wp.pdf_from_html_file(filepath)
|
|
42
|
+
pdf = @wp.pdf_from_html_file(filepath)
|
|
49
43
|
assert pdf.start_with?('%PDF-1.4')
|
|
50
44
|
assert pdf.rstrip.end_with?('%%EOF')
|
|
51
45
|
assert pdf.length > 100
|
|
@@ -91,33 +85,30 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
91
85
|
end
|
|
92
86
|
|
|
93
87
|
test 'should parse header and footer options' do
|
|
94
|
-
wp = WickedPdf.new
|
|
95
|
-
|
|
96
88
|
[:header, :footer].each do |hf|
|
|
97
89
|
[:center, :font_name, :left, :right].each do |o|
|
|
98
90
|
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} header_footer",
|
|
99
|
-
wp.get_parsed_options(hf => { o => 'header_footer' }).strip
|
|
91
|
+
@wp.get_parsed_options(hf => { o => 'header_footer' }).strip
|
|
100
92
|
end
|
|
101
93
|
|
|
102
94
|
[:font_size, :spacing].each do |o|
|
|
103
95
|
assert_equal "--#{hf}-#{o.to_s.tr('_', '-')} 12",
|
|
104
|
-
wp.get_parsed_options(hf => { o => '12' }).strip
|
|
96
|
+
@wp.get_parsed_options(hf => { o => '12' }).strip
|
|
105
97
|
end
|
|
106
98
|
|
|
107
99
|
assert_equal "--#{hf}-line",
|
|
108
|
-
wp.get_parsed_options(hf => { :line => true }).strip
|
|
100
|
+
@wp.get_parsed_options(hf => { :line => true }).strip
|
|
109
101
|
assert_equal "--#{hf}-html http://www.abc.com",
|
|
110
|
-
wp.get_parsed_options(hf => { :html => { :url => 'http://www.abc.com' } }).strip
|
|
102
|
+
@wp.get_parsed_options(hf => { :html => { :url => 'http://www.abc.com' } }).strip
|
|
111
103
|
end
|
|
112
104
|
end
|
|
113
105
|
|
|
114
106
|
test 'should parse toc options' do
|
|
115
|
-
|
|
116
|
-
toc_option = wp.get_valid_option('toc')
|
|
107
|
+
toc_option = @wp.get_valid_option('toc')
|
|
117
108
|
|
|
118
109
|
[:font_name, :header_text].each do |o|
|
|
119
110
|
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} toc",
|
|
120
|
-
wp.get_parsed_options(:toc => { o => 'toc' }).strip
|
|
111
|
+
@wp.get_parsed_options(:toc => { o => 'toc' }).strip
|
|
121
112
|
end
|
|
122
113
|
|
|
123
114
|
[
|
|
@@ -126,59 +117,52 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
126
117
|
:l3_indentation, :l4_indentation, :l5_indentation, :l6_indentation, :l7_indentation
|
|
127
118
|
].each do |o|
|
|
128
119
|
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')} 5",
|
|
129
|
-
wp.get_parsed_options(:toc => { o => 5 }).strip
|
|
120
|
+
@wp.get_parsed_options(:toc => { o => 5 }).strip
|
|
130
121
|
end
|
|
131
122
|
|
|
132
123
|
[:no_dots, :disable_links, :disable_back_links].each do |o|
|
|
133
124
|
assert_equal "#{toc_option} --toc-#{o.to_s.tr('_', '-')}",
|
|
134
|
-
wp.get_parsed_options(:toc => { o => true }).strip
|
|
125
|
+
@wp.get_parsed_options(:toc => { o => true }).strip
|
|
135
126
|
end
|
|
136
127
|
end
|
|
137
128
|
|
|
138
129
|
test 'should parse outline options' do
|
|
139
|
-
wp
|
|
140
|
-
|
|
141
|
-
assert_equal '--outline', wp.get_parsed_options(:outline => { :outline => true }).strip
|
|
142
|
-
assert_equal '--outline-depth 5', wp.get_parsed_options(:outline => { :outline_depth => 5 }).strip
|
|
130
|
+
assert_equal '--outline', @wp.get_parsed_options(:outline => { :outline => true }).strip
|
|
131
|
+
assert_equal '--outline-depth 5', @wp.get_parsed_options(:outline => { :outline_depth => 5 }).strip
|
|
143
132
|
end
|
|
144
133
|
|
|
145
134
|
test 'should parse margins options' do
|
|
146
|
-
wp = WickedPdf.new
|
|
147
|
-
|
|
148
135
|
[:top, :bottom, :left, :right].each do |o|
|
|
149
|
-
assert_equal "--margin-#{o} 12", wp.get_parsed_options(:margin => { o => '12' }).strip
|
|
136
|
+
assert_equal "--margin-#{o} 12", @wp.get_parsed_options(:margin => { o => '12' }).strip
|
|
150
137
|
end
|
|
151
138
|
end
|
|
152
139
|
|
|
153
140
|
test 'should parse cover' do
|
|
154
|
-
|
|
155
|
-
cover_option = wp.get_valid_option('cover')
|
|
141
|
+
cover_option = @wp.get_valid_option('cover')
|
|
156
142
|
|
|
157
143
|
pathname = Rails.root.join('app', 'views', 'pdf', 'file.html')
|
|
158
|
-
assert_equal "#{cover_option} http://example.org", wp.get_parsed_options(:cover => 'http://example.org').strip, 'URL'
|
|
159
|
-
assert_equal "#{cover_option} #{pathname}", wp.get_parsed_options(:cover => pathname).strip, 'Pathname'
|
|
160
|
-
assert_match
|
|
144
|
+
assert_equal "#{cover_option} http://example.org", @wp.get_parsed_options(:cover => 'http://example.org').strip, 'URL'
|
|
145
|
+
assert_equal "#{cover_option} #{pathname}", @wp.get_parsed_options(:cover => pathname).strip, 'Pathname'
|
|
146
|
+
assert_match %r(#{cover_option} .+wicked_cover_pdf.+\.html), @wp.get_parsed_options(:cover => '<html><body>HELLO</body></html>').strip, 'HTML'
|
|
161
147
|
end
|
|
162
148
|
|
|
163
149
|
test 'should parse other options' do
|
|
164
|
-
wp = WickedPdf.new
|
|
165
|
-
|
|
166
150
|
[
|
|
167
151
|
:orientation, :page_size, :proxy, :username, :password, :dpi,
|
|
168
152
|
:encoding, :user_style_sheet
|
|
169
153
|
].each do |o|
|
|
170
|
-
assert_equal "--#{o.to_s.tr('_', '-')} opts", wp.get_parsed_options(o => 'opts').strip
|
|
154
|
+
assert_equal "--#{o.to_s.tr('_', '-')} opts", @wp.get_parsed_options(o => 'opts').strip
|
|
171
155
|
end
|
|
172
156
|
|
|
173
157
|
[:cookie, :post].each do |o|
|
|
174
|
-
assert_equal "--#{o.to_s.tr('_', '-')} name value", wp.get_parsed_options(o => 'name value').strip
|
|
158
|
+
assert_equal "--#{o.to_s.tr('_', '-')} name value", @wp.get_parsed_options(o => 'name value').strip
|
|
175
159
|
|
|
176
160
|
nv_formatter = proc { |number| "--#{o.to_s.tr('_', '-')} par#{number} val#{number}" }
|
|
177
|
-
assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", wp.get_parsed_options(o => ['par1 val1', 'par2 val2']).strip
|
|
161
|
+
assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", @wp.get_parsed_options(o => ['par1 val1', 'par2 val2']).strip
|
|
178
162
|
end
|
|
179
163
|
|
|
180
164
|
[:redirect_delay, :zoom, :page_offset].each do |o|
|
|
181
|
-
assert_equal "--#{o.to_s.tr('_', '-')} 5", wp.get_parsed_options(o => 5).strip
|
|
165
|
+
assert_equal "--#{o.to_s.tr('_', '-')} 5", @wp.get_parsed_options(o => 5).strip
|
|
182
166
|
end
|
|
183
167
|
|
|
184
168
|
[
|
|
@@ -186,7 +170,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
186
170
|
:enable_plugins, :disable_internal_links, :disable_external_links,
|
|
187
171
|
:print_media_type, :disable_smart_shrinking, :use_xserver, :no_background
|
|
188
172
|
].each do |o|
|
|
189
|
-
assert_equal "--#{o.to_s.tr('_', '-')}", wp.get_parsed_options(o => true).strip
|
|
173
|
+
assert_equal "--#{o.to_s.tr('_', '-')}", @wp.get_parsed_options(o => true).strip
|
|
190
174
|
end
|
|
191
175
|
end
|
|
192
176
|
|
|
@@ -210,11 +194,11 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
210
194
|
end
|
|
211
195
|
|
|
212
196
|
test 'should set version on initialize' do
|
|
213
|
-
assert_not_equal @wp.send(:
|
|
197
|
+
assert_not_equal @wp.send(:binary_version), ''
|
|
214
198
|
end
|
|
215
199
|
|
|
216
200
|
test 'should not use double dash options for version without dashes' do
|
|
217
|
-
@wp.
|
|
201
|
+
@wp.binary_version = WickedPdf::BINARY_VERSION_WITHOUT_DASHES
|
|
218
202
|
|
|
219
203
|
%w(toc cover).each do |name|
|
|
220
204
|
assert_equal @wp.get_valid_option(name), name
|
|
@@ -222,7 +206,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
222
206
|
end
|
|
223
207
|
|
|
224
208
|
test 'should use double dash options for version with dashes' do
|
|
225
|
-
@wp.
|
|
209
|
+
@wp.binary_version = Gem::Version.new('0.11.0')
|
|
226
210
|
|
|
227
211
|
%w(toc cover).each do |name|
|
|
228
212
|
assert_equal @wp.get_valid_option(name), "--#{name}"
|
|
@@ -234,4 +218,9 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
234
218
|
cover_option = @wp.get_valid_option('cover')
|
|
235
219
|
assert_equal @wp.get_parsed_options(options), "--disable-javascript --header-center 3 #{cover_option} http://example.org"
|
|
236
220
|
end
|
|
221
|
+
|
|
222
|
+
test 'options are shellescaped' do
|
|
223
|
+
options = { :header => { :left => "That's crazy" } }
|
|
224
|
+
assert_equal "--header-left That\\'s\\ crazy", @wp.get_parsed_options(options)
|
|
225
|
+
end
|
|
237
226
|
end
|
data/wicked_pdf.gemspec
CHANGED
|
@@ -12,8 +12,13 @@ Gem::Specification.new do |spec|
|
|
|
12
12
|
spec.homepage = 'https://github.com/mileszs/wicked_pdf'
|
|
13
13
|
spec.license = 'MIT'
|
|
14
14
|
spec.date = Time.now.strftime('%Y-%m-%d')
|
|
15
|
+
spec.description = <<desc
|
|
16
|
+
Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
|
|
17
|
+
In other words, rather than dealing with a PDF generation DSL of some sort,
|
|
18
|
+
you simply write an HTML view as you would normally, and let Wicked take care of the hard stuff.
|
|
19
|
+
desc
|
|
15
20
|
|
|
16
|
-
spec.files = `git ls-files`.split(
|
|
21
|
+
spec.files = `git ls-files`.split($/)
|
|
17
22
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
23
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
24
|
spec.require_paths = ['lib']
|
|
@@ -25,10 +30,4 @@ Gem::Specification.new do |spec|
|
|
|
25
30
|
spec.add_development_dependency 'sqlite3'
|
|
26
31
|
spec.add_development_dependency 'mocha'
|
|
27
32
|
spec.add_development_dependency 'test-unit'
|
|
28
|
-
|
|
29
|
-
spec.description = <<desc
|
|
30
|
-
Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
|
|
31
|
-
In other words, rather than dealing with a PDF generation DSL of some sort,
|
|
32
|
-
you simply write an HTML view as you would normally, and let Wicked take care of the hard stuff.
|
|
33
|
-
desc
|
|
34
33
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wicked_pdf
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.5
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Miles Z. Sterrett
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2016-
|
|
11
|
+
date: 2016-03-28 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|