wicked_pdf 0.9.4 → 0.9.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -27,9 +27,11 @@ or add this to your Gemfile:
27
27
 
28
28
  gem 'wicked_pdf'
29
29
 
30
- You may also need to add
31
- Mime::Type.register "application/pdf", :pdf
32
- to config/initializers/mime_types.rb
30
+ You may also need to add
31
+ ```ruby
32
+ Mime::Type.register "application/pdf", :pdf
33
+ ```
34
+ to `config/initializers/mime_types.rb`
33
35
 
34
36
  ### Basic Usage
35
37
 
data/lib/pdf_helper.rb CHANGED
@@ -38,7 +38,7 @@ module PdfHelper
38
38
  private
39
39
 
40
40
  def log_pdf_creation
41
- logger.info '*'*15 + 'WICKED' + '*'*15
41
+ logger.info '*'*15 + 'WICKED' + '*'*15 unless logger.nil?
42
42
  end
43
43
 
44
44
  def set_basic_auth(options={})
@@ -91,7 +91,6 @@ module PdfHelper
91
91
  render_opts.merge!({:file => options[hf][:html][:file]}) if options[:file]
92
92
  tf.write render_to_string(render_opts)
93
93
  tf.flush
94
- options[hf][:html].delete(:template)
95
94
  options[hf][:html][:url] = "file:///#{tf.path}"
96
95
  end
97
96
  end
data/lib/wicked_pdf.rb CHANGED
@@ -19,6 +19,7 @@ require 'wicked_pdf_tempfile'
19
19
  require 'wicked_pdf_middleware'
20
20
 
21
21
  class WickedPdf
22
+ DEFAULT_BINARY_VERSION = Gem::Version.new('0.9.9')
22
23
  EXE_NAME = "wkhtmltopdf"
23
24
  @@config = {}
24
25
  cattr_accessor :config
@@ -28,11 +29,25 @@ class WickedPdf
28
29
  raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
29
30
  raise "Bad #{EXE_NAME}'s path" unless File.exists?(@exe_path)
30
31
  raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
32
+
33
+ @binary_version = DEFAULT_BINARY_VERSION
34
+ end
35
+
36
+ def retreive_binary_version
37
+ begin
38
+ stdin, stdout, stderr = Open3.popen3(@exe_path + ' -V')
39
+ @binary_version = parse_version(stdout.gets(nil))
40
+ rescue StandardError
41
+ end
31
42
  end
32
43
 
33
44
  def pdf_from_string(string, options={})
45
+ if WickedPdf.config[:retreive_version]
46
+ retreive_binary_version
47
+ end
48
+
34
49
  temp_path = options.delete(:temp_path)
35
- string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
50
+ string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
36
51
  string_file.write(string)
37
52
  string_file.close
38
53
  generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", temp_path)
@@ -44,10 +59,13 @@ class WickedPdf
44
59
  generated_pdf_file.rewind
45
60
  generated_pdf_file.binmode
46
61
  pdf = generated_pdf_file.read
47
- raise "PDF could not be generated!" if pdf and pdf.rstrip.length == 0
62
+ raise "PDF could not be generated!\n Command Error: #{err}" if pdf and pdf.rstrip.length == 0
48
63
  pdf
49
64
  rescue Exception => e
50
65
  raise "Failed to execute:\n#{command}\nError: #{e}"
66
+ ensure
67
+ string_file.close! if string_file
68
+ generated_pdf_file.close! if generated_pdf_file
51
69
  end
52
70
 
53
71
  private
@@ -57,6 +75,10 @@ class WickedPdf
57
75
  RAILS_ENV == 'development' if defined?(RAILS_ENV)
58
76
  end
59
77
 
78
+ def get_binary_version
79
+ @binary_version
80
+ end
81
+
60
82
  def on_windows?
61
83
  RbConfig::CONFIG['target_os'] == 'mingw32'
62
84
  end
@@ -65,6 +87,15 @@ class WickedPdf
65
87
  p "*"*15 + cmd + "*"*15
66
88
  end
67
89
 
90
+ def parse_version(version_info)
91
+ match_data = /wkhtmltopdf\s*(\d*\.\d*\.\d*\w*)/.match(version_info)
92
+ if (match_data && (2 == match_data.length))
93
+ Gem::Version.new(match_data[1])
94
+ else
95
+ DEFAULT_BINARY_VERSION
96
+ end
97
+ end
98
+
68
99
  def parse_options(options)
69
100
  [
70
101
  parse_extra(options),
@@ -56,9 +56,12 @@ module WickedPdfHelper
56
56
 
57
57
  private
58
58
 
59
+ # borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
60
+ URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
61
+
59
62
  def asset_pathname(source)
60
63
  if Rails.configuration.assets.compile == false
61
- if ActionController::Base.asset_host
64
+ if asset_path(source) =~ URI_REGEXP
62
65
  # asset_path returns an absolute URL using asset_host if asset_host is set
63
66
  asset_path(source)
64
67
  else
@@ -71,7 +74,7 @@ module WickedPdfHelper
71
74
 
72
75
  def read_asset(source)
73
76
  if Rails.configuration.assets.compile == false
74
- if ActionController::Base.asset_host
77
+ if asset_path(source) =~ URI_REGEXP
75
78
  require 'open-uri'
76
79
  open(asset_pathname(source), 'r:UTF-8') {|f| f.read }
77
80
  else
@@ -3,7 +3,7 @@ class WickedPdf
3
3
 
4
4
  def initialize(app, options = {}, conditions = {})
5
5
  @app = app
6
- @options = options
6
+ @options = (WickedPdf.config || {}).merge(options)
7
7
  @conditions = conditions
8
8
  end
9
9
 
@@ -18,7 +18,8 @@ class WickedPdf
18
18
  body = response.respond_to?(:body) ? response.body : response.join
19
19
  body = body.join if body.is_a?(Array)
20
20
 
21
- body = WickedPdf.new.pdf_from_string(translate_paths(body, env))
21
+ body = WickedPdf.new(@options[:wkhtmltopdf]).pdf_from_string(translate_paths(body, env), @options)
22
+
22
23
  response = [body]
23
24
 
24
25
  # Do not cache PDFs
@@ -27,6 +28,10 @@ class WickedPdf
27
28
 
28
29
  headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
29
30
  headers["Content-Type"] = "application/pdf"
31
+ if @options.fetch(:disposition, '') == 'attachment'
32
+ headers["Content-Disposition"] = 'attachment'
33
+ headers["Content-Transfer-Encoding"] = 'binary'
34
+ end
30
35
  end
31
36
 
32
37
  [status, headers, response]
@@ -87,4 +92,4 @@ class WickedPdf
87
92
  end
88
93
 
89
94
  end
90
- end
95
+ end
@@ -1,3 +1,3 @@
1
1
  class WickedPdf
2
- VERSION = '0.9.4'
2
+ VERSION = '0.9.5'
3
3
  end
@@ -21,7 +21,6 @@ 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 !options[:header][:html].has_key?(:template)
25
24
  assert_match /^file:\/\/\/.*wicked_header_pdf.*\.html/, options[:header][:html][:url]
26
25
  end
27
26
  end
data/test/test_helper.rb CHANGED
@@ -3,9 +3,9 @@ ENV["RAILS_ENV"] = "test"
3
3
 
4
4
  require File.expand_path("../dummy/config/environment.rb", __FILE__)
5
5
  if Rails::VERSION::MAJOR == 2
6
- require "test_help"
6
+ require "test_help"
7
7
  else
8
- require "rails/test_help"
8
+ require "rails/test_help"
9
9
  end
10
10
 
11
11
  require "wicked_pdf"
@@ -11,6 +11,9 @@ class WickedPdf
11
11
  end
12
12
 
13
13
  class WickedPdfTest < ActiveSupport::TestCase
14
+ def setup
15
+ @wp = WickedPdf.new
16
+ end
14
17
 
15
18
  test "should generate PDF from html document" do
16
19
  wp = WickedPdf.new
@@ -153,4 +156,27 @@ class WickedPdfTest < ActiveSupport::TestCase
153
156
  assert_equal "--#{o.to_s.gsub('_', '-')}", wp.get_parsed_options(o => true).strip
154
157
  end
155
158
  end
159
+
160
+ test "should extract old wkhtmltopdf version" do
161
+ version_info_sample = "Name:\n wkhtmltopdf 0.9.9\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
162
+ assert_equal WickedPdf::DEFAULT_BINARY_VERSION, @wp.send(:parse_version, version_info_sample)
163
+ end
164
+
165
+ test "should extract new wkhtmltopdf version" do
166
+ version_info_sample = "Name:\n wkhtmltopdf 0.11.0 rc2\n\nLicense:\n Copyright (C) 2010 wkhtmltopdf/wkhtmltoimage Authors.\n\n\n\n License LGPLv3+: GNU Lesser General Public License version 3 or later\n <http://gnu.org/licenses/lgpl.html>. This is free software: you are free to\n change and redistribute it. There is NO WARRANTY, to the extent permitted by\n law.\n\nAuthors:\n Written by Jan Habermann, Christian Sciberras and Jakob Truelsen. Patches by\n Mehdi Abbad, Lyes Amazouz, Pascal Bach, Emmanuel Bouthenot, Benoit Garret and\n Mario Silva."
167
+ assert_equal Gem::Version.new('0.11.0'), @wp.send(:parse_version, version_info_sample)
168
+ end
169
+
170
+ test "should extract wkhtmltopdf version with nondigit symbols" do
171
+ version_info_sample = "Name:\n wkhtmltopdf 0.10.4b\n\nLicense:\n Copyright (C) 2008,2009 Wkhtmltopdf Authors.\n\n\n\n License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n This is free software: you are free to change and redistribute it. There is NO\n WARRANTY, to the extent permitted by law.\n\nAuthors:\n Written by Jakob Truelsen. Patches by Mrio Silva, Benoit Garret and Emmanuel\n Bouthenot.\n"
172
+ assert_equal Gem::Version.new('0.10.4b'), @wp.send(:parse_version, version_info_sample)
173
+ end
174
+
175
+ test "should fallback to default version on parse error" do
176
+ assert_equal WickedPdf::DEFAULT_BINARY_VERSION, @wp.send(:parse_version, '')
177
+ end
178
+
179
+ test "should set Default version on initialize" do
180
+ assert_equal WickedPdf::DEFAULT_BINARY_VERSION, @wp.send(:get_binary_version)
181
+ end
156
182
  end
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wicked_pdf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 51
5
- prerelease:
4
+ prerelease: false
6
5
  segments:
7
6
  - 0
8
7
  - 9
9
- - 4
10
- version: 0.9.4
8
+ - 5
9
+ version: 0.9.5
11
10
  platform: ruby
12
11
  authors:
13
12
  - Miles Z. Sterret
@@ -15,17 +14,16 @@ autorequire:
15
14
  bindir: bin
16
15
  cert_chain: []
17
16
 
18
- date: 2013-01-17 00:00:00 Z
17
+ date: 2013-04-12 00:00:00 -04:00
18
+ default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: rails
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
28
- hash: 3
29
27
  segments:
30
28
  - 0
31
29
  version: "0"
@@ -35,11 +33,9 @@ dependencies:
35
33
  name: rake
36
34
  prerelease: false
37
35
  requirement: &id002 !ruby/object:Gem::Requirement
38
- none: false
39
36
  requirements:
40
37
  - - ">="
41
38
  - !ruby/object:Gem::Version
42
- hash: 3
43
39
  segments:
44
40
  - 0
45
41
  version: "0"
@@ -49,11 +45,9 @@ dependencies:
49
45
  name: sqlite3
50
46
  prerelease: false
51
47
  requirement: &id003 !ruby/object:Gem::Requirement
52
- none: false
53
48
  requirements:
54
49
  - - ">="
55
50
  - !ruby/object:Gem::Version
56
- hash: 3
57
51
  segments:
58
52
  - 0
59
53
  version: "0"
@@ -122,6 +116,7 @@ files:
122
116
  - test/functional/wicked_pdf_helper_test.rb
123
117
  - test/test_helper.rb
124
118
  - test/unit/wicked_pdf_test.rb
119
+ has_rdoc: true
125
120
  homepage: https://github.com/mileszs/wicked_pdf
126
121
  licenses: []
127
122
 
@@ -131,27 +126,23 @@ rdoc_options: []
131
126
  require_paths:
132
127
  - lib
133
128
  required_ruby_version: !ruby/object:Gem::Requirement
134
- none: false
135
129
  requirements:
136
130
  - - ">="
137
131
  - !ruby/object:Gem::Version
138
- hash: 3
139
132
  segments:
140
133
  - 0
141
134
  version: "0"
142
135
  required_rubygems_version: !ruby/object:Gem::Requirement
143
- none: false
144
136
  requirements:
145
137
  - - ">="
146
138
  - !ruby/object:Gem::Version
147
- hash: 3
148
139
  segments:
149
140
  - 0
150
141
  version: "0"
151
142
  requirements: []
152
143
 
153
144
  rubyforge_project:
154
- rubygems_version: 1.8.15
145
+ rubygems_version: 1.3.6
155
146
  signing_key:
156
147
  specification_version: 3
157
148
  summary: PDF generator (from HTML) gem for Ruby on Rails