wicked_pdf 0.7.2 → 0.7.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## A PDF generation plugin for Ruby on Rails
4
4
 
5
- Wicked PDF uses the shell utility [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, and let Wicked take care of the hard stuff.
5
+ Wicked PDF uses the shell utility [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) to serve a PDF file to a user from HTML. In other words, rather than dealing with a PDF generation DSL of some sort, you simply write an HTML view as you would normally, then let Wicked take care of the hard stuff.
6
6
 
7
7
  _Wicked PDF has been verified to work on Ruby 1.8.7 and 1.9.2; Rails 2 and Rails 3_
8
8
 
@@ -18,6 +18,11 @@ Next:
18
18
 
19
19
  script/plugin install git://github.com/mileszs/wicked_pdf.git
20
20
  script/generate wicked_pdf
21
+
22
+ or add this to your Gemfile:
23
+
24
+ gem 'wicked_pdf'
25
+
21
26
  ### Basic Usage
22
27
 
23
28
  class ThingsController < ApplicationController
@@ -40,7 +45,7 @@ Next:
40
45
  format.pdf do
41
46
  render :pdf => 'file_name',
42
47
  :template => 'things/show.pdf.erb',
43
- :layout => 'pdf.html', # use 'pdf.html' for a pfd.html.erb file
48
+ :layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file
44
49
  :wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary
45
50
  :show_as_html => params[:debug].present?, # allow debuging based on url param
46
51
  :orientation => 'Landscape', # default Portrait
@@ -55,6 +60,8 @@ Next:
55
60
  :dpi => 'dpi',
56
61
  :encoding => 'TEXT',
57
62
  :user_style_sheet => 'URL',
63
+ :cookie => ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
64
+ :post => ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
58
65
  :redirect_delay => NUMBER,
59
66
  :zoom => FLOAT,
60
67
  :page_offset => NUMBER,
@@ -74,7 +81,8 @@ Next:
74
81
  :bottom => SIZE,
75
82
  :left => SIZE,
76
83
  :right => SIZE},
77
- :header => {:html => { :template => 'users/header.pdf.erb', # use :template OR :url
84
+ :header => {:html => { :template => 'users/header.pdf.erb', # use :template OR :url
85
+ :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
78
86
  :url => 'www.example.com',
79
87
  :locals => { :foo => @bar }},
80
88
  :center => 'TEXT',
@@ -85,6 +93,7 @@ Next:
85
93
  :spacing => REAL,
86
94
  :line => true},
87
95
  :footer => {:html => { :template => 'shared/footer.pdf.erb', # use :template OR :url
96
+ :layout => 'pdf_plain.html', # optional, use 'pdf_plain.html' for a pdf_plain.html.erb file, defaults to main layout
88
97
  :url => 'www.example.com',
89
98
  :locals => { :foo => @bar }},
90
99
  :center => 'TEXT',
@@ -146,7 +155,7 @@ If you need to display utf encoded characters, add this to your pdf views or lay
146
155
 
147
156
  ### Styles
148
157
 
149
- You must define absolute path's to CSS files, images, and javascripts; the best option is to use the *wicked_pdf_stylesheet_link_tag*, *wicked_pdf_image_tag*, and *wicked_pdf_javascript_include_tag* helpers.
158
+ You must define absolute paths to CSS files, images, and javascripts; the best option is to use the *wicked_pdf_stylesheet_link_tag*, *wicked_pdf_image_tag*, and *wicked_pdf_javascript_include_tag* helpers.
150
159
 
151
160
  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
152
161
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
@@ -168,7 +177,7 @@ You must define absolute path's to CSS files, images, and javascripts; the best
168
177
 
169
178
  ### Page Numbering
170
179
 
171
- A bit of javascript can help you number your pages, create a template or header/footer file with this:
180
+ A bit of javascript can help you number your pages. Create a template or header/footer file with this:
172
181
 
173
182
  <html>
174
183
  <head>
@@ -202,7 +211,9 @@ You can put your default configuration, applied to all pdf's at "wicked_pdf.rb"
202
211
 
203
212
  ### Further Reading
204
213
 
205
- Andreas Happe's post [Generating PDFs from Ruby on Rails](http://snikt.net/index.php/2010/03/03/generating-pdfs-from-ruby-on-rails)
214
+ Andreas Happe's post [Generating PDFs from Ruby on Rails](http://starseeders.net/generating-pdfs-from-ruby-on-rails/)
215
+
216
+ StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf)
206
217
 
207
218
  ### Debugging
208
219
 
data/lib/pdf_helper.rb CHANGED
@@ -78,7 +78,8 @@ module PdfHelper
78
78
  if options[hf] && options[hf][:html] && options[hf][:html][:template]
79
79
  @hf_tempfiles = [] if ! defined?(@hf_tempfiles)
80
80
  @hf_tempfiles.push( tf=WickedPdfTempfile.new("wicked_#{hf}_pdf.html") )
81
- tf.write render_to_string(:template => options[hf][:html][:template], :layout => options[:layout], :locals => options[hf][:html][:locals])
81
+ options[hf][:html][:layout] ||= options[:layout]
82
+ tf.write render_to_string(:template => options[hf][:html][:template], :layout => options[hf][:html][:layout], :locals => options[hf][:html][:locals])
82
83
  tf.flush
83
84
  options[hf][:html].delete(:template)
84
85
  options[hf][:html][:url] = "file://#{tf.path}"
data/lib/wicked_pdf.rb CHANGED
@@ -3,8 +3,10 @@
3
3
 
4
4
  require 'logger'
5
5
  require 'digest/md5'
6
- require 'open3'
6
+ require 'rbconfig'
7
+ require Config::CONFIG['target_os'] == 'mingw32' ? 'win32/open3' : 'open3'
7
8
  require 'active_support/core_ext/class/attribute_accessors'
9
+ require 'active_support/core_ext/object/blank'
8
10
 
9
11
  require 'wicked_pdf_railtie'
10
12
  require 'wicked_pdf_tempfile'
@@ -16,15 +18,15 @@ class WickedPdf
16
18
  def initialize(wkhtmltopdf_binary_path = nil)
17
19
  @exe_path = wkhtmltopdf_binary_path
18
20
  @exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
19
- @exe_path ||= `which wkhtmltopdf`.chomp
21
+ @exe_path ||= (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
20
22
  raise "Location of wkhtmltopdf unknown" if @exe_path.empty?
21
23
  raise "Bad wkhtmltopdf's path" unless File.exists?(@exe_path)
22
24
  raise "Wkhtmltopdf is not executable" unless File.executable?(@exe_path)
23
25
  end
24
26
 
25
27
  def pdf_from_string(string, options={})
26
- command = "#{@exe_path} #{parse_options(options)} -q - - " # -q for no errors on stdout
27
- p "*"*15 + command + "*"*15 unless defined?(Rails) and Rails.env != 'development'
28
+ command = "\"#{@exe_path}\" #{parse_options(options)} #{'-q ' unless on_windows?}- - " # -q for no errors on stdout
29
+ print_command(command) if in_development_mode?
28
30
  pdf, err = Open3.popen3(command) do |stdin, stdout, stderr|
29
31
  stdin.binmode
30
32
  stdout.binmode
@@ -41,6 +43,19 @@ class WickedPdf
41
43
 
42
44
  private
43
45
 
46
+ def in_development_mode?
47
+ (defined?(Rails) && Rails.env == 'development') ||
48
+ (RAILS_ENV && RAILS_ENV == 'development')
49
+ end
50
+
51
+ def on_windows?
52
+ Config::CONFIG['target_os'] == 'mingw32'
53
+ end
54
+
55
+ def print_command(cmd)
56
+ p "*"*15 + cmd + "*"*15
57
+ end
58
+
44
59
  def parse_options(options)
45
60
  [
46
61
  parse_header_footer(:header => options.delete(:header),
@@ -64,10 +79,14 @@ class WickedPdf
64
79
  end
65
80
 
66
81
  def make_option(name, value, type=:string)
82
+ if value.respond_to? :each
83
+ return value.collect { |v| make_option(name, v, type) }.join('')
84
+ end
67
85
  "--#{name.gsub('_', '-')} " + case type
68
86
  when :boolean then ""
69
87
  when :numeric then value.to_s
70
- else "'#{value}'"
88
+ when :name_value then value.to_s
89
+ else "\"#{value}\""
71
90
  end + " "
72
91
  end
73
92
 
@@ -92,8 +111,9 @@ class WickedPdf
92
111
  end
93
112
 
94
113
  def parse_toc(options)
114
+ r = '--toc ' unless options.nil?
95
115
  unless options.blank?
96
- r = make_options(options, [ :font_name, :header_text], "toc")
116
+ r += make_options(options, [ :font_name, :header_text], "toc")
97
117
  r +=make_options(options, [ :depth,
98
118
  :header_fs,
99
119
  :l1_font_size,
@@ -114,6 +134,7 @@ class WickedPdf
114
134
  :disable_links,
115
135
  :disable_back_links], "toc", :boolean)
116
136
  end
137
+ return r
117
138
  end
118
139
 
119
140
  def parse_outline(options)
@@ -140,6 +161,8 @@ class WickedPdf
140
161
  :dpi,
141
162
  :encoding,
142
163
  :user_style_sheet])
164
+ r +=make_options(options, [ :cookie,
165
+ :post], "", :name_value)
143
166
  r +=make_options(options, [ :redirect_delay,
144
167
  :zoom,
145
168
  :page_offset], "", :numeric)
@@ -7,14 +7,35 @@ module WickedPdfHelper
7
7
  end
8
8
 
9
9
  def wicked_pdf_image_tag(img, options={})
10
- image_tag "file://#{Rails.root.join('public', 'images', img)}", options
10
+ image_tag "file:///#{Rails.root.join('public', 'images', img)}", options
11
11
  end
12
12
 
13
13
  def wicked_pdf_javascript_src_tag(jsfile, options={})
14
- javascript_src_tag "file://#{Rails.root.join('public','javascripts',jsfile)}", options
14
+ javascript_src_tag "file:///#{Rails.root.join('public','javascripts',jsfile)}", options
15
15
  end
16
16
 
17
17
  def wicked_pdf_javascript_include_tag(*sources)
18
18
  sources.collect{ |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n").html_safe
19
19
  end
20
+ module Assets
21
+ def wicked_pdf_stylesheet_link_tag(*sources)
22
+ sources.collect { |source|
23
+ "<style type='text/css'>#{Rails.application.assets.find_asset(source+".css")}</style>"
24
+ }.join("\n").html_safe
25
+ end
26
+
27
+ def wicked_pdf_image_tag(img, options={})
28
+ asset = Rails.application.assets.find_asset(img)
29
+ image_tag "file:///#{asset.pathname.to_s}", options
30
+ end
31
+
32
+ def wicked_pdf_javascript_src_tag(jsfile, options={})
33
+ asset = Rails.application.assets.find_asset(jsfile)
34
+ javascript_include_tag "file:///#{asset.pathname.to_s}", options
35
+ end
36
+
37
+ def wicked_pdf_javascript_include_tag(*sources)
38
+ sources.collect{ |source| "<script type='text/javascript'>#{Rails.application.assets.find_asset(source+".js")}</script>" }.join("\n").html_safe
39
+ end
40
+ end
20
41
  end
@@ -2,6 +2,7 @@ require 'pdf_helper'
2
2
  require 'wicked_pdf_helper'
3
3
 
4
4
  if defined?(Rails)
5
+
5
6
  if Rails::VERSION::MAJOR == 2
6
7
 
7
8
  unless ActionController::Base.instance_methods.include? "render_with_wicked_pdf"
@@ -10,17 +11,24 @@ if defined?(Rails)
10
11
  unless ActionView::Base.instance_methods.include? "wicked_pdf_stylesheet_link_tag"
11
12
  ActionView::Base.send :include, WickedPdfHelper
12
13
  end
13
- Mime::Type.register 'application/pdf', :pdf
14
-
14
+
15
15
  else
16
16
 
17
17
  class WickedRailtie < Rails::Railtie
18
18
  initializer "wicked_pdf.register" do |app|
19
19
  ActionController::Base.send :include, PdfHelper
20
- ActionView::Base.send :include, WickedPdfHelper
21
- Mime::Type.register 'application/pdf', :pdf
20
+ if Rails::VERSION::MINOR > 0
21
+ ActionView::Base.send :include, WickedPdfHelper::Assets
22
+ else
23
+ ActionView::Base.send :include, WickedPdfHelper
24
+ end
22
25
  end
23
26
  end
24
27
 
25
28
  end
29
+
30
+ if Mime::Type.lookup_by_extension(:pdf).nil?
31
+ Mime::Type.register('application/pdf', :pdf)
32
+ end
33
+
26
34
  end
@@ -1,4 +1,5 @@
1
1
  require 'test_helper'
2
+ require 'action_view/test_case'
2
3
 
3
4
  class WickedPdfHelperTest < ActionView::TestCase
4
5
  test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
@@ -64,7 +64,7 @@ class WickedPdfTest < ActiveSupport::TestCase
64
64
 
65
65
  [:header, :footer].each do |hf|
66
66
  [:center, :font_name, :left, :right].each do |o|
67
- assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} 'header_footer'",
67
+ assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} \"header_footer\"",
68
68
  wp.get_parsed_options(hf => {o => "header_footer"}).strip
69
69
  end
70
70
 
@@ -75,7 +75,7 @@ class WickedPdfTest < ActiveSupport::TestCase
75
75
 
76
76
  assert_equal "--#{hf.to_s}-line",
77
77
  wp.get_parsed_options(hf => {:line => true}).strip
78
- assert_equal "--#{hf.to_s}-html 'http://www.abc.com'",
78
+ assert_equal "--#{hf.to_s}-html \"http://www.abc.com\"",
79
79
  wp.get_parsed_options(hf => {:html => {:url => 'http://www.abc.com'}}).strip
80
80
  end
81
81
  end
@@ -84,7 +84,7 @@ class WickedPdfTest < ActiveSupport::TestCase
84
84
  wp = WickedPdf.new
85
85
 
86
86
  [:font_name, :header_text].each do |o|
87
- assert_equal "--toc-#{o.to_s.gsub('_', '-')} 'toc'",
87
+ assert_equal "--toc-#{o.to_s.gsub('_', '-')} \"toc\"",
88
88
  wp.get_parsed_options(:toc => {o => "toc"}).strip
89
89
  end
90
90
 
@@ -123,7 +123,14 @@ class WickedPdfTest < ActiveSupport::TestCase
123
123
  [ :orientation, :page_size, :proxy, :username, :password, :cover, :dpi,
124
124
  :encoding, :user_style_sheet
125
125
  ].each do |o|
126
- assert_equal "--#{o.to_s.gsub('_', '-')} 'opts'", wp.get_parsed_options(o => "opts").strip
126
+ assert_equal "--#{o.to_s.gsub('_', '-')} \"opts\"", wp.get_parsed_options(o => "opts").strip
127
+ end
128
+
129
+ [:cookie, :post].each do |o|
130
+ assert_equal "--#{o.to_s.gsub('_', '-')} name value", wp.get_parsed_options(o => "name value").strip
131
+
132
+ nv_formatter = ->(number){ "--#{o.to_s.gsub('_', '-')} par#{number} val#{number}" }
133
+ assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", wp.get_parsed_options(o => ['par1 val1', 'par2 val2']).strip
127
134
  end
128
135
 
129
136
  [:redirect_delay, :zoom, :page_offset].each do |o|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wicked_pdf
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 7
9
- - 2
10
- version: 0.7.2
9
+ - 3
10
+ version: 0.7.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Miles Z. Sterret
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-07 00:00:00 -04:00
18
+ date: 2012-02-22 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -76,7 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  requirements: []
77
77
 
78
78
  rubyforge_project:
79
- rubygems_version: 1.6.2
79
+ rubygems_version: 1.4.2
80
80
  signing_key:
81
81
  specification_version: 3
82
82
  summary: PDF generator (from HTML) plugin for Ruby on Rails