wicked_pdf 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -54,6 +54,7 @@ to config/initializers/mime_types.rb
54
54
  render :pdf => 'file_name',
55
55
  :disposition => 'attachment', # default 'inline'
56
56
  :template => 'things/show.pdf.erb',
57
+ :file => "#{Rails.root}/files/foo.erb"
57
58
  :layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file
58
59
  :wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary
59
60
  :show_as_html => params[:debug].present?, # allow debuging based on url param
@@ -156,7 +157,7 @@ If you need to just create a pdf and not display it:
156
157
  WickedPdf.new.pdf_from_string(
157
158
  render_to_string(:pdf => "pdf_file.pdf", :template => 'templates/pdf.html.erb', :layout => 'pdfs/layout_pdf'),
158
159
  :footer => {:content => render_to_string({:template => 'templates/pdf_footer.html.erb', :layout => 'pdfs/layout_pdf'})}
159
-
160
+ )
160
161
  # or from your controller, using views & templates and all wicked_pdf options as normal
161
162
  pdf = render_to_string :pdf => "some_file_name"
162
163
 
@@ -226,6 +227,22 @@ If you do not have explicit page breaks (and therefore do not have any "page" cl
226
227
 
227
228
  You can put your default configuration, applied to all pdf's at "wicked_pdf.rb" initializer.
228
229
 
230
+ ### Rack Middleware
231
+
232
+ If you would like to have WickedPdf automatically generate PDF views for all (or nearly all) pages by appending .pdf to the URL, add the following to your Rails app:
233
+
234
+ # in application.rb (Rails3) or environment.rb (Rails2)
235
+ require 'wicked_pdf'
236
+ config.middleware.use WickedPdf::Middleware
237
+
238
+ If you want to turn on or off the middleware for certain urls, use the `:only` or `:except` conditions like so:
239
+
240
+ # conditions can be plain strings or regular expressions, and you can supply only one or an array
241
+ config.middleware.use WickedPdf::Middleware, {}, :only => '/invoice'
242
+ config.middleware.use WickedPdf::Middleware, {}, :except => [ %r[^/admin], '/secret', %r[^/people/\d] ]
243
+
244
+ If you use the standard `render :pdf => 'some_pdf'` in your app, you will want to exclude those actions from the middleware.
245
+
229
246
  ### Further Reading
230
247
 
231
248
  Andreas Happe's post [Generating PDFs from Ruby on Rails](http://www.snikt.net/blog/2012/04/26/wicked-pdf/)
data/lib/pdf_helper.rb CHANGED
@@ -55,7 +55,7 @@ module PdfHelper
55
55
  end
56
56
 
57
57
  def make_pdf(options = {})
58
- html_string = render_to_string(:template => options[:template], :layout => options[:layout], :formats => options[:formats], :handlers => options[:handlers])
58
+ html_string = render_to_string(:template => options[:template], :file => options[:file], :layout => options[:layout], :formats => options[:formats], :handlers => options[:handlers])
59
59
  options = prerender_header_and_footer(options)
60
60
  w = WickedPdf.new(options[:wkhtmltopdf])
61
61
  w.pdf_from_string(html_string, options)
@@ -67,7 +67,7 @@ module PdfHelper
67
67
  options[:template] ||= File.join(controller_path, action_name)
68
68
  options[:disposition] ||= "inline"
69
69
  if options[:show_as_html]
70
- render :template => options[:template], :layout => options[:layout], :formats => options[:formats], :handlers => options[:handlers], :content_type => "text/html"
70
+ render :template => options[:template], :file => options[:file], :layout => options[:layout], :formats => options[:formats], :handlers => options[:handlers], :content_type => "text/html"
71
71
  else
72
72
  pdf_content = make_pdf(options)
73
73
  File.open(options[:save_to_file], 'wb') {|file| file << pdf_content } if options[:save_to_file]
@@ -83,7 +83,7 @@ module PdfHelper
83
83
  @hf_tempfiles = [] if ! defined?(@hf_tempfiles)
84
84
  @hf_tempfiles.push( tf=WickedPdfTempfile.new("wicked_#{hf}_pdf.html") )
85
85
  options[hf][:html][:layout] ||= options[:layout]
86
- tf.write render_to_string(:template => options[hf][:html][:template], :layout => options[hf][:html][:layout], :locals => options[hf][:html][:locals], :formats => options[hf][:html][:formats], :handlers => options[hf][:html][:handlers])
86
+ tf.write render_to_string(:template => options[hf][:html][:template], :file => options[hf][:html][:file], :layout => options[hf][:html][:layout], :locals => options[hf][:html][:locals], :formats => options[hf][:html][:formats], :handlers => options[hf][:html][:handlers])
87
87
  tf.flush
88
88
  options[hf][:html].delete(:template)
89
89
  options[hf][:html][:url] = "file:///#{tf.path}"
data/lib/wicked_pdf.rb CHANGED
@@ -15,6 +15,7 @@ end
15
15
 
16
16
  require 'wicked_pdf_railtie'
17
17
  require 'wicked_pdf_tempfile'
18
+ require 'wicked_pdf_middleware'
18
19
 
19
20
  class WickedPdf
20
21
  EXE_NAME = "wkhtmltopdf"
@@ -32,7 +33,7 @@ class WickedPdf
32
33
  string_file = WickedPdfTempfile.new("wicked_pdf.html")
33
34
  string_file.write(string)
34
35
  string_file.close
35
-
36
+
36
37
  generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf")
37
38
  command = "\"#{@exe_path}\" #{'-q ' unless on_windows?}#{parse_options(options)} \"file://#{string_file.path}\" \"#{generated_pdf_file.path}\" " # -q for no errors on stdout
38
39
  print_command(command) if in_development_mode?
@@ -53,7 +53,7 @@ module WickedPdfHelper
53
53
  # asset_path returns an absolute URL using asset_host if asset_host is set
54
54
  asset_path(source)
55
55
  else
56
- File.join(Rails.public_path, asset_path(source))
56
+ File.join(Rails.public_path, asset_path(source).sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
57
57
  end
58
58
  else
59
59
  Rails.application.assets.find_asset(source).pathname
@@ -0,0 +1,90 @@
1
+ class WickedPdf
2
+ class Middleware
3
+
4
+ def initialize(app, options = {}, conditions = {})
5
+ @app = app
6
+ @options = options
7
+ @conditions = conditions
8
+ end
9
+
10
+ def call(env)
11
+ @request = Rack::Request.new(env)
12
+ @render_pdf = false
13
+
14
+ set_request_to_render_as_pdf(env) if render_as_pdf?
15
+ status, headers, response = @app.call(env)
16
+
17
+ if rendering_pdf? && headers['Content-Type'] =~ /text\/html|application\/xhtml\+xml/
18
+ body = response.respond_to?(:body) ? response.body : response.join
19
+ body = body.join if body.is_a?(Array)
20
+
21
+ body = WickedPdf.new.pdf_from_string(translate_paths(body, env))
22
+ response = [body]
23
+
24
+ # Do not cache PDFs
25
+ headers.delete('ETag')
26
+ headers.delete('Cache-Control')
27
+
28
+ headers["Content-Length"] = (body.respond_to?(:bytesize) ? body.bytesize : body.size).to_s
29
+ headers["Content-Type"] = "application/pdf"
30
+ end
31
+
32
+ [status, headers, response]
33
+ end
34
+
35
+ private
36
+
37
+ # Change relative paths to absolute
38
+ def translate_paths(body, env)
39
+ # Host with protocol
40
+ root = WickedPdf.config[:root_url] || "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
41
+
42
+ body.gsub(/(href|src)=(['"])\/([^\"']*|[^"']*)['"]/, '\1=\2' + root + '\3\2')
43
+ end
44
+
45
+ def rendering_pdf?
46
+ @render_pdf
47
+ end
48
+
49
+ def render_as_pdf?
50
+ request_path_is_pdf = @request.path.match(%r{\.pdf$})
51
+
52
+ if request_path_is_pdf && @conditions[:only]
53
+ rules = [@conditions[:only]].flatten
54
+ rules.any? do |pattern|
55
+ if pattern.is_a?(Regexp)
56
+ @request.path =~ pattern
57
+ else
58
+ @request.path[0, pattern.length] == pattern
59
+ end
60
+ end
61
+ elsif request_path_is_pdf && @conditions[:except]
62
+ rules = [@conditions[:except]].flatten
63
+ rules.map do |pattern|
64
+ if pattern.is_a?(Regexp)
65
+ return false if @request.path =~ pattern
66
+ else
67
+ return false if @request.path[0, pattern.length] == pattern
68
+ end
69
+ end
70
+
71
+ return true
72
+ else
73
+ request_path_is_pdf
74
+ end
75
+ end
76
+
77
+ def set_request_to_render_as_pdf(env)
78
+ @render_pdf = true
79
+ path = @request.path.sub(%r{\.pdf$}, '')
80
+ %w[PATH_INFO REQUEST_URI].each { |e| env[e] = path }
81
+ env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
82
+ env["Rack-Middleware-WickedPdf"] = "true"
83
+ end
84
+
85
+ def concat(accepts, type)
86
+ (accepts || '').split(',').unshift(type).compact.join(',')
87
+ end
88
+
89
+ end
90
+ end
metadata CHANGED
@@ -1,63 +1,62 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: wicked_pdf
3
- version: !ruby/object:Gem::Version
4
- hash: 59
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.1
5
5
  prerelease:
6
- segments:
7
- - 0
8
- - 9
9
- - 0
10
- version: 0.9.0
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Miles Z. Sterret
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2012-12-20 00:00:00 Z
19
- dependencies:
20
- - !ruby/object:Gem::Dependency
12
+ date: 2013-01-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
21
15
  name: rails
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
16
+ requirement: !ruby/object:Gem::Requirement
24
17
  none: false
25
- requirements:
26
- - - ">="
27
- - !ruby/object:Gem::Version
28
- hash: 3
29
- segments:
30
- - 0
31
- version: "0"
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
32
22
  type: :runtime
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
35
- name: rake
36
23
  prerelease: false
37
- requirement: &id002 !ruby/object:Gem::Requirement
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
38
33
  none: false
39
- requirements:
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- hash: 3
43
- segments:
44
- - 0
45
- version: "0"
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
46
38
  type: :development
47
- version_requirements: *id002
48
- description: |
49
- Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: ! 'Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file
47
+ to a user from HTML.
48
+
50
49
  In other words, rather than dealing with a PDF generation DSL of some sort,
51
- you simply write an HTML view as you would normally, and let Wicked take care of the hard stuff.
52
50
 
51
+ you simply write an HTML view as you would normally, and let Wicked take care of
52
+ the hard stuff.
53
+
54
+ '
53
55
  email: miles.sterrett@gmail.com
54
56
  executables: []
55
-
56
57
  extensions: []
57
-
58
58
  extra_rdoc_files: []
59
-
60
- files:
59
+ files:
61
60
  - README.md
62
61
  - Rakefile
63
62
  - MIT-LICENSE
@@ -65,6 +64,7 @@ files:
65
64
  - lib/pdf_helper.rb
66
65
  - lib/wicked_pdf.rb
67
66
  - lib/wicked_pdf_helper.rb
67
+ - lib/wicked_pdf_middleware.rb
68
68
  - lib/wicked_pdf_railtie.rb
69
69
  - lib/wicked_pdf_tempfile.rb
70
70
  - test/fixtures/document_with_long_line.html
@@ -77,36 +77,26 @@ files:
77
77
  - generators/wicked_pdf/wicked_pdf_generator.rb
78
78
  homepage: https://github.com/mileszs/wicked_pdf
79
79
  licenses: []
80
-
81
80
  post_install_message:
82
81
  rdoc_options: []
83
-
84
- require_paths:
82
+ require_paths:
85
83
  - lib
86
- required_ruby_version: !ruby/object:Gem::Requirement
84
+ required_ruby_version: !ruby/object:Gem::Requirement
87
85
  none: false
88
- requirements:
89
- - - ">="
90
- - !ruby/object:Gem::Version
91
- hash: 3
92
- segments:
93
- - 0
94
- version: "0"
95
- required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ! '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
96
91
  none: false
97
- requirements:
98
- - - ">="
99
- - !ruby/object:Gem::Version
100
- hash: 3
101
- segments:
102
- - 0
103
- version: "0"
92
+ requirements:
93
+ - - ! '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
104
96
  requirements: []
105
-
106
97
  rubyforge_project:
107
- rubygems_version: 1.8.21
98
+ rubygems_version: 1.8.24
108
99
  signing_key:
109
100
  specification_version: 3
110
101
  summary: PDF generator (from HTML) plugin for Ruby on Rails
111
102
  test_files: []
112
-