wicked_pdf 1.0.6 → 1.1.0
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 +4 -1
- data/.travis.yml +4 -13
- data/CHANGELOG.md +13 -1
- data/README.md +64 -11
- data/Rakefile +4 -4
- data/gemfiles/2.3.gemfile +1 -1
- data/gemfiles/3.0.gemfile +6 -1
- data/gemfiles/3.1.gemfile +6 -1
- data/gemfiles/3.2.gemfile +6 -1
- data/gemfiles/4.0.gemfile +1 -1
- data/gemfiles/4.1.gemfile +1 -1
- data/gemfiles/4.2.gemfile +1 -1
- data/gemfiles/5.0.gemfile +6 -0
- data/gemfiles/rails_edge.gemfile +1 -1
- data/lib/wicked_pdf.rb +11 -8
- data/lib/wicked_pdf/pdf_helper.rb +111 -90
- data/lib/wicked_pdf/railtie.rb +35 -24
- data/lib/wicked_pdf/tempfile.rb +9 -7
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper.rb +28 -26
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +106 -96
- data/test/fixtures/wicked.js +1 -0
- data/test/functional/pdf_helper_test.rb +4 -6
- data/test/functional/wicked_pdf_helper_assets_test.rb +27 -9
- data/test/functional/wicked_pdf_helper_test.rb +15 -13
- data/test/test_helper.rb +6 -0
- data/test/unit/wicked_pdf_test.rb +3 -3
- data/wicked_pdf.gemspec +3 -2
- metadata +6 -4
data/lib/wicked_pdf/railtie.rb
CHANGED
@@ -2,43 +2,54 @@ require 'wicked_pdf/pdf_helper'
|
|
2
2
|
require 'wicked_pdf/wicked_pdf_helper'
|
3
3
|
require 'wicked_pdf/wicked_pdf_helper/assets'
|
4
4
|
|
5
|
-
|
5
|
+
class WickedPdf
|
6
|
+
if defined?(Rails)
|
6
7
|
|
7
|
-
|
8
|
+
if Rails::VERSION::MAJOR >= 5
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
|
10
|
+
class WickedRailtie < Rails::Railtie
|
11
|
+
initializer 'wicked_pdf.register' do |_app|
|
12
|
+
ActionController::Base.send :prepend, PdfHelper
|
13
13
|
ActionView::Base.send :include, WickedPdfHelper::Assets
|
14
|
-
else
|
15
|
-
ActionView::Base.send :include, WickedPdfHelper
|
16
14
|
end
|
17
15
|
end
|
18
|
-
end
|
19
16
|
|
20
|
-
|
17
|
+
elsif Rails::VERSION::MAJOR == 4
|
21
18
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
19
|
+
class WickedRailtie < Rails::Railtie
|
20
|
+
initializer 'wicked_pdf.register' do |_app|
|
21
|
+
ActionController::Base.send :include, PdfHelper
|
22
|
+
ActionView::Base.send :include, WickedPdfHelper::Assets
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
elsif Rails::VERSION::MAJOR == 3
|
28
27
|
|
29
|
-
|
28
|
+
class WickedRailtie < Rails::Railtie
|
29
|
+
initializer 'wicked_pdf.register' do |_app|
|
30
|
+
ActionController::Base.send :include, PdfHelper
|
31
|
+
if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
|
32
|
+
ActionView::Base.send :include, WickedPdfHelper::Assets
|
33
|
+
else
|
34
|
+
ActionView::Base.send :include, WickedPdfHelper
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
30
38
|
|
31
|
-
|
32
|
-
|
39
|
+
elsif Rails::VERSION::MAJOR == 2
|
40
|
+
|
41
|
+
unless ActionController::Base.instance_methods.include? 'render_with_wicked_pdf'
|
33
42
|
ActionController::Base.send :include, PdfHelper
|
34
|
-
ActionView::Base.send :include, WickedPdfHelper::Assets
|
35
43
|
end
|
44
|
+
unless ActionView::Base.instance_methods.include? 'wicked_pdf_stylesheet_link_tag'
|
45
|
+
ActionView::Base.send :include, WickedPdfHelper
|
46
|
+
end
|
47
|
+
|
36
48
|
end
|
37
49
|
|
38
|
-
|
50
|
+
if Mime::Type.lookup_by_extension(:pdf).nil?
|
51
|
+
Mime::Type.register('application/pdf', :pdf)
|
52
|
+
end
|
39
53
|
|
40
|
-
if Mime::Type.lookup_by_extension(:pdf).nil?
|
41
|
-
Mime::Type.register('application/pdf', :pdf)
|
42
54
|
end
|
43
|
-
|
44
55
|
end
|
data/lib/wicked_pdf/tempfile.rb
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'tempfile'
|
2
2
|
|
3
|
-
class
|
4
|
-
|
5
|
-
|
6
|
-
temp_dir
|
7
|
-
|
8
|
-
|
9
|
-
|
3
|
+
class WickedPdf
|
4
|
+
class WickedPdfTempfile < Tempfile
|
5
|
+
# ensures the Tempfile's filename always keeps its extension
|
6
|
+
def initialize(filename, temp_dir = nil)
|
7
|
+
temp_dir ||= Dir.tmpdir
|
8
|
+
extension = File.extname(filename)
|
9
|
+
basename = File.basename(filename, extension)
|
10
|
+
super([basename, extension], temp_dir)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
end
|
data/lib/wicked_pdf/version.rb
CHANGED
@@ -1,33 +1,35 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
class WickedPdf
|
2
|
+
module WickedPdfHelper
|
3
|
+
def self.root_path
|
4
|
+
String === Rails.root ? Pathname.new(Rails.root) : Rails.root
|
5
|
+
end
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
7
|
+
def self.add_extension(filename, extension)
|
8
|
+
filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
11
|
+
def wicked_pdf_stylesheet_link_tag(*sources)
|
12
|
+
css_dir = WickedPdfHelper.root_path.join('public', 'stylesheets')
|
13
|
+
css_text = sources.collect do |source|
|
14
|
+
source = WickedPdfHelper.add_extension(source, 'css')
|
15
|
+
"<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
|
16
|
+
end.join("\n")
|
17
|
+
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
def wicked_pdf_image_tag(img, options = {})
|
21
|
+
image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
|
22
|
+
end
|
22
23
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
24
|
+
def wicked_pdf_javascript_src_tag(jsfile, options = {})
|
25
|
+
jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
|
26
|
+
src = "file:///#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}"
|
27
|
+
content_tag('script', '', { 'type' => Mime::JS, 'src' => path_to_javascript(src) }.merge(options))
|
28
|
+
end
|
28
29
|
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
def wicked_pdf_javascript_include_tag(*sources)
|
31
|
+
js_text = sources.collect { |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
|
32
|
+
js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
|
33
|
+
end
|
32
34
|
end
|
33
35
|
end
|
@@ -1,120 +1,130 @@
|
|
1
1
|
require 'open-uri'
|
2
2
|
|
3
|
-
|
4
|
-
module
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
def wicked_pdf_stylesheet_link_tag(*sources)
|
15
|
-
stylesheet_contents = sources.collect do |source|
|
16
|
-
source = WickedPdfHelper.add_extension(source, 'css')
|
17
|
-
"<style type='text/css'>#{read_asset(source)}</style>"
|
18
|
-
end.join("\n")
|
19
|
-
|
20
|
-
stylesheet_contents.gsub(ASSET_URL_REGEX) do
|
21
|
-
if Regexp.last_match[1].starts_with?('data:')
|
22
|
-
"url(#{Regexp.last_match[1]})"
|
23
|
-
else
|
24
|
-
"url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
|
25
|
-
end
|
26
|
-
end.html_safe
|
27
|
-
end
|
3
|
+
class WickedPdf
|
4
|
+
module WickedPdfHelper
|
5
|
+
module Assets
|
6
|
+
ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
|
7
|
+
|
8
|
+
def wicked_pdf_asset_base64(path)
|
9
|
+
asset = find_asset(path)
|
10
|
+
raise "Could not find asset '#{path}'" if asset.nil?
|
11
|
+
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
|
12
|
+
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
|
13
|
+
end
|
28
14
|
|
29
|
-
|
30
|
-
|
31
|
-
|
15
|
+
def wicked_pdf_stylesheet_link_tag(*sources)
|
16
|
+
stylesheet_contents = sources.collect do |source|
|
17
|
+
source = WickedPdfHelper.add_extension(source, 'css')
|
18
|
+
"<style type='text/css'>#{read_asset(source)}</style>"
|
19
|
+
end.join("\n")
|
20
|
+
|
21
|
+
stylesheet_contents.gsub(ASSET_URL_REGEX) do
|
22
|
+
if Regexp.last_match[1].starts_with?('data:')
|
23
|
+
"url(#{Regexp.last_match[1]})"
|
24
|
+
else
|
25
|
+
"url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
|
26
|
+
end
|
27
|
+
end.html_safe
|
28
|
+
end
|
32
29
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
30
|
+
def wicked_pdf_image_tag(img, options = {})
|
31
|
+
image_tag wicked_pdf_asset_path(img), options
|
32
|
+
end
|
37
33
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
end.join("\n").html_safe
|
43
|
-
end
|
34
|
+
def wicked_pdf_javascript_src_tag(jsfile, options = {})
|
35
|
+
jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
|
36
|
+
javascript_include_tag wicked_pdf_asset_path(jsfile), options
|
37
|
+
end
|
44
38
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
"
|
39
|
+
def wicked_pdf_javascript_include_tag(*sources)
|
40
|
+
sources.collect do |source|
|
41
|
+
source = WickedPdfHelper.add_extension(source, 'js')
|
42
|
+
"<script type='text/javascript'>#{read_asset(source)}</script>"
|
43
|
+
end.join("\n").html_safe
|
50
44
|
end
|
51
|
-
end
|
52
45
|
|
53
|
-
|
46
|
+
def wicked_pdf_asset_path(asset)
|
47
|
+
if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
|
48
|
+
pathname
|
49
|
+
else
|
50
|
+
"file:///#{pathname}"
|
51
|
+
end
|
52
|
+
end
|
54
53
|
|
55
|
-
|
56
|
-
|
54
|
+
private
|
55
|
+
|
56
|
+
# borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
|
57
|
+
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
|
58
|
+
|
59
|
+
def asset_pathname(source)
|
60
|
+
if precompiled_or_absolute_asset?(source)
|
61
|
+
asset = asset_path(source)
|
62
|
+
pathname = prepend_protocol(asset)
|
63
|
+
if pathname =~ URI_REGEXP
|
64
|
+
# asset_path returns an absolute URL using asset_host if asset_host is set
|
65
|
+
pathname
|
66
|
+
else
|
67
|
+
File.join(Rails.public_path, asset.sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
|
68
|
+
end
|
69
|
+
else
|
70
|
+
asset = find_asset(source)
|
71
|
+
asset ? asset.pathname : File.join(Rails.public_path, source)
|
72
|
+
end
|
73
|
+
end
|
57
74
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
pathname = prepend_protocol(asset)
|
62
|
-
if pathname =~ URI_REGEXP
|
63
|
-
# asset_path returns an absolute URL using asset_host if asset_host is set
|
64
|
-
pathname
|
75
|
+
def find_asset(path)
|
76
|
+
if Rails.application.assets.respond_to?(:find_asset)
|
77
|
+
Rails.application.assets.find_asset(path)
|
65
78
|
else
|
66
|
-
|
79
|
+
Sprockets::Railtie.build_environment(Rails.application).find_asset(path)
|
67
80
|
end
|
68
|
-
else
|
69
|
-
asset = Rails.application.assets.find_asset(source)
|
70
|
-
asset ? asset.pathname : File.join(Rails.public_path, source)
|
71
81
|
end
|
72
|
-
end
|
73
82
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
83
|
+
# will prepend a http or default_protocol to a protocol relative URL
|
84
|
+
# or when no protcol is set.
|
85
|
+
def prepend_protocol(source)
|
86
|
+
protocol = WickedPdf.config[:default_protocol] || 'http'
|
87
|
+
if source[0, 2] == '//'
|
88
|
+
source = [protocol, ':', source].join
|
89
|
+
elsif source[0] != '/' && !source[0, 8].include?('://')
|
90
|
+
source = [protocol, '://', source].join
|
91
|
+
end
|
92
|
+
source
|
82
93
|
end
|
83
|
-
source
|
84
|
-
end
|
85
94
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
95
|
+
def precompiled_or_absolute_asset?(source)
|
96
|
+
Rails.configuration.assets.compile == false ||
|
97
|
+
source.to_s[0] == '/' ||
|
98
|
+
source.to_s.match(/\Ahttps?\:\/\//)
|
99
|
+
end
|
91
100
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
101
|
+
def read_asset(source)
|
102
|
+
if precompiled_or_absolute_asset?(source)
|
103
|
+
pathname = asset_pathname(source)
|
104
|
+
if pathname =~ URI_REGEXP
|
105
|
+
read_from_uri(pathname)
|
106
|
+
elsif File.file?(pathname)
|
107
|
+
IO.read(pathname)
|
108
|
+
end
|
109
|
+
else
|
110
|
+
find_asset(source).to_s
|
99
111
|
end
|
100
|
-
else
|
101
|
-
Rails.application.assets.find_asset(source).to_s
|
102
112
|
end
|
103
|
-
end
|
104
113
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
114
|
+
def read_from_uri(uri)
|
115
|
+
encoding = ':UTF-8' if RUBY_VERSION > '1.8'
|
116
|
+
asset = open(uri, "r#{encoding}", &:read)
|
117
|
+
asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
|
118
|
+
asset
|
119
|
+
end
|
111
120
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
121
|
+
def gzip(asset)
|
122
|
+
stringified_asset = StringIO.new(asset)
|
123
|
+
gzipper = Zlib::GzipReader.new(stringified_asset)
|
124
|
+
gzipper.read
|
125
|
+
rescue Zlib::GzipFile::Error
|
126
|
+
nil
|
127
|
+
end
|
118
128
|
end
|
119
129
|
end
|
120
130
|
end
|
@@ -0,0 +1 @@
|
|
1
|
+
// Wicked js
|
@@ -17,11 +17,9 @@ class PdfHelperTest < ActionController::TestCase
|
|
17
17
|
@ac = nil
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
assert_match %r(^file:\/\/\/.*wicked_header_pdf.*\.html), options[:header][:html][:url]
|
25
|
-
end
|
20
|
+
test 'should prerender header and footer :template options' do
|
21
|
+
options = @ac.send(:prerender_header_and_footer,
|
22
|
+
:header => { :html => { :template => 'hf.html.erb' } })
|
23
|
+
assert_match %r{^file:\/\/\/.*wicked_header_pdf.*\.html}, options[:header][:html][:url]
|
26
24
|
end
|
27
25
|
end
|
@@ -2,11 +2,29 @@ require 'test_helper'
|
|
2
2
|
require 'action_view/test_case'
|
3
3
|
|
4
4
|
class WickedPdfHelperAssetsTest < ActionView::TestCase
|
5
|
-
include WickedPdfHelper::Assets
|
5
|
+
include WickedPdf::WickedPdfHelper::Assets
|
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 %r
|
9
|
+
assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
|
10
|
+
end
|
11
|
+
|
12
|
+
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
|
13
|
+
Rails.configuration.assets.expects(:compile => true)
|
14
|
+
assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
|
15
|
+
wicked_pdf_stylesheet_link_tag('wicked')
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
|
19
|
+
Rails.configuration.assets.expects(:compile => true)
|
20
|
+
assert_equal image_tag("file:///#{Rails.root.join('public', 'pdf')}"),
|
21
|
+
wicked_pdf_image_tag('pdf')
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'wicked_pdf_javascript_include_tag should inline the javascripts passed in' do
|
25
|
+
Rails.configuration.assets.expects(:compile => true)
|
26
|
+
assert_equal "<script type='text/javascript'>// Wicked js\n;\n</script>",
|
27
|
+
wicked_pdf_javascript_include_tag('wicked')
|
10
28
|
end
|
11
29
|
|
12
30
|
test 'wicked_pdf_asset_path should return a url when assets are served by an asset server' do
|
@@ -86,13 +104,13 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
|
|
86
104
|
end
|
87
105
|
|
88
106
|
test 'WickedPdfHelper::Assets::ASSET_URL_REGEX should match various URL data type formats' do
|
89
|
-
assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
|
90
|
-
assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
|
91
|
-
assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
|
92
|
-
assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
|
93
|
-
assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
|
94
|
-
assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
|
95
|
-
assert_no_match WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
|
107
|
+
assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
|
108
|
+
assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
|
109
|
+
assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
|
110
|
+
assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
|
111
|
+
assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
|
112
|
+
assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
|
113
|
+
assert_no_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
|
96
114
|
end
|
97
115
|
|
98
116
|
test 'prepend_protocol should properly set the protocol when the asset is precompiled' do
|