wicked_pdf 0.7.9 → 0.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +11 -3
- data/lib/pdf_helper.rb +4 -0
- data/lib/wicked_pdf.rb +24 -9
- data/lib/wicked_pdf_helper.rb +47 -12
- data/test/pdf_helper_test.rb +7 -5
- data/test/wicked_pdf_helper_test.rb +17 -15
- data/test/wicked_pdf_test.rb +5 -5
- metadata +35 -8
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Wicked PDF
|
1
|
+
# Wicked PDF [![Build Status](https://secure.travis-ci.org/mileszs/wicked_pdf.png)](http://travis-ci.org/mileszs/wicked_pdf)
|
2
2
|
|
3
3
|
## A PDF generation plugin for Ruby on Rails
|
4
4
|
|
@@ -9,6 +9,10 @@ _Wicked PDF has been verified to work on Ruby 1.8.7 and 1.9.2; Rails 2 and Rails
|
|
9
9
|
### Installation
|
10
10
|
|
11
11
|
First, be sure to install [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/).
|
12
|
+
If your wkhtmltopdf executable is not on your webserver's path, configure it in an initializer:
|
13
|
+
WickedPdf.config = {
|
14
|
+
:exe_path => '/usr/local/bin/wkhtmltopdf'
|
15
|
+
}
|
12
16
|
Note that versions before 0.9.0 [have problems](http://code.google.com/p/wkhtmltopdf/issues/detail?id=82&q=vodnik) on some machines with reading/writing to streams.
|
13
17
|
This plugin relies on streams to communicate with wkhtmltopdf.
|
14
18
|
|
@@ -22,6 +26,10 @@ Next:
|
|
22
26
|
or add this to your Gemfile:
|
23
27
|
|
24
28
|
gem 'wicked_pdf'
|
29
|
+
|
30
|
+
You may also need to add
|
31
|
+
Mime::Type.register "application/pdf", :pdf
|
32
|
+
to config/initializers/mime_types.rb
|
25
33
|
|
26
34
|
### Basic Usage
|
27
35
|
|
@@ -69,7 +77,7 @@ or add this to your Gemfile:
|
|
69
77
|
:book => true,
|
70
78
|
:default_header => true,
|
71
79
|
:disable_javascript => false,
|
72
|
-
:
|
80
|
+
:grayscale => true,
|
73
81
|
:lowquality => true,
|
74
82
|
:enable_plugins => true,
|
75
83
|
:disable_internal_links => true,
|
@@ -220,7 +228,7 @@ You can put your default configuration, applied to all pdf's at "wicked_pdf.rb"
|
|
220
228
|
|
221
229
|
### Further Reading
|
222
230
|
|
223
|
-
Andreas Happe's post [Generating PDFs from Ruby on Rails](http://
|
231
|
+
Andreas Happe's post [Generating PDFs from Ruby on Rails](http://www.snikt.net/blog/2012/04/26/wicked-pdf/)
|
224
232
|
|
225
233
|
StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf)
|
226
234
|
|
data/lib/pdf_helper.rb
CHANGED
@@ -3,6 +3,10 @@ module PdfHelper
|
|
3
3
|
require 'wicked_pdf_tempfile'
|
4
4
|
|
5
5
|
def self.included(base)
|
6
|
+
# Protect from trying to augment modules that appear
|
7
|
+
# as the result of adding other gems.
|
8
|
+
return if base != ActionController::Base
|
9
|
+
|
6
10
|
base.class_eval do
|
7
11
|
alias_method_chain :render, :wicked_pdf
|
8
12
|
alias_method_chain :render_to_string, :wicked_pdf
|
data/lib/wicked_pdf.rb
CHANGED
@@ -6,22 +6,26 @@ require 'digest/md5'
|
|
6
6
|
require 'rbconfig'
|
7
7
|
require RbConfig::CONFIG['target_os'] == 'mingw32' && !(RUBY_VERSION =~ /1.9/) ? 'win32/open3' : 'open3'
|
8
8
|
require 'active_support/core_ext/class/attribute_accessors'
|
9
|
-
|
9
|
+
|
10
|
+
begin
|
11
|
+
require 'active_support/core_ext/object/blank'
|
12
|
+
rescue LoadError
|
13
|
+
require 'active_support/core_ext/blank'
|
14
|
+
end
|
10
15
|
|
11
16
|
require 'wicked_pdf_railtie'
|
12
17
|
require 'wicked_pdf_tempfile'
|
13
18
|
|
14
19
|
class WickedPdf
|
20
|
+
EXE_NAME = "wkhtmltopdf"
|
15
21
|
@@config = {}
|
16
22
|
cattr_accessor :config
|
17
23
|
|
18
24
|
def initialize(wkhtmltopdf_binary_path = nil)
|
19
|
-
@exe_path = wkhtmltopdf_binary_path
|
20
|
-
|
21
|
-
|
22
|
-
raise "
|
23
|
-
raise "Bad wkhtmltopdf's path" unless File.exists?(@exe_path)
|
24
|
-
raise "Wkhtmltopdf is not executable" unless File.executable?(@exe_path)
|
25
|
+
@exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
|
26
|
+
raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
|
27
|
+
raise "Bad #{EXE_NAME}'s path" unless File.exists?(@exe_path)
|
28
|
+
raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
|
25
29
|
end
|
26
30
|
|
27
31
|
def pdf_from_string(string, options={})
|
@@ -44,8 +48,8 @@ class WickedPdf
|
|
44
48
|
private
|
45
49
|
|
46
50
|
def in_development_mode?
|
47
|
-
|
48
|
-
|
51
|
+
return Rails.env == 'development' if defined?(Rails)
|
52
|
+
RAILS_ENV == 'development' if defined?(RAILS_ENV)
|
49
53
|
end
|
50
54
|
|
51
55
|
def on_windows?
|
@@ -196,4 +200,15 @@ class WickedPdf
|
|
196
200
|
end
|
197
201
|
end
|
198
202
|
|
203
|
+
def find_wkhtmltopdf_binary_path
|
204
|
+
possible_locations = (ENV['PATH'].split(':')+%w[/usr/bin /usr/local/bin ~/bin]).uniq
|
205
|
+
exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
|
206
|
+
exe_path ||= begin
|
207
|
+
(defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
208
|
+
rescue Exception => e
|
209
|
+
nil
|
210
|
+
end
|
211
|
+
exe_path ||= possible_locations.map{|l| File.expand_path("#{l}/#{EXE_NAME}") }.find{|location| File.exists? location}
|
212
|
+
exe_path || ''
|
213
|
+
end
|
199
214
|
end
|
data/lib/wicked_pdf_helper.rb
CHANGED
@@ -1,41 +1,76 @@
|
|
1
1
|
module WickedPdfHelper
|
2
|
+
def self.root_path
|
3
|
+
String === Rails.root ? Pathname.new(Rails.root) : Rails.root
|
4
|
+
end
|
5
|
+
|
2
6
|
def wicked_pdf_stylesheet_link_tag(*sources)
|
3
|
-
css_dir =
|
4
|
-
sources.collect { |source|
|
7
|
+
css_dir = WickedPdfHelper.root_path.join('public', 'stylesheets')
|
8
|
+
css_text = sources.collect { |source|
|
5
9
|
"<style type='text/css'>#{File.read(css_dir.join(source+'.css'))}</style>"
|
6
|
-
}.join("\n")
|
10
|
+
}.join("\n")
|
11
|
+
css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
|
7
12
|
end
|
8
13
|
|
9
14
|
def wicked_pdf_image_tag(img, options={})
|
10
|
-
image_tag "file
|
15
|
+
image_tag "file://#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
|
11
16
|
end
|
12
17
|
|
13
18
|
def wicked_pdf_javascript_src_tag(jsfile, options={})
|
14
|
-
javascript_src_tag "file
|
19
|
+
javascript_src_tag "file://#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}", options
|
15
20
|
end
|
16
21
|
|
17
22
|
def wicked_pdf_javascript_include_tag(*sources)
|
18
|
-
sources.collect{ |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
|
23
|
+
js_text = sources.collect{ |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
|
24
|
+
js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
|
19
25
|
end
|
26
|
+
|
20
27
|
module Assets
|
21
28
|
def wicked_pdf_stylesheet_link_tag(*sources)
|
22
29
|
sources.collect { |source|
|
23
|
-
"<style type='text/css'>#{
|
30
|
+
"<style type='text/css'>#{read_asset(source+".css")}</style>"
|
24
31
|
}.join("\n").html_safe
|
25
32
|
end
|
26
33
|
|
27
34
|
def wicked_pdf_image_tag(img, options={})
|
28
|
-
|
29
|
-
image_tag "file:///#{asset.pathname.to_s}", options
|
35
|
+
image_tag "file://#{asset_pathname(img).to_s}", options
|
30
36
|
end
|
31
37
|
|
32
38
|
def wicked_pdf_javascript_src_tag(jsfile, options={})
|
33
|
-
|
34
|
-
javascript_include_tag "file:///#{asset.pathname.to_s}", options
|
39
|
+
javascript_include_tag "file://#{asset_pathname(jsfile).to_s}", options
|
35
40
|
end
|
36
41
|
|
37
42
|
def wicked_pdf_javascript_include_tag(*sources)
|
38
|
-
sources.collect{ |source|
|
43
|
+
sources.collect { |source|
|
44
|
+
"<script type='text/javascript'>#{read_asset(source+".js")}</script>"
|
45
|
+
}.join("\n").html_safe
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def asset_pathname(source)
|
51
|
+
if Rails.configuration.assets.compile == false
|
52
|
+
if ActionController::Base.asset_host
|
53
|
+
# asset_path returns an absolute URL using asset_host if asset_host is set
|
54
|
+
asset_path(source)
|
55
|
+
else
|
56
|
+
File.join(Rails.public_path, asset_path(source))
|
57
|
+
end
|
58
|
+
else
|
59
|
+
Rails.application.assets.find_asset(source).pathname
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def read_asset(source)
|
64
|
+
if Rails.configuration.assets.compile == false
|
65
|
+
if ActionController::Base.asset_host
|
66
|
+
require 'open-uri'
|
67
|
+
open(asset_pathname(source)) {|f| f.read }
|
68
|
+
else
|
69
|
+
IO.read(asset_pathname(source))
|
70
|
+
end
|
71
|
+
else
|
72
|
+
Rails.application.assets.find_asset(source).to_s
|
73
|
+
end
|
39
74
|
end
|
40
75
|
end
|
41
76
|
end
|
data/test/pdf_helper_test.rb
CHANGED
@@ -17,10 +17,12 @@ class PdfHelperTest < ActionController::TestCase
|
|
17
17
|
@ac=nil
|
18
18
|
end
|
19
19
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
if Rails::VERSION::MAJOR == 2
|
21
|
+
test "should prerender header and footer :template options" do
|
22
|
+
options = @ac.send( :prerender_header_and_footer,
|
23
|
+
:header => {:html => { :template => 'hf.html.erb'}});
|
24
|
+
assert !options[:header][:html].has_key?(:template)
|
25
|
+
assert_match /^file:\/\/.*wicked_header_pdf.*\.html/, options[:header][:html][:url]
|
26
|
+
end
|
25
27
|
end
|
26
28
|
end
|
@@ -2,23 +2,25 @@ require 'test_helper'
|
|
2
2
|
require 'action_view/test_case'
|
3
3
|
|
4
4
|
class WickedPdfHelperTest < ActionView::TestCase
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
if Rails::VERSION::MAJOR == 2
|
6
|
+
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
|
7
|
+
assert_equal "<style type='text/css'>/* Wicked styles */\n</style>",
|
8
|
+
wicked_pdf_stylesheet_link_tag('../../vendor/plugins/wicked_pdf/test/fixtures/wicked')
|
9
|
+
end
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
|
12
|
+
assert_equal image_tag("file://#{Rails.root.join('public','images','pdf')}"),
|
13
|
+
wicked_pdf_image_tag('pdf')
|
14
|
+
end
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
16
|
+
test 'wicked_pdf_javascript_src_tag should return the same as javascript_src_tag when passed a full path' do
|
17
|
+
assert_equal javascript_src_tag("file://#{Rails.root.join('public','javascripts','pdf')}", {}),
|
18
|
+
wicked_pdf_javascript_src_tag('pdf')
|
19
|
+
end
|
19
20
|
|
20
|
-
|
21
|
-
|
22
|
-
|
21
|
+
test 'wicked_pdf_include_tag should return many wicked_pdf_javascript_src_tags' do
|
22
|
+
assert_equal [wicked_pdf_javascript_src_tag('foo'), wicked_pdf_javascript_src_tag('bar')].join("\n"),
|
23
|
+
wicked_pdf_javascript_include_tag('foo', 'bar')
|
24
|
+
end
|
23
25
|
end
|
24
26
|
end
|
data/test/wicked_pdf_test.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'test_helper'
|
2
2
|
|
3
|
-
WickedPdf.config = { :exe_path => '/usr/local/bin/wkhtmltopdf' }
|
3
|
+
WickedPdf.config = { :exe_path => ENV['WKHTMLTOPDF_BIN'] || '/usr/local/bin/wkhtmltopdf' }
|
4
4
|
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
|
5
5
|
|
6
6
|
# Provide a public accessor to the normally-private parse_options function
|
@@ -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 --toc-#{o.to_s.gsub('_', '-')} \"toc\"",
|
88
88
|
wp.get_parsed_options(:toc => {o => "toc"}).strip
|
89
89
|
end
|
90
90
|
|
@@ -92,12 +92,12 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
92
92
|
:l5_font_size, :l6_font_size, :l7_font_size, :l1_indentation, :l2_indentation,
|
93
93
|
:l3_indentation, :l4_indentation, :l5_indentation, :l6_indentation, :l7_indentation
|
94
94
|
].each do |o|
|
95
|
-
assert_equal "--toc-#{o.to_s.gsub('_', '-')} 5",
|
95
|
+
assert_equal "--toc --toc-#{o.to_s.gsub('_', '-')} 5",
|
96
96
|
wp.get_parsed_options(:toc => {o => 5}).strip
|
97
97
|
end
|
98
98
|
|
99
99
|
[:no_dots, :disable_links, :disable_back_links].each do |o|
|
100
|
-
assert_equal "--toc-#{o.to_s.gsub('_', '-')}",
|
100
|
+
assert_equal "--toc --toc-#{o.to_s.gsub('_', '-')}",
|
101
101
|
wp.get_parsed_options(:toc => {o => true}).strip
|
102
102
|
end
|
103
103
|
end
|
@@ -129,7 +129,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
129
129
|
[:cookie, :post].each do |o|
|
130
130
|
assert_equal "--#{o.to_s.gsub('_', '-')} name value", wp.get_parsed_options(o => "name value").strip
|
131
131
|
|
132
|
-
nv_formatter =
|
132
|
+
nv_formatter = Proc.new{|number| "--#{o.to_s.gsub('_', '-')} par#{number} val#{number}" }
|
133
133
|
assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", wp.get_parsed_options(o => ['par1 val1', 'par2 val2']).strip
|
134
134
|
end
|
135
135
|
|
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:
|
4
|
+
hash: 63
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 8
|
9
|
+
- 0
|
10
|
+
version: 0.8.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Miles Z. Sterret
|
@@ -15,9 +15,36 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2012-
|
19
|
-
dependencies:
|
20
|
-
|
18
|
+
date: 2012-11-14 00:00:00 Z
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
hash: 3
|
29
|
+
segments:
|
30
|
+
- 0
|
31
|
+
version: "0"
|
32
|
+
type: :runtime
|
33
|
+
version_requirements: *id001
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: rake
|
36
|
+
prerelease: false
|
37
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
hash: 3
|
43
|
+
segments:
|
44
|
+
- 0
|
45
|
+
version: "0"
|
46
|
+
type: :development
|
47
|
+
version_requirements: *id002
|
21
48
|
description: |
|
22
49
|
Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file to a user from HTML.
|
23
50
|
In other words, rather than dealing with a PDF generation DSL of some sort,
|
@@ -45,7 +72,7 @@ files:
|
|
45
72
|
- test/test_helper.rb
|
46
73
|
- test/wicked_pdf_helper_test.rb
|
47
74
|
- test/wicked_pdf_test.rb
|
48
|
-
homepage:
|
75
|
+
homepage: https://github.com/mileszs/wicked_pdf
|
49
76
|
licenses: []
|
50
77
|
|
51
78
|
post_install_message:
|