wicked_pdf 2.0.2 → 2.6.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 +55 -1
- data/.rubocop_todo.yml +7 -86
- data/CHANGELOG.md +70 -4
- data/README.md +51 -11
- 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 +12 -0
- data/gemfiles/7.0.gemfile +12 -0
- data/generators/wicked_pdf/templates/wicked_pdf.rb +9 -0
- data/lib/wicked_pdf/binary.rb +65 -0
- data/lib/wicked_pdf/option_parser.rb +229 -0
- data/lib/wicked_pdf/pdf_helper.rb +29 -44
- data/lib/wicked_pdf/railtie.rb +3 -12
- data/lib/wicked_pdf/tempfile.rb +32 -3
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +62 -6
- data/lib/wicked_pdf.rb +19 -257
- data/test/functional/pdf_helper_test.rb +1 -1
- data/test/unit/wicked_pdf_binary_test.rb +26 -0
- data/test/unit/wicked_pdf_option_parser_test.rb +128 -0
- data/test/unit/wicked_pdf_test.rb +0 -157
- data/test/unit/wkhtmltopdf_location_test.rb +0 -2
- data/wicked_pdf.gemspec +3 -4
- metadata +33 -29
- 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
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
class WickedPdf
|
|
2
|
+
class Binary
|
|
3
|
+
EXE_NAME = 'wkhtmltopdf'.freeze
|
|
4
|
+
|
|
5
|
+
attr_reader :path, :default_version
|
|
6
|
+
|
|
7
|
+
def initialize(binary_path, default_version = WickedPdf::DEFAULT_BINARY_VERSION)
|
|
8
|
+
@path = binary_path || find_binary_path
|
|
9
|
+
@default_version = default_version
|
|
10
|
+
|
|
11
|
+
raise "Location of #{EXE_NAME} unknown" if @path.empty?
|
|
12
|
+
raise "Bad #{EXE_NAME}'s path: #{@path}" unless File.exist?(@path)
|
|
13
|
+
raise "#{EXE_NAME} is not executable" unless File.executable?(@path)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def version
|
|
17
|
+
@version ||= retrieve_binary_version
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def parse_version_string(version_info)
|
|
21
|
+
match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
|
|
22
|
+
if match_data && (match_data.length == 2)
|
|
23
|
+
Gem::Version.new(match_data[1])
|
|
24
|
+
else
|
|
25
|
+
default_version
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def xvfb_run_path
|
|
30
|
+
path = possible_binary_locations.map { |l| File.expand_path("#{l}/xvfb-run") }.find { |location| File.exist?(location) }
|
|
31
|
+
raise StandardError, 'Could not find binary xvfb-run on the system.' unless path
|
|
32
|
+
|
|
33
|
+
path
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
def retrieve_binary_version
|
|
39
|
+
_stdin, stdout, _stderr = Open3.popen3(@path + ' -V')
|
|
40
|
+
parse_version_string(stdout.gets(nil))
|
|
41
|
+
rescue StandardError
|
|
42
|
+
default_version
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def find_binary_path
|
|
46
|
+
exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
|
|
47
|
+
exe_path ||= possible_which_path
|
|
48
|
+
exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
|
|
49
|
+
exe_path || ''
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def possible_which_path
|
|
53
|
+
detected_path = (defined?(Bundler) ? Bundler.which('wkhtmltopdf') : `which wkhtmltopdf`).chomp
|
|
54
|
+
detected_path.present? && detected_path
|
|
55
|
+
rescue StandardError
|
|
56
|
+
nil
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def possible_binary_locations
|
|
60
|
+
possible_locations = (ENV['PATH'].split(':') + %w[/usr/bin /usr/local/bin]).uniq
|
|
61
|
+
possible_locations += %w[~/bin] if ENV.key?('HOME')
|
|
62
|
+
possible_locations
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
end
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
class WickedPdf
|
|
2
|
+
class OptionParser
|
|
3
|
+
BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
|
|
4
|
+
|
|
5
|
+
attr_reader :binary_version
|
|
6
|
+
|
|
7
|
+
def initialize(binary_version = WickedPdf::DEFAULT_BINARY_VERSION)
|
|
8
|
+
@binary_version = binary_version
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def parse(options)
|
|
12
|
+
[
|
|
13
|
+
parse_extra(options),
|
|
14
|
+
parse_others(options),
|
|
15
|
+
parse_global(options),
|
|
16
|
+
parse_outline(options.delete(:outline)),
|
|
17
|
+
parse_header_footer(:header => options.delete(:header),
|
|
18
|
+
:footer => options.delete(:footer),
|
|
19
|
+
:layout => options[:layout]),
|
|
20
|
+
parse_cover(options.delete(:cover)),
|
|
21
|
+
parse_toc(options.delete(:toc)),
|
|
22
|
+
parse_basic_auth(options)
|
|
23
|
+
].flatten
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def valid_option(name)
|
|
27
|
+
if binary_version < BINARY_VERSION_WITHOUT_DASHES
|
|
28
|
+
"--#{name}"
|
|
29
|
+
else
|
|
30
|
+
name
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def parse_extra(options)
|
|
37
|
+
return [] if options[:extra].nil?
|
|
38
|
+
return options[:extra].split if options[:extra].respond_to?(:split)
|
|
39
|
+
|
|
40
|
+
options[:extra]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def parse_basic_auth(options)
|
|
44
|
+
if options[:basic_auth]
|
|
45
|
+
user, passwd = Base64.decode64(options[:basic_auth]).split(':')
|
|
46
|
+
['--username', user, '--password', passwd]
|
|
47
|
+
else
|
|
48
|
+
[]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def parse_header_footer(options)
|
|
53
|
+
r = []
|
|
54
|
+
unless options.blank?
|
|
55
|
+
%i[header footer].collect do |hf|
|
|
56
|
+
next if options[hf].blank?
|
|
57
|
+
|
|
58
|
+
opt_hf = options[hf]
|
|
59
|
+
r += make_options(opt_hf, %i[center font_name left right], hf.to_s)
|
|
60
|
+
r += make_options(opt_hf, %i[font_size spacing], hf.to_s, :numeric)
|
|
61
|
+
r += make_options(opt_hf, [:line], hf.to_s, :boolean)
|
|
62
|
+
if options[hf] && options[hf][:content]
|
|
63
|
+
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
|
|
64
|
+
@hf_tempfiles.push(tf = WickedPdf::Tempfile.new("wicked_#{hf}_pdf.html"))
|
|
65
|
+
tf.write options[hf][:content]
|
|
66
|
+
tf.flush
|
|
67
|
+
options[hf][:html] = {}
|
|
68
|
+
options[hf][:html][:url] = "file:///#{tf.path}"
|
|
69
|
+
end
|
|
70
|
+
unless opt_hf[:html].blank?
|
|
71
|
+
r += make_option("#{hf}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
r
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def parse_cover(argument)
|
|
79
|
+
arg = argument.to_s
|
|
80
|
+
return [] if arg.blank?
|
|
81
|
+
|
|
82
|
+
# Filesystem path or URL - hand off to wkhtmltopdf
|
|
83
|
+
if argument.is_a?(Pathname) || (arg[0, 4] == 'http')
|
|
84
|
+
[valid_option('cover'), arg]
|
|
85
|
+
else # HTML content
|
|
86
|
+
@hf_tempfiles ||= []
|
|
87
|
+
@hf_tempfiles << tf = WickedPdf::Tempfile.new('wicked_cover_pdf.html')
|
|
88
|
+
tf.write arg
|
|
89
|
+
tf.flush
|
|
90
|
+
[valid_option('cover'), tf.path]
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def parse_toc(options)
|
|
95
|
+
return [] if options.nil?
|
|
96
|
+
|
|
97
|
+
r = [valid_option('toc')]
|
|
98
|
+
unless options.blank?
|
|
99
|
+
r += make_options(options, %i[font_name header_text], 'toc')
|
|
100
|
+
r += make_options(options, [:xsl_style_sheet])
|
|
101
|
+
r += make_options(options, %i[depth
|
|
102
|
+
header_fs
|
|
103
|
+
text_size_shrink
|
|
104
|
+
l1_font_size
|
|
105
|
+
l2_font_size
|
|
106
|
+
l3_font_size
|
|
107
|
+
l4_font_size
|
|
108
|
+
l5_font_size
|
|
109
|
+
l6_font_size
|
|
110
|
+
l7_font_size
|
|
111
|
+
level_indentation
|
|
112
|
+
l1_indentation
|
|
113
|
+
l2_indentation
|
|
114
|
+
l3_indentation
|
|
115
|
+
l4_indentation
|
|
116
|
+
l5_indentation
|
|
117
|
+
l6_indentation
|
|
118
|
+
l7_indentation], 'toc', :numeric)
|
|
119
|
+
r += make_options(options, %i[no_dots
|
|
120
|
+
disable_links
|
|
121
|
+
disable_back_links], 'toc', :boolean)
|
|
122
|
+
r += make_options(options, %i[disable_dotted_lines
|
|
123
|
+
disable_toc_links], nil, :boolean)
|
|
124
|
+
end
|
|
125
|
+
r
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def parse_outline(options)
|
|
129
|
+
r = []
|
|
130
|
+
unless options.blank?
|
|
131
|
+
r = make_options(options, [:outline], '', :boolean)
|
|
132
|
+
r += make_options(options, [:outline_depth], '', :numeric)
|
|
133
|
+
end
|
|
134
|
+
r
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def parse_margins(options)
|
|
138
|
+
make_options(options, %i[top bottom left right], 'margin', :numeric)
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def parse_global(options)
|
|
142
|
+
r = []
|
|
143
|
+
unless options.blank?
|
|
144
|
+
r += make_options(options, %i[orientation
|
|
145
|
+
dpi
|
|
146
|
+
page_size
|
|
147
|
+
page_width
|
|
148
|
+
title
|
|
149
|
+
log_level])
|
|
150
|
+
r += make_options(options, %i[lowquality
|
|
151
|
+
grayscale
|
|
152
|
+
no_pdf_compression
|
|
153
|
+
quiet], '', :boolean)
|
|
154
|
+
r += make_options(options, %i[image_dpi
|
|
155
|
+
image_quality
|
|
156
|
+
page_height], '', :numeric)
|
|
157
|
+
r += parse_margins(options.delete(:margin))
|
|
158
|
+
end
|
|
159
|
+
r
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def parse_others(options)
|
|
163
|
+
r = []
|
|
164
|
+
unless options.blank?
|
|
165
|
+
r += make_options(options, %i[proxy
|
|
166
|
+
username
|
|
167
|
+
password
|
|
168
|
+
encoding
|
|
169
|
+
user_style_sheet
|
|
170
|
+
viewport_size
|
|
171
|
+
window_status])
|
|
172
|
+
r += make_options(options, %i[cookie
|
|
173
|
+
post], '', :name_value)
|
|
174
|
+
r += make_options(options, %i[redirect_delay
|
|
175
|
+
zoom
|
|
176
|
+
page_offset
|
|
177
|
+
javascript_delay], '', :numeric)
|
|
178
|
+
r += make_options(options, %i[book
|
|
179
|
+
default_header
|
|
180
|
+
disable_javascript
|
|
181
|
+
enable_plugins
|
|
182
|
+
disable_internal_links
|
|
183
|
+
disable_external_links
|
|
184
|
+
keep_relative_links
|
|
185
|
+
print_media_type
|
|
186
|
+
disable_local_file_access
|
|
187
|
+
enable_local_file_access
|
|
188
|
+
disable_smart_shrinking
|
|
189
|
+
use_xserver
|
|
190
|
+
no_background
|
|
191
|
+
images
|
|
192
|
+
no_images
|
|
193
|
+
no_stop_slow_scripts], '', :boolean)
|
|
194
|
+
end
|
|
195
|
+
r
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
def make_options(options, names, prefix = '', type = :string)
|
|
199
|
+
return [] if options.nil?
|
|
200
|
+
|
|
201
|
+
names.collect do |o|
|
|
202
|
+
if options[o].blank?
|
|
203
|
+
[]
|
|
204
|
+
else
|
|
205
|
+
make_option("#{prefix.blank? ? '' : prefix + '-'}#{o}",
|
|
206
|
+
options[o],
|
|
207
|
+
type)
|
|
208
|
+
end
|
|
209
|
+
end
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
def make_option(name, value, type = :string)
|
|
213
|
+
return value.collect { |v| make_option(name, v, type) } if value.is_a?(Array)
|
|
214
|
+
|
|
215
|
+
if type == :name_value
|
|
216
|
+
parts = value.to_s.split(' ')
|
|
217
|
+
["--#{name.tr('_', '-')}", *parts]
|
|
218
|
+
elsif type == :boolean
|
|
219
|
+
if value
|
|
220
|
+
["--#{name.tr('_', '-')}"]
|
|
221
|
+
else
|
|
222
|
+
[]
|
|
223
|
+
end
|
|
224
|
+
else
|
|
225
|
+
["--#{name.tr('_', '-')}", value.to_s]
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
end
|
|
229
|
+
end
|
|
@@ -1,21 +1,5 @@
|
|
|
1
1
|
class WickedPdf
|
|
2
2
|
module PdfHelper
|
|
3
|
-
def self.included(base)
|
|
4
|
-
# Protect from trying to augment modules that appear
|
|
5
|
-
# as the result of adding other gems.
|
|
6
|
-
return if base != ActionController::Base
|
|
7
|
-
|
|
8
|
-
base.class_eval do
|
|
9
|
-
alias_method_chain :render, :wicked_pdf
|
|
10
|
-
alias_method_chain :render_to_string, :wicked_pdf
|
|
11
|
-
if respond_to?(:after_action)
|
|
12
|
-
after_action :clean_temp_files
|
|
13
|
-
else
|
|
14
|
-
after_filter :clean_temp_files
|
|
15
|
-
end
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
3
|
def self.prepended(base)
|
|
20
4
|
# Protect from trying to augment modules that appear
|
|
21
5
|
# as the result of adding other gems.
|
|
@@ -26,52 +10,52 @@ class WickedPdf
|
|
|
26
10
|
end
|
|
27
11
|
end
|
|
28
12
|
|
|
29
|
-
def render(
|
|
30
|
-
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def render_to_string(options = nil, *args, &block)
|
|
34
|
-
render_to_string_with_wicked_pdf(options, *args, &block)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def render_with_wicked_pdf(options = nil, *args, &block)
|
|
13
|
+
def render(*args)
|
|
14
|
+
options = args.first
|
|
38
15
|
if options.is_a?(Hash) && options.key?(:pdf)
|
|
39
|
-
|
|
40
|
-
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
|
|
41
|
-
elsif respond_to?(:render_without_wicked_pdf)
|
|
42
|
-
# support alias_method_chain (module included)
|
|
43
|
-
render_without_wicked_pdf(options, *args, &block)
|
|
16
|
+
render_with_wicked_pdf(options)
|
|
44
17
|
else
|
|
45
|
-
|
|
46
|
-
method(:render).super_method.call(options, *args, &block)
|
|
18
|
+
super
|
|
47
19
|
end
|
|
48
20
|
end
|
|
49
21
|
|
|
50
|
-
def
|
|
22
|
+
def render_to_string(*args)
|
|
23
|
+
options = args.first
|
|
51
24
|
if options.is_a?(Hash) && options.key?(:pdf)
|
|
52
|
-
|
|
53
|
-
options.delete :pdf
|
|
54
|
-
make_pdf((WickedPdf.config || {}).merge(options))
|
|
55
|
-
elsif respond_to?(:render_to_string_without_wicked_pdf)
|
|
56
|
-
# support alias_method_chain (module included)
|
|
57
|
-
render_to_string_without_wicked_pdf(options, *args, &block)
|
|
25
|
+
render_to_string_with_wicked_pdf(options)
|
|
58
26
|
else
|
|
59
|
-
|
|
60
|
-
method(:render_to_string).super_method.call(options, *args, &block)
|
|
27
|
+
super
|
|
61
28
|
end
|
|
62
29
|
end
|
|
63
30
|
|
|
31
|
+
def render_with_wicked_pdf(options)
|
|
32
|
+
raise ArgumentError, 'missing keyword: pdf' unless options.is_a?(Hash) && options.key?(:pdf)
|
|
33
|
+
|
|
34
|
+
options[:basic_auth] = set_basic_auth(options)
|
|
35
|
+
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def render_to_string_with_wicked_pdf(options)
|
|
39
|
+
raise ArgumentError, 'missing keyword: pdf' unless options.is_a?(Hash) && options.key?(:pdf)
|
|
40
|
+
|
|
41
|
+
options[:basic_auth] = set_basic_auth(options)
|
|
42
|
+
options.delete :pdf
|
|
43
|
+
make_pdf((WickedPdf.config || {}).merge(options))
|
|
44
|
+
end
|
|
45
|
+
|
|
64
46
|
private
|
|
65
47
|
|
|
66
48
|
def set_basic_auth(options = {})
|
|
67
49
|
options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
|
|
68
50
|
return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']
|
|
51
|
+
|
|
69
52
|
request.env['HTTP_AUTHORIZATION'].split(' ').last
|
|
70
53
|
end
|
|
71
54
|
|
|
72
55
|
def clean_temp_files
|
|
73
56
|
return unless defined?(@hf_tempfiles)
|
|
74
|
-
|
|
57
|
+
|
|
58
|
+
@hf_tempfiles.each(&:close)
|
|
75
59
|
end
|
|
76
60
|
|
|
77
61
|
def make_pdf(options = {})
|
|
@@ -119,10 +103,11 @@ class WickedPdf
|
|
|
119
103
|
# Given an options hash, prerenders content for the header and footer sections
|
|
120
104
|
# to temp files and return a new options hash including the URLs to these files.
|
|
121
105
|
def prerender_header_and_footer(options)
|
|
122
|
-
[
|
|
106
|
+
%i[header footer].each do |hf|
|
|
123
107
|
next unless options[hf] && options[hf][:html] && options[hf][:html][:template]
|
|
108
|
+
|
|
124
109
|
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
|
|
125
|
-
@hf_tempfiles.push(tf =
|
|
110
|
+
@hf_tempfiles.push(tf = WickedPdf::Tempfile.new("wicked_#{hf}_pdf.html"))
|
|
126
111
|
options[hf][:html][:layout] ||= options[:layout]
|
|
127
112
|
render_opts = {
|
|
128
113
|
:template => options[hf][:html][:template],
|
data/lib/wicked_pdf/railtie.rb
CHANGED
|
@@ -6,21 +6,12 @@ class WickedPdf
|
|
|
6
6
|
if defined?(Rails.env)
|
|
7
7
|
class WickedRailtie < Rails::Railtie
|
|
8
8
|
initializer 'wicked_pdf.register', :after => 'remotipart.controller_helper' do |_app|
|
|
9
|
-
ActiveSupport.on_load(:action_controller)
|
|
10
|
-
|
|
11
|
-
Object.method(:new).respond_to?(:super_method)
|
|
12
|
-
ActionController::Base.send :prepend, PdfHelper
|
|
13
|
-
else
|
|
14
|
-
ActionController::Base.send :include, PdfHelper
|
|
15
|
-
end
|
|
16
|
-
ActionView::Base.send :include, WickedPdfHelper::Assets
|
|
17
|
-
end
|
|
9
|
+
ActiveSupport.on_load(:action_controller) { ActionController::Base.send :prepend, PdfHelper }
|
|
10
|
+
ActiveSupport.on_load(:action_view) { include WickedPdfHelper::Assets }
|
|
18
11
|
end
|
|
19
12
|
end
|
|
20
13
|
|
|
21
|
-
if Mime::Type.lookup_by_extension(:pdf).nil?
|
|
22
|
-
Mime::Type.register('application/pdf', :pdf)
|
|
23
|
-
end
|
|
14
|
+
Mime::Type.register('application/pdf', :pdf) if Mime::Type.lookup_by_extension(:pdf).nil?
|
|
24
15
|
|
|
25
16
|
end
|
|
26
17
|
end
|
data/lib/wicked_pdf/tempfile.rb
CHANGED
|
@@ -1,13 +1,42 @@
|
|
|
1
1
|
require 'tempfile'
|
|
2
2
|
|
|
3
3
|
class WickedPdf
|
|
4
|
-
class
|
|
5
|
-
# ensures the Tempfile's filename always keeps its extension
|
|
4
|
+
class Tempfile < ::Tempfile
|
|
6
5
|
def initialize(filename, temp_dir = nil)
|
|
7
6
|
temp_dir ||= Dir.tmpdir
|
|
8
7
|
extension = File.extname(filename)
|
|
9
|
-
basename
|
|
8
|
+
basename = File.basename(filename, extension)
|
|
10
9
|
super([basename, extension], temp_dir)
|
|
11
10
|
end
|
|
11
|
+
|
|
12
|
+
def write_in_chunks(input_string)
|
|
13
|
+
binmode
|
|
14
|
+
string_io = StringIO.new(input_string)
|
|
15
|
+
write(string_io.read(chunk_size)) until string_io.eof?
|
|
16
|
+
close
|
|
17
|
+
self
|
|
18
|
+
rescue Errno::EINVAL => e
|
|
19
|
+
raise e, file_too_large_message
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def read_in_chunks
|
|
23
|
+
rewind
|
|
24
|
+
binmode
|
|
25
|
+
output_string = ''
|
|
26
|
+
output_string << read(chunk_size) until eof?
|
|
27
|
+
output_string
|
|
28
|
+
rescue Errno::EINVAL => e
|
|
29
|
+
raise e, file_too_large_message
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def chunk_size
|
|
35
|
+
1024 * 1024
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def file_too_large_message
|
|
39
|
+
'The HTML file is too large! Try reducing the size or using the return_file option instead.'
|
|
40
|
+
end
|
|
12
41
|
end
|
|
13
42
|
end
|
data/lib/wicked_pdf/version.rb
CHANGED
|
@@ -1,19 +1,47 @@
|
|
|
1
1
|
require 'net/http'
|
|
2
|
-
|
|
3
|
-
require 'webpacker/version' if defined?(Webpacker)
|
|
2
|
+
require 'delegate'
|
|
4
3
|
|
|
5
4
|
class WickedPdf
|
|
6
5
|
module WickedPdfHelper
|
|
7
6
|
module Assets
|
|
8
7
|
ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
|
|
9
8
|
|
|
9
|
+
class PropshaftAsset < SimpleDelegator
|
|
10
|
+
def content_type
|
|
11
|
+
super.to_s
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def to_s
|
|
15
|
+
content
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def filename
|
|
19
|
+
path.to_s
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
10
23
|
def wicked_pdf_asset_base64(path)
|
|
11
24
|
asset = find_asset(path)
|
|
12
25
|
raise "Could not find asset '#{path}'" if asset.nil?
|
|
26
|
+
|
|
13
27
|
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
|
|
14
28
|
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
|
|
15
29
|
end
|
|
16
30
|
|
|
31
|
+
# Using `image_tag` with URLs when generating PDFs (specifically large PDFs with lots of pages) can cause buffer/stack overflows.
|
|
32
|
+
#
|
|
33
|
+
def wicked_pdf_url_base64(url)
|
|
34
|
+
response = Net::HTTP.get_response(URI(url))
|
|
35
|
+
|
|
36
|
+
if response.is_a?(Net::HTTPSuccess)
|
|
37
|
+
base64 = Base64.encode64(response.body).gsub(/\s+/, '')
|
|
38
|
+
"data:#{response.content_type};base64,#{Rack::Utils.escape(base64)}"
|
|
39
|
+
else
|
|
40
|
+
Rails.logger.warn("[wicked_pdf] #{response.code} #{response.message}: #{url}")
|
|
41
|
+
nil
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
17
45
|
def wicked_pdf_stylesheet_link_tag(*sources)
|
|
18
46
|
stylesheet_contents = sources.collect do |source|
|
|
19
47
|
source = WickedPdfHelper.add_extension(source, 'css')
|
|
@@ -31,6 +59,7 @@ class WickedPdf
|
|
|
31
59
|
|
|
32
60
|
def wicked_pdf_stylesheet_pack_tag(*sources)
|
|
33
61
|
return unless defined?(Webpacker)
|
|
62
|
+
|
|
34
63
|
if running_in_development?
|
|
35
64
|
stylesheet_pack_tag(*sources)
|
|
36
65
|
else
|
|
@@ -79,6 +108,16 @@ class WickedPdf
|
|
|
79
108
|
end
|
|
80
109
|
end
|
|
81
110
|
|
|
111
|
+
def wicked_pdf_asset_pack_path(asset)
|
|
112
|
+
return unless defined?(Webpacker)
|
|
113
|
+
|
|
114
|
+
if running_in_development?
|
|
115
|
+
asset_pack_path(asset)
|
|
116
|
+
else
|
|
117
|
+
wicked_pdf_asset_path webpacker_source_url(asset)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
|
|
82
121
|
private
|
|
83
122
|
|
|
84
123
|
# borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
|
|
@@ -108,6 +147,8 @@ class WickedPdf
|
|
|
108
147
|
def find_asset(path)
|
|
109
148
|
if Rails.application.assets.respond_to?(:find_asset)
|
|
110
149
|
Rails.application.assets.find_asset(path, :base_path => Rails.application.root.to_s)
|
|
150
|
+
elsif defined?(Propshaft::Assembly) && Rails.application.assets.is_a?(Propshaft::Assembly)
|
|
151
|
+
PropshaftAsset.new(Rails.application.assets.load_path.find(path))
|
|
111
152
|
else
|
|
112
153
|
Sprockets::Railtie.build_environment(Rails.application).find_asset(path, :base_path => Rails.application.root.to_s)
|
|
113
154
|
end
|
|
@@ -161,10 +202,15 @@ class WickedPdf
|
|
|
161
202
|
end
|
|
162
203
|
|
|
163
204
|
def webpacker_source_url(source)
|
|
164
|
-
return unless
|
|
205
|
+
return unless webpacker_version
|
|
206
|
+
|
|
165
207
|
# In Webpacker 3.2.0 asset_pack_url is introduced
|
|
166
|
-
if
|
|
167
|
-
|
|
208
|
+
if webpacker_version >= '3.2.0'
|
|
209
|
+
if (host = Rails.application.config.asset_host)
|
|
210
|
+
asset_pack_path(source, :host => host)
|
|
211
|
+
else
|
|
212
|
+
asset_pack_url(source)
|
|
213
|
+
end
|
|
168
214
|
else
|
|
169
215
|
source_path = asset_pack_path(source)
|
|
170
216
|
# Remove last slash from root path
|
|
@@ -173,7 +219,8 @@ class WickedPdf
|
|
|
173
219
|
end
|
|
174
220
|
|
|
175
221
|
def running_in_development?
|
|
176
|
-
return unless
|
|
222
|
+
return unless webpacker_version
|
|
223
|
+
|
|
177
224
|
# :dev_server method was added in webpacker 3.0.0
|
|
178
225
|
if Webpacker.respond_to?(:dev_server)
|
|
179
226
|
Webpacker.dev_server.running?
|
|
@@ -181,6 +228,15 @@ class WickedPdf
|
|
|
181
228
|
Rails.env.development? || Rails.env.test?
|
|
182
229
|
end
|
|
183
230
|
end
|
|
231
|
+
|
|
232
|
+
def webpacker_version
|
|
233
|
+
return unless defined?(Webpacker)
|
|
234
|
+
|
|
235
|
+
# If webpacker is used, need to check for version
|
|
236
|
+
require 'webpacker/version'
|
|
237
|
+
|
|
238
|
+
Webpacker::VERSION
|
|
239
|
+
end
|
|
184
240
|
end
|
|
185
241
|
end
|
|
186
242
|
end
|