wicked_pdf 0.11.0 → 1.0.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.
Files changed (63) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.travis.yml +13 -3
  4. data/Gemfile +1 -1
  5. data/README.md +152 -142
  6. data/Rakefile +24 -12
  7. data/gemfiles/2.3.gemfile +2 -0
  8. data/gemfiles/3.1.gemfile +3 -2
  9. data/gemfiles/3.2.gemfile +3 -2
  10. data/gemfiles/4.0.gemfile +2 -2
  11. data/gemfiles/4.1.gemfile +6 -0
  12. data/gemfiles/4.2.gemfile +6 -0
  13. data/gemfiles/rails_edge.gemfile +3 -3
  14. data/generators/wicked_pdf/templates/wicked_pdf.rb +19 -3
  15. data/generators/wicked_pdf/wicked_pdf_generator.rb +1 -1
  16. data/lib/generators/wicked_pdf_generator.rb +2 -2
  17. data/lib/wicked_pdf.rb +241 -212
  18. data/lib/wicked_pdf/middleware.rb +6 -7
  19. data/lib/wicked_pdf/pdf_helper.rb +53 -53
  20. data/lib/wicked_pdf/railtie.rb +14 -14
  21. data/lib/wicked_pdf/tempfile.rb +2 -2
  22. data/lib/wicked_pdf/version.rb +2 -2
  23. data/lib/wicked_pdf/wicked_pdf_helper.rb +30 -15
  24. data/test/functional/pdf_helper_test.rb +5 -5
  25. data/test/functional/wicked_pdf_helper_assets_test.rb +35 -8
  26. data/test/functional/wicked_pdf_helper_test.rb +2 -2
  27. data/test/test_helper.rb +10 -5
  28. data/test/unit/wicked_pdf_test.rb +90 -57
  29. data/wicked_pdf.gemspec +5 -4
  30. metadata +20 -72
  31. data/test/dummy/README.rdoc +0 -261
  32. data/test/dummy/Rakefile +0 -7
  33. data/test/dummy/app/assets/javascripts/application.js +0 -15
  34. data/test/dummy/app/assets/stylesheets/application.css +0 -13
  35. data/test/dummy/app/controllers/application_controller.rb +0 -3
  36. data/test/dummy/app/helpers/application_helper.rb +0 -2
  37. data/test/dummy/app/mailers/.gitkeep +0 -0
  38. data/test/dummy/app/models/.gitkeep +0 -0
  39. data/test/dummy/app/views/layouts/application.html.erb +0 -14
  40. data/test/dummy/config.ru +0 -4
  41. data/test/dummy/config/application.rb +0 -59
  42. data/test/dummy/config/boot.rb +0 -10
  43. data/test/dummy/config/database.yml +0 -25
  44. data/test/dummy/config/environment.rb +0 -5
  45. data/test/dummy/config/environments/development.rb +0 -37
  46. data/test/dummy/config/environments/production.rb +0 -67
  47. data/test/dummy/config/environments/test.rb +0 -37
  48. data/test/dummy/config/initializers/backtrace_silencers.rb +0 -7
  49. data/test/dummy/config/initializers/inflections.rb +0 -15
  50. data/test/dummy/config/initializers/mime_types.rb +0 -5
  51. data/test/dummy/config/initializers/secret_token.rb +0 -7
  52. data/test/dummy/config/initializers/session_store.rb +0 -8
  53. data/test/dummy/config/initializers/wrap_parameters.rb +0 -14
  54. data/test/dummy/config/locales/en.yml +0 -5
  55. data/test/dummy/config/routes.rb +0 -58
  56. data/test/dummy/db/test.sqlite3 +0 -0
  57. data/test/dummy/lib/assets/.gitkeep +0 -0
  58. data/test/dummy/log/.gitkeep +0 -0
  59. data/test/dummy/public/404.html +0 -26
  60. data/test/dummy/public/422.html +0 -26
  61. data/test/dummy/public/500.html +0 -25
  62. data/test/dummy/public/favicon.ico +0 -0
  63. data/test/dummy/script/rails +0 -6
data/Rakefile CHANGED
@@ -5,7 +5,7 @@ require 'rails/version'
5
5
  require 'bundler/gem_tasks'
6
6
 
7
7
  desc 'Default: run unit tests.'
8
- task :default => :test
8
+ task :default => :setup_and_run_tests
9
9
 
10
10
  desc 'Test the wicked_pdf plugin.'
11
11
  Rake::TestTask.new(:test) do |t|
@@ -15,19 +15,31 @@ Rake::TestTask.new(:test) do |t|
15
15
  t.verbose = true
16
16
  end
17
17
 
18
- desc "Generate dummy application for test cases"
18
+ desc 'Setup and run all tests'
19
+ task :setup_and_run_tests do
20
+ unless File.exist?('test/dummy/config/environment.rb')
21
+ Rake::Task[:dummy_generate].invoke
22
+ end
23
+ Rake::Task[:test].invoke
24
+ end
25
+
26
+ desc 'Generate dummy application for test cases'
19
27
  task :dummy_generate do
20
- # recursively remove test/dummy directory entity
21
- FileUtils.rm_r Dir.glob('test/dummy/*')
28
+ Rake::Task[:dummy_remove].invoke
29
+ puts 'Creating dummy application to run tests'
22
30
 
23
- prefix = ''
24
- if Rails::VERSION::MAJOR != 2
25
- prefix = 'new '
26
- end
31
+ prefix = ''
32
+ if Rails::VERSION::MAJOR != 2
33
+ prefix = 'new '
34
+ end
35
+
36
+ system("rails #{prefix}test/dummy")
37
+ FileUtils.rm_r Dir.glob('test/dummy/test/*')
38
+ end
27
39
 
28
- system("rails #{prefix}test/dummy")
29
-
30
- FileUtils.rm_r Dir.glob('test/dummy/test/*')
40
+ desc 'Remove dummy application'
41
+ task :dummy_remove do
42
+ FileUtils.rm_r Dir.glob('test/dummy/*')
31
43
  end
32
44
 
33
45
  desc 'Generate documentation for the wicked_pdf plugin.'
@@ -35,6 +47,6 @@ RDoc::Task.new(:rdoc) do |rdoc|
35
47
  rdoc.rdoc_dir = 'rdoc'
36
48
  rdoc.title = 'WickedPdf'
37
49
  rdoc.options << '--line-numbers' << '--inline-source'
38
- rdoc.rdoc_files.include('README')
50
+ rdoc.rdoc_files.include('README.md')
39
51
  rdoc.rdoc_files.include('lib/**/*.rb')
40
52
  end
@@ -4,5 +4,7 @@ gem 'rdoc'
4
4
  gem 'rails', '~> 2.3.0'
5
5
  gem 'rake', '~> 0.9.2'
6
6
  gem 'ruby-prof', '~> 0.11.3'
7
+ gem 'test-unit', '= 2.5.2'
8
+ gem 'mocha', '~> 0.12.8'
7
9
  gem 'wicked_pdf', :path => '../'
8
10
  gemspec :path => '../'
@@ -3,5 +3,6 @@ source "https://rubygems.org"
3
3
  gem 'rdoc'
4
4
  gem 'rails', '~> 3.1.0'
5
5
  gem 'sqlite3'
6
- gem 'wicked_pdf', :path => '../'
7
- gemspec :path => '../'
6
+ gem 'i18n', '~> 0.6.0'
7
+ gem 'wicked_pdf', path: '../'
8
+ gemspec path: '../'
@@ -2,5 +2,6 @@ source "https://rubygems.org"
2
2
 
3
3
  gem 'rdoc'
4
4
  gem 'rails', '~> 3.2.0'
5
- gem 'wicked_pdf', :path => '../'
6
- gemspec :path => '../'
5
+ gem 'i18n', '~> 0.6.0'
6
+ gem 'wicked_pdf', path: '../'
7
+ gemspec path: '../'
@@ -2,5 +2,5 @@ source "https://rubygems.org"
2
2
 
3
3
  gem 'rdoc'
4
4
  gem 'rails', '~> 4.0.0'
5
- gem 'wicked_pdf', :path => '../'
6
- gemspec :path => '../'
5
+ gem 'wicked_pdf', path: '../'
6
+ gemspec path: '../'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rdoc'
4
+ gem 'rails', '~> 4.1.0'
5
+ gem 'wicked_pdf', path: '../'
6
+ gemspec path: '../'
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rdoc'
4
+ gem 'rails', '~> 4.2.0'
5
+ gem 'wicked_pdf', path: '../'
6
+ gemspec path: '../'
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem 'rdoc'
4
- gem 'rails', :git => "https://github.com/rails/rails.git"
5
- gem 'wicked_pdf', :path => '../'
6
- gemspec :path => '../'
4
+ gem 'rails', git: "https://github.com/rails/rails.git"
5
+ gem 'wicked_pdf', path: '../'
6
+ gemspec path: '../'
@@ -1,5 +1,21 @@
1
+ # WickedPDF Global Configuration
2
+ #
3
+ # Use this to set up shared configuration options for your entire application.
4
+ # Any of the configuration options shown here can also be applied to single
5
+ # models by passing arguments to the `render :pdf` call.
6
+ #
7
+ # To learn more, check out the README:
8
+ #
9
+ # https://github.com/mileszs/wicked_pdf/blob/master/README.md
10
+
1
11
  WickedPdf.config = {
2
- #:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf',
3
- #:layout => "pdf.html",
4
- :exe_path => '/usr/local/bin/wkhtmltopdf'
12
+ # Path to the wkhtmltopdf executable: This usually isn't needed if using
13
+ # one of the wkhtmltopdf-binary family of gems.
14
+ # exe_path: '/usr/local/bin/wkhtmltopdf',
15
+ # or
16
+ # exe_path: Gem.bin_path('wkhtmltopdf-binary', 'wkhtmltopdf')
17
+
18
+ # Layout file to be used for all PDFs
19
+ # (but can be overridden in `render :pdf` calls)
20
+ # layout: 'pdf.html',
5
21
  }
@@ -1,7 +1,7 @@
1
1
  class WickedPdfGenerator < Rails::Generator::Base
2
2
  def manifest
3
3
  record do |m|
4
- m.file "wicked_pdf.rb", "config/initializers/wicked_pdf.rb"
4
+ m.file 'wicked_pdf.rb', 'config/initializers/wicked_pdf.rb'
5
5
  end
6
6
  end
7
7
  end
@@ -2,10 +2,10 @@ if defined?(Rails) && Rails::VERSION::MAJOR != 2
2
2
 
3
3
  # Rails3 generator invoked with 'rails generate wicked_pdf'
4
4
  class WickedPdfGenerator < Rails::Generators::Base
5
- source_root(File.expand_path(File.dirname(__FILE__) + "/../../generators/wicked_pdf/templates"))
5
+ source_root(File.expand_path(File.dirname(__FILE__) + '/../../generators/wicked_pdf/templates'))
6
6
  def copy_initializer
7
7
  copy_file 'wicked_pdf.rb', 'config/initializers/wicked_pdf.rb'
8
8
  end
9
9
  end
10
10
 
11
- end
11
+ end
@@ -30,38 +30,49 @@ require 'wicked_pdf/middleware'
30
30
 
31
31
  class WickedPdf
32
32
  DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
33
- EXE_NAME = "wkhtmltopdf"
33
+ BINARY_VERSION_WITHOUT_DASHES = Gem::Version.new('0.12.0')
34
+ EXE_NAME = 'wkhtmltopdf'
34
35
  @@config = {}
35
36
  cattr_accessor :config
36
37
 
37
38
  def initialize(wkhtmltopdf_binary_path = nil)
38
39
  @exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
39
- raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
40
- raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exists?(@exe_path)
41
- raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
40
+ fail "Location of #{EXE_NAME} unknown" if @exe_path.empty?
41
+ fail "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exist?(@exe_path)
42
+ fail "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
42
43
 
43
- @binary_version = DEFAULT_BINARY_VERSION
44
+ retreive_binary_version
44
45
  end
45
46
 
46
- def retreive_binary_version
47
- begin
48
- stdin, stdout, stderr = Open3.popen3(@exe_path + ' -V')
49
- @binary_version = parse_version(stdout.gets(nil))
50
- rescue StandardError
51
- end
47
+ def pdf_from_html_file(filepath, options = {})
48
+ pdf_from_url("file:///#{filepath}", options)
52
49
  end
53
50
 
54
- def pdf_from_html_file(filepath, options={})
55
- if WickedPdf.config[:retreive_version]
56
- retreive_binary_version
57
- end
51
+ def pdf_from_string(string, options = {})
52
+ options = options.dup
53
+ # merge in global config options
54
+ options.merge!(WickedPdf.config) {|key, option, config| option}
55
+ string_file = WickedPdfTempfile.new("wicked_pdf.html", options[:temp_path])
56
+ string_file.binmode
57
+ string_file.write(string)
58
+ string_file.close
58
59
 
59
- temp_path = options.delete(:temp_path)
60
- generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", temp_path)
60
+ pdf = pdf_from_html_file(string_file.path, options)
61
+ pdf
62
+ rescue => e
63
+ raise "Error: #{e}"
64
+ ensure
65
+ string_file.close! if string_file
66
+ end
67
+
68
+ def pdf_from_url(url, options = {})
69
+ # merge in global config options
70
+ options.merge!(WickedPdf.config) {|key, option, config| option}
71
+ generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", options[:temp_path])
61
72
  command = [@exe_path]
62
73
  command << '-q' unless on_windows? # suppress errors on stdout
63
74
  command += parse_options(options)
64
- command << "file://#{filepath}"
75
+ command << url
65
76
  command << generated_pdf_file.path.to_s
66
77
 
67
78
  print_command(command.inspect) if in_development_mode?
@@ -75,239 +86,257 @@ class WickedPdf
75
86
  generated_pdf_file.rewind
76
87
  generated_pdf_file.binmode
77
88
  pdf = generated_pdf_file.read
78
- raise "PDF could not be generated!\n Command Error: #{err}" if pdf and pdf.rstrip.length == 0
89
+ fail "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.length == 0
79
90
  pdf
80
- rescue Exception => e
91
+ rescue => e
81
92
  raise "Failed to execute:\n#{command}\nError: #{e}"
82
93
  ensure
83
94
  generated_pdf_file.close! if generated_pdf_file && !return_file
84
95
  end
85
96
 
86
- def pdf_from_string(string, options={})
87
- temp_path = options.delete(:temp_path)
88
- string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
89
- string_file.binmode
90
- string_file.write(string)
91
- string_file.close
97
+ private
92
98
 
93
- pdf = pdf_from_html_file(string_file.path, options)
94
- pdf
95
- rescue Exception => e
96
- raise "Error: #{e}"
97
- ensure
98
- string_file.close! if string_file
99
+ def in_development_mode?
100
+ return Rails.env == 'development' if defined?(Rails)
101
+ RAILS_ENV == 'development' if defined?(RAILS_ENV)
99
102
  end
100
103
 
101
- private
104
+ def get_binary_version
105
+ @binary_version
106
+ end
102
107
 
103
- def in_development_mode?
104
- return Rails.env == 'development' if defined?(Rails)
105
- RAILS_ENV == 'development' if defined?(RAILS_ENV)
106
- end
108
+ def on_windows?
109
+ RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
110
+ end
107
111
 
108
- def get_binary_version
109
- @binary_version
110
- end
112
+ def print_command(cmd)
113
+ p '*' * 15 + cmd + '*' * 15
114
+ end
111
115
 
112
- def on_windows?
113
- RbConfig::CONFIG['target_os'] =~ /mswin|mingw/
114
- end
116
+ def retreive_binary_version
117
+ stdin, stdout, stderr = Open3.popen3(@exe_path + ' -V')
118
+ @binary_version = parse_version(stdout.gets(nil))
119
+ rescue StandardError
120
+ end
115
121
 
116
- def print_command(cmd)
117
- p "*"*15 + cmd + "*"*15
122
+ def parse_version(version_info)
123
+ match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
124
+ if match_data && (2 == match_data.length)
125
+ Gem::Version.new(match_data[1])
126
+ else
127
+ DEFAULT_BINARY_VERSION
118
128
  end
129
+ end
119
130
 
120
- def parse_version(version_info)
121
- match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
122
- if (match_data && (2 == match_data.length))
123
- Gem::Version.new(match_data[1])
124
- else
125
- DEFAULT_BINARY_VERSION
126
- end
127
- end
131
+ def parse_options(options)
132
+ [
133
+ parse_extra(options),
134
+ parse_others(options),
135
+ parse_global(options),
136
+ parse_outline(options.delete(:outline)),
137
+ parse_header_footer(:header => options.delete(:header),
138
+ :footer => options.delete(:footer),
139
+ :layout => options[:layout]),
140
+ parse_cover(options.delete(:cover)),
141
+ parse_toc(options.delete(:toc)),
142
+ parse_basic_auth(options)
143
+ ].flatten
144
+ end
145
+
146
+ def parse_extra(options)
147
+ return [] if options[:extra].nil?
148
+ return options[:extra].split if options[:extra].respond_to?(:split)
149
+ options[:extra]
150
+ end
128
151
 
129
- def parse_options(options)
130
- [
131
- parse_extra(options),
132
- parse_header_footer(:header => options.delete(:header),
133
- :footer => options.delete(:footer),
134
- :layout => options[:layout]),
135
- parse_cover(options.delete(:cover)),
136
- parse_toc(options.delete(:toc)),
137
- parse_outline(options.delete(:outline)),
138
- parse_margins(options.delete(:margin)),
139
- parse_others(options),
140
- parse_basic_auth(options)
141
- ].flatten
152
+ def parse_basic_auth(options)
153
+ if options[:basic_auth]
154
+ user, passwd = Base64.decode64(options[:basic_auth]).split(':')
155
+ ['--username', user, '--password', passwd]
156
+ else
157
+ []
142
158
  end
159
+ end
143
160
 
144
- def parse_extra(options)
145
- return [] if options[:extra].nil?
146
- return options[:extra].split if options[:extra].respond_to?(:split)
147
- return options[:extra]
161
+ def make_option(name, value, type = :string)
162
+ if value.is_a?(Array)
163
+ return value.collect { |v| make_option(name, v, type) }
148
164
  end
165
+ if type == :name_value
166
+ parts = value.to_s.split(' ')
167
+ ["--#{name.gsub('_', '-')}", *parts]
168
+ elsif type == :boolean
169
+ ["--#{name.gsub('_', '-')}"]
170
+ else
171
+ ["--#{name.gsub('_', '-')}", value.to_s]
172
+ end
173
+ end
149
174
 
150
- def parse_basic_auth(options)
151
- if options[:basic_auth]
152
- user, passwd = Base64.decode64(options[:basic_auth]).split(":")
153
- ["--username", user, "--password", passwd]
154
- else
155
- []
156
- end
175
+ def valid_option(name)
176
+ if get_binary_version < BINARY_VERSION_WITHOUT_DASHES
177
+ "--#{name}"
178
+ else
179
+ name
157
180
  end
181
+ end
158
182
 
159
- def make_option(name, value, type=:string)
160
- if value.is_a?(Array)
161
- return value.collect { |v| make_option(name, v, type) }
162
- end
163
- if type == :boolean
164
- ["--#{name.gsub('_', '-')}"]
183
+ def make_options(options, names, prefix = '', type = :string)
184
+ return [] if options.nil?
185
+ names.collect do |o|
186
+ if options[o].blank?
187
+ []
165
188
  else
166
- ["--#{name.gsub('_', '-')}", value.to_s]
189
+ make_option("#{prefix.blank? ? '' : prefix + '-'}#{o.to_s}",
190
+ options[o],
191
+ type)
167
192
  end
168
193
  end
194
+ end
169
195
 
170
- def make_options(options, names, prefix="", type=:string)
171
- return [] if options.nil?
172
- names.collect do |o|
173
- if options[o].blank?
174
- []
175
- else
176
- make_option("#{prefix.blank? ? "" : prefix + "-"}#{o.to_s}",
177
- options[o],
178
- type)
196
+ def parse_header_footer(options)
197
+ r = []
198
+ [:header, :footer].collect do |hf|
199
+ unless options[hf].blank?
200
+ opt_hf = options[hf]
201
+ r += make_options(opt_hf, [:center, :font_name, :left, :right], "#{hf.to_s}")
202
+ r += make_options(opt_hf, [:font_size, :spacing], "#{hf.to_s}", :numeric)
203
+ r += make_options(opt_hf, [:line], "#{hf.to_s}", :boolean)
204
+ if options[hf] && options[hf][:content]
205
+ @hf_tempfiles = [] unless defined?(@hf_tempfiles)
206
+ @hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
207
+ tf.write options[hf][:content]
208
+ tf.flush
209
+ options[hf][:html] = {}
210
+ options[hf][:html][:url] = "file:///#{tf.path}"
211
+ end
212
+ unless opt_hf[:html].blank?
213
+ r += make_option("#{hf.to_s}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
179
214
  end
180
215
  end
181
- end
216
+ end unless options.blank?
217
+ r
218
+ end
182
219
 
183
- def parse_header_footer(options)
184
- r=[]
185
- [:header, :footer].collect do |hf|
186
- unless options[hf].blank?
187
- opt_hf = options[hf]
188
- r += make_options(opt_hf, [:center, :font_name, :left, :right], "#{hf.to_s}")
189
- r += make_options(opt_hf, [:font_size, :spacing], "#{hf.to_s}", :numeric)
190
- r += make_options(opt_hf, [:line], "#{hf.to_s}", :boolean)
191
- if options[hf] && options[hf][:content]
192
- @hf_tempfiles = [] if ! defined?(@hf_tempfiles)
193
- @hf_tempfiles.push( tf=WickedPdfTempfile.new("wicked_#{hf}_pdf.html") )
194
- tf.write options[hf][:content]
195
- tf.flush
196
- options[hf].delete(:content)
197
- options[hf][:html] = {}
198
- options[hf][:html][:url] = "file:///#{tf.path}"
199
- end
200
- unless opt_hf[:html].blank?
201
- r += make_option("#{hf.to_s}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
202
- end
203
- end
204
- end unless options.blank?
205
- r
220
+ def parse_cover(argument)
221
+ arg = argument.to_s
222
+ return [] if arg.blank?
223
+ # Filesystem path or URL - hand off to wkhtmltopdf
224
+ if argument.is_a?(Pathname) || (arg[0, 4] == 'http')
225
+ [valid_option('cover'), arg]
226
+ else # HTML content
227
+ @hf_tempfiles ||= []
228
+ @hf_tempfiles << tf = WickedPdfTempfile.new('wicked_cover_pdf.html')
229
+ tf.write arg
230
+ tf.flush
231
+ [valid_option('cover'), tf.path]
206
232
  end
233
+ end
207
234
 
208
- def parse_cover(argument)
209
- arg = argument.to_s
210
- return [] if arg.blank?
211
- # Filesystem path or URL - hand off to wkhtmltopdf
212
- if argument.is_a?(Pathname) || (arg[0,4] == 'http')
213
- ['--cover', arg]
214
- else # HTML content
215
- @hf_tempfiles ||= []
216
- @hf_tempfiles << tf=WickedPdfTempfile.new("wicked_cover_pdf.html")
217
- tf.write arg
218
- tf.flush
219
- ['--cover', tf.path]
220
- end
235
+ def parse_toc(options)
236
+ return [] if options.nil?
237
+ r = [valid_option('toc')]
238
+ unless options.blank?
239
+ r += make_options(options, [:font_name, :header_text], 'toc')
240
+ r += make_options(options, [:xsl_style_sheet])
241
+ r += make_options(options, [:depth,
242
+ :header_fs,
243
+ :text_size_shrink,
244
+ :l1_font_size,
245
+ :l2_font_size,
246
+ :l3_font_size,
247
+ :l4_font_size,
248
+ :l5_font_size,
249
+ :l6_font_size,
250
+ :l7_font_size,
251
+ :level_indentation,
252
+ :l1_indentation,
253
+ :l2_indentation,
254
+ :l3_indentation,
255
+ :l4_indentation,
256
+ :l5_indentation,
257
+ :l6_indentation,
258
+ :l7_indentation], 'toc', :numeric)
259
+ r += make_options(options, [:no_dots,
260
+ :disable_links,
261
+ :disable_back_links], 'toc', :boolean)
262
+ r += make_options(options, [:disable_dotted_lines,
263
+ :disable_toc_links], nil, :boolean)
221
264
  end
265
+ r
266
+ end
222
267
 
223
- def parse_toc(options)
224
- return [] if options.nil?
225
- r = ['--toc']
226
- unless options.blank?
227
- r += make_options(options, [ :font_name, :header_text], "toc")
228
- r +=make_options(options, [ :depth,
229
- :header_fs,
230
- :l1_font_size,
231
- :l2_font_size,
232
- :l3_font_size,
233
- :l4_font_size,
234
- :l5_font_size,
235
- :l6_font_size,
236
- :l7_font_size,
237
- :l1_indentation,
238
- :l2_indentation,
239
- :l3_indentation,
240
- :l4_indentation,
241
- :l5_indentation,
242
- :l6_indentation,
243
- :l7_indentation], "toc", :numeric)
244
- r +=make_options(options, [ :no_dots,
245
- :disable_links,
246
- :disable_back_links], "toc", :boolean)
247
- end
248
- return r
268
+ def parse_outline(options)
269
+ r = []
270
+ unless options.blank?
271
+ r = make_options(options, [:outline], '', :boolean)
272
+ r += make_options(options, [:outline_depth], '', :numeric)
249
273
  end
274
+ r
275
+ end
250
276
 
251
- def parse_outline(options)
252
- r = []
253
- unless options.blank?
254
- r = make_options(options, [:outline], "", :boolean)
255
- r +=make_options(options, [:outline_depth], "", :numeric)
256
- end
257
- r
258
- end
277
+ def parse_margins(options)
278
+ make_options(options, [:top, :bottom, :left, :right], 'margin', :numeric)
279
+ end
259
280
 
260
- def parse_margins(options)
261
- make_options(options, [:top, :bottom, :left, :right], "margin", :numeric)
281
+ def parse_global(options)
282
+ r = []
283
+ unless options.blank?
284
+ r += make_options(options, [:orientation,
285
+ :dpi,
286
+ :page_size,
287
+ :page_width,
288
+ :title])
289
+ r += make_options(options, [:lowquality,
290
+ :grayscale,
291
+ :no_pdf_compression], '', :boolean)
292
+ r += make_options(options, [:image_dpi,
293
+ :image_quality,
294
+ :page_height], '', :numeric)
295
+ r += parse_margins(options.delete(:margin))
262
296
  end
297
+ r
298
+ end
263
299
 
264
- def parse_others(options)
265
- r = []
266
- unless options.blank?
267
- r += make_options(options, [ :orientation,
268
- :page_size,
269
- :page_width,
270
- :page_height,
271
- :proxy,
272
- :username,
273
- :password,
274
- :dpi,
275
- :encoding,
276
- :user_style_sheet,
277
- :viewport_size])
278
- r +=make_options(options, [ :cookie,
279
- :post], "", :name_value)
280
- r +=make_options(options, [ :redirect_delay,
281
- :zoom,
282
- :page_offset,
283
- :javascript_delay,
284
- :image_quality], "", :numeric)
285
- r +=make_options(options, [ :book,
286
- :default_header,
287
- :disable_javascript,
288
- :grayscale,
289
- :lowquality,
290
- :enable_plugins,
291
- :disable_internal_links,
292
- :disable_external_links,
293
- :print_media_type,
294
- :disable_smart_shrinking,
295
- :use_xserver,
296
- :no_background], "", :boolean)
297
- r +=make_options(options, [ :no_stop_slow_scripts ], "", nil)
298
- end
299
- r
300
+ def parse_others(options)
301
+ r = []
302
+ unless options.blank?
303
+ r += make_options(options, [:proxy,
304
+ :username,
305
+ :password,
306
+ :encoding,
307
+ :user_style_sheet,
308
+ :viewport_size])
309
+ r += make_options(options, [:cookie,
310
+ :post], '', :name_value)
311
+ r += make_options(options, [:redirect_delay,
312
+ :zoom,
313
+ :page_offset,
314
+ :javascript_delay], '', :numeric)
315
+ r += make_options(options, [:book,
316
+ :default_header,
317
+ :disable_javascript,
318
+ :enable_plugins,
319
+ :disable_internal_links,
320
+ :disable_external_links,
321
+ :print_media_type,
322
+ :disable_smart_shrinking,
323
+ :use_xserver,
324
+ :no_background], '', :boolean)
325
+ r += make_options(options, [:no_stop_slow_scripts], '', nil)
300
326
  end
327
+ r
328
+ end
301
329
 
302
- def find_wkhtmltopdf_binary_path
303
- possible_locations = (ENV['PATH'].split(':')+%w[/usr/bin /usr/local/bin ~/bin]).uniq
304
- exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
305
- exe_path ||= begin
306
- (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
307
- rescue Exception => e
308
- nil
309
- end
310
- exe_path ||= possible_locations.map{|l| File.expand_path("#{l}/#{EXE_NAME}") }.find{|location| File.exists? location}
311
- exe_path || ''
330
+ def find_wkhtmltopdf_binary_path
331
+ possible_locations = (ENV['PATH'].split(':') + %w(/usr/bin /usr/local/bin ~/bin)).uniq
332
+ exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
333
+ exe_path ||= begin
334
+ detected_path = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
335
+ detected_path.present? && detected_path
336
+ rescue
337
+ nil
312
338
  end
339
+ exe_path ||= possible_locations.map { |l| File.expand_path("#{l}/#{EXE_NAME}") }.find { |location| File.exist?(location) }
340
+ exe_path || ''
341
+ end
313
342
  end