wicked_pdf 0.6.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.
- data/MIT-LICENSE +20 -0
- data/README.md +194 -0
- data/Rakefile +23 -0
- data/lib/pdf_helper.rb +58 -0
- data/lib/wicked_pdf.rb +145 -0
- data/lib/wicked_pdf_helper.rb +17 -0
- data/lib/wicked_pdf_railtie.rb +27 -0
- data/lib/wicked_pdf_tempfile.rb +10 -0
- data/test/pdf_helper_test.rb +26 -0
- data/test/test_helper.rb +4 -0
- data/test/wicked_pdf_helper_test.rb +23 -0
- data/test/wicked_pdf_test.rb +140 -0
- metadata +66 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 [name of plugin creator]
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,194 @@
|
|
1
|
+
# Wicked PDF
|
2
|
+
|
3
|
+
## A PDF generation plugin for Ruby on Rails
|
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.
|
6
|
+
|
7
|
+
_Wicked PDF has been verified to work on Ruby 1.8.7 and 1.9.2; Rails 2 and Rails 3_
|
8
|
+
|
9
|
+
### Installation
|
10
|
+
|
11
|
+
First, be sure to install [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/).
|
12
|
+
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
|
+
This plugin relies on streams to communicate with wkhtmltopdf.
|
14
|
+
|
15
|
+
More information about [wkhtmltopdf](http://code.google.com/p/wkhtmltopdf/) could be found [here](http://madalgo.au.dk/~jakobt/wkhtmltopdf-0.9.0_beta2-doc.html).
|
16
|
+
|
17
|
+
Next:
|
18
|
+
|
19
|
+
script/plugin install git://github.com/mileszs/wicked_pdf.git
|
20
|
+
script/generate wicked_pdf
|
21
|
+
### Basic Usage
|
22
|
+
|
23
|
+
class ThingsController < ApplicationController
|
24
|
+
def show
|
25
|
+
respond_to do |format|
|
26
|
+
format.html
|
27
|
+
format.pdf do
|
28
|
+
render :pdf => "file_name"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
### Advanced Usage with all available options
|
35
|
+
|
36
|
+
class ThingsController < ApplicationController
|
37
|
+
def show
|
38
|
+
respond_to do |format|
|
39
|
+
format.html
|
40
|
+
format.pdf do
|
41
|
+
render :pdf => 'file_name',
|
42
|
+
:template => 'things/show.pdf.erb',
|
43
|
+
:layout => 'pdf.html', # use 'pdf.html' for a pfd.html.erb file
|
44
|
+
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary
|
45
|
+
:show_as_html => params[:debug].present?, # allow debuging based on url param
|
46
|
+
:orientation => 'Landscape', # default Portrait
|
47
|
+
:page_size => 'A4, Letter, ...', # default A4
|
48
|
+
:proxy => 'TEXT',
|
49
|
+
:username => 'TEXT',
|
50
|
+
:password => 'TEXT',
|
51
|
+
:cover => 'URL',
|
52
|
+
:dpi => 'dpi',
|
53
|
+
:encoding => 'TEXT',
|
54
|
+
:user_style_sheet => 'URL',
|
55
|
+
:redirect_delay => NUMBER,
|
56
|
+
:zoom => FLOAT,
|
57
|
+
:page_offset => NUMBER,
|
58
|
+
:book => true,
|
59
|
+
:default_header => true,
|
60
|
+
:disable_javascript => false,
|
61
|
+
:greyscale => true,
|
62
|
+
:lowquality => true,
|
63
|
+
:enable_plugins => true,
|
64
|
+
:disable_internal_links => true,
|
65
|
+
:disable_external_links => true,
|
66
|
+
:print_media_type => true,
|
67
|
+
:disable_smart_shrinking => true,
|
68
|
+
:use_xserver => true,
|
69
|
+
:no_background => true,
|
70
|
+
:margin => {:top => SIZE, # default 10 (mm)
|
71
|
+
:bottom => SIZE,
|
72
|
+
:left => SIZE,
|
73
|
+
:right => SIZE},
|
74
|
+
:header => {:html => {:template => 'users/header.pdf.erb' OR :url => 'www.header.bbb'},
|
75
|
+
:center => 'TEXT',
|
76
|
+
:font_name => 'NAME',
|
77
|
+
:font_size => SIZE,
|
78
|
+
:left => 'TEXT',
|
79
|
+
:right => 'TEXT',
|
80
|
+
:spacing => REAL,
|
81
|
+
:line => true},
|
82
|
+
:footer => {:html => {:template => 'public/header.pdf.erb' OR :url => 'www.header.bbb'},
|
83
|
+
:center => 'TEXT',
|
84
|
+
:font_name => 'NAME',
|
85
|
+
:font_size => SIZE,
|
86
|
+
:left => 'TEXT',
|
87
|
+
:right => 'TEXT',
|
88
|
+
:spacing => REAL,
|
89
|
+
:line => true},
|
90
|
+
:toc => {:font_name => "NAME",
|
91
|
+
:depth => LEVEL,
|
92
|
+
:header_text => "TEXT",
|
93
|
+
:header_fs => SIZE,
|
94
|
+
:l1_font_size => SIZE,
|
95
|
+
:l2_font_size => SIZE,
|
96
|
+
:l3_font_size => SIZE,
|
97
|
+
:l4_font_size => SIZE,
|
98
|
+
:l5_font_size => SIZE,
|
99
|
+
:l6_font_size => SIZE,
|
100
|
+
:l7_font_size => SIZE,
|
101
|
+
:l1_indentation => NUM,
|
102
|
+
:l2_indentation => NUM,
|
103
|
+
:l3_indentation => NUM,
|
104
|
+
:l4_indentation => NUM,
|
105
|
+
:l5_indentation => NUM,
|
106
|
+
:l6_indentation => NUM,
|
107
|
+
:l7_indentation => NUM,
|
108
|
+
:no_dots => true,
|
109
|
+
:disable_links => true,
|
110
|
+
:disable_back_links => true},
|
111
|
+
:outline => {:outline => true,
|
112
|
+
:outline_depth => LEVEL}
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
By default, it will render without a layout (:layout => false) and the template for the current controller and action.
|
119
|
+
|
120
|
+
### Styles
|
121
|
+
|
122
|
+
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.
|
123
|
+
|
124
|
+
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
125
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
126
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
127
|
+
<head>
|
128
|
+
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
129
|
+
<%= wicked_pdf_stylesheet_link_tag "pdf" -%>
|
130
|
+
<%= wicked_pdf_javascript_include_tag "number_pages" %>
|
131
|
+
</head>
|
132
|
+
<body onload='number_pages'>
|
133
|
+
<div id="header">
|
134
|
+
<%= wicked_pdf_image_tag 'mysite.jpg' %>
|
135
|
+
</div>
|
136
|
+
<div id="content">
|
137
|
+
<%= yield %>
|
138
|
+
</div>
|
139
|
+
</body>
|
140
|
+
</html>
|
141
|
+
|
142
|
+
### Page Numbering
|
143
|
+
|
144
|
+
A bit of javascript can help you number your pages, create a template or header/footer file with this:
|
145
|
+
|
146
|
+
<html>
|
147
|
+
<head>
|
148
|
+
<script>
|
149
|
+
function number_pages() {
|
150
|
+
var vars={};
|
151
|
+
var x=document.location.search.substring(1).split('&');
|
152
|
+
for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = unescape(z[1]);}
|
153
|
+
var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
|
154
|
+
for(var i in x) {
|
155
|
+
var y = document.getElementsByClassName(x[i]);
|
156
|
+
for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
|
157
|
+
}
|
158
|
+
}
|
159
|
+
</script>
|
160
|
+
</head>
|
161
|
+
<body onload="number_pages()">
|
162
|
+
Page <span class="page"></span> of <span class="topage"></span>
|
163
|
+
</body>
|
164
|
+
</html>
|
165
|
+
|
166
|
+
Anything with a class listed in "var x" above will be auto-filled at render time.
|
167
|
+
|
168
|
+
### Configuration
|
169
|
+
|
170
|
+
You can put your default configuration, applied to all pdf's at "wicked_pdf.rb" initializer.
|
171
|
+
|
172
|
+
### Further Reading
|
173
|
+
|
174
|
+
Andreas Happe's post [Generating PDFs from Ruby on Rails](http://snikt.net/index.php/2010/03/03/generating-pdfs-from-ruby-on-rails)
|
175
|
+
|
176
|
+
### Debugging
|
177
|
+
|
178
|
+
Now you can use a debug param on the URL that shows you the content of the pdf in plain html to design it faster.
|
179
|
+
|
180
|
+
First of all you must configure the render parameter ":show_as_html => params[:debug]" and then just use it like normally but adding "debug=1" as a param:
|
181
|
+
|
182
|
+
http://localhost:3001/CONTROLLER/X.pdf?debug=1
|
183
|
+
|
184
|
+
However, the wicked_pdf_* helpers will use file:// paths for assets when using :show_as_html, and your browser's cross-domain safety feature will kick in, and not render them. To get around this, you can load your assets like so in your templates:
|
185
|
+
|
186
|
+
<%= params[:debug].present? ? image_tag('foo') : wicked_pdf_image_tag('foo') %>
|
187
|
+
|
188
|
+
### Inspiration
|
189
|
+
|
190
|
+
You may have noticed: this plugin is heavily inspired by the PrinceXML plugin [princely](http://github.com/mbleigh/princely/tree/master). PrinceXML's cost was prohibitive for me. So, with a little help from some friends (thanks [jqr](http://github.com/jqr)), I tracked down wkhtmltopdf, and here we are.
|
191
|
+
|
192
|
+
### Awesome Peoples
|
193
|
+
|
194
|
+
Also, thanks to [galdomedia](http://github.com/galdomedia) and [jcrisp](http://github.com/jcrisp) and [lleirborras](http://github.com/lleirborras), [tiennou](http://github.com/tiennou), and everyone else for all their hard work and patience with my delays in merging in their enhancements.
|
data/Rakefile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
|
5
|
+
desc 'Default: run unit tests.'
|
6
|
+
task :default => :test
|
7
|
+
|
8
|
+
desc 'Test the wicked_pdf plugin.'
|
9
|
+
Rake::TestTask.new(:test) do |t|
|
10
|
+
t.libs << 'lib'
|
11
|
+
t.libs << 'test'
|
12
|
+
t.pattern = 'test/**/*_test.rb'
|
13
|
+
t.verbose = true
|
14
|
+
end
|
15
|
+
|
16
|
+
desc 'Generate documentation for the wicked_pdf plugin.'
|
17
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
18
|
+
rdoc.rdoc_dir = 'rdoc'
|
19
|
+
rdoc.title = 'WickedPdf'
|
20
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
21
|
+
rdoc.rdoc_files.include('README')
|
22
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
23
|
+
end
|
data/lib/pdf_helper.rb
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
module PdfHelper
|
2
|
+
require 'wicked_pdf'
|
3
|
+
|
4
|
+
def self.included(base)
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :render, :wicked_pdf
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def render_with_wicked_pdf(options = nil, *args, &block)
|
11
|
+
if options.is_a?(Hash) && options.has_key?(:pdf)
|
12
|
+
logger.info '*'*15 + 'WICKED' + '*'*15
|
13
|
+
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
|
14
|
+
else
|
15
|
+
render_without_wicked_pdf(options, *args, &block)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
def make_pdf(options = {})
|
21
|
+
html_string = render_to_string(:template => options[:template], :layout => options[:layout])
|
22
|
+
w = WickedPdf.new(options[:wkhtmltopdf])
|
23
|
+
w.pdf_from_string(html_string, options)
|
24
|
+
end
|
25
|
+
|
26
|
+
def make_and_send_pdf(pdf_name, options = {})
|
27
|
+
options[:wkhtmltopdf] ||= nil
|
28
|
+
options[:layout] ||= false
|
29
|
+
options[:template] ||= File.join(controller_path, action_name)
|
30
|
+
options[:disposition] ||= "inline"
|
31
|
+
|
32
|
+
options = prerender_header_and_footer(options)
|
33
|
+
if options[:show_as_html]
|
34
|
+
render :template => options[:template], :layout => options[:layout], :content_type => "text/html"
|
35
|
+
else
|
36
|
+
pdf_content = make_pdf(options)
|
37
|
+
File.open(options[:save_to_file], 'wb') {|file| file << pdf_content } if options[:save_to_file]
|
38
|
+
send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Given an options hash, prerenders content for the header and footer sections
|
43
|
+
# to temp files and return a new options hash including the URLs to these files.
|
44
|
+
def prerender_header_and_footer(options)
|
45
|
+
[:header, :footer].each do |hf|
|
46
|
+
if options[hf] && options[hf][:html] && options[hf][:html][:template]
|
47
|
+
WickedPdfTempfile.open("wicked_pdf.html") do |f|
|
48
|
+
f << render_to_string(:template => options[hf][:html][:template],
|
49
|
+
:layout => options[:layout])
|
50
|
+
options[hf][:html].delete(:template)
|
51
|
+
options[hf][:html][:url] = "file://#{f.path}"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
return options
|
57
|
+
end
|
58
|
+
end
|
data/lib/wicked_pdf.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
# wkhtml2pdf Ruby interface
|
2
|
+
# http://code.google.com/p/wkhtmltopdf/
|
3
|
+
|
4
|
+
require 'logger'
|
5
|
+
require 'digest/md5'
|
6
|
+
require 'open3'
|
7
|
+
require 'active_support/core_ext/class/attribute_accessors'
|
8
|
+
|
9
|
+
require 'wicked_pdf_railtie'
|
10
|
+
|
11
|
+
class WickedPdf
|
12
|
+
@@config = {}
|
13
|
+
cattr_accessor :config
|
14
|
+
|
15
|
+
def initialize(wkhtmltopdf_binary_path = nil)
|
16
|
+
@exe_path = wkhtmltopdf_binary_path
|
17
|
+
@exe_path ||= WickedPdf.config[:exe_path] unless WickedPdf.config.empty?
|
18
|
+
@exe_path ||= `which wkhtmltopdf`.chomp
|
19
|
+
raise "Location of wkhtmltopdf unknown" if @exe_path.empty?
|
20
|
+
raise "Bad wkhtmltopdf's path" unless File.exists?(@exe_path)
|
21
|
+
raise "Wkhtmltopdf is not executable" unless File.executable?(@exe_path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def pdf_from_string(string, options={})
|
25
|
+
command_for_stdin_stdout = "#{@exe_path} #{parse_options(options)} -q - - " # -q for no errors on stdout
|
26
|
+
p "*"*15 + command_for_stdin_stdout + "*"*15 unless defined?(Rails) and Rails.env != 'development'
|
27
|
+
pdf, err = begin
|
28
|
+
Open3.popen3(command_for_stdin_stdout) do |stdin, stdout, stderr|
|
29
|
+
stdin.write(string)
|
30
|
+
stdin.close
|
31
|
+
[stdout.read, stderr.read]
|
32
|
+
end
|
33
|
+
rescue Exception => e
|
34
|
+
raise "Failed to execute #{@exe_path}: #{e}"
|
35
|
+
end
|
36
|
+
raise "PDF could not be generated!\n#{err}" if pdf and pdf.length == 0
|
37
|
+
pdf
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def parse_options(options)
|
42
|
+
[
|
43
|
+
parse_header_footer(:header => options.delete(:header),
|
44
|
+
:footer => options.delete(:footer),
|
45
|
+
:layout => options[:layout]),
|
46
|
+
parse_toc(options.delete(:toc)),
|
47
|
+
parse_outline(options.delete(:outline)),
|
48
|
+
parse_margins(options.delete(:margin)),
|
49
|
+
parse_others(options),
|
50
|
+
].join(' ')
|
51
|
+
end
|
52
|
+
|
53
|
+
def make_option(name, value, type=:string)
|
54
|
+
"--#{name.gsub('_', '-')} " + case type
|
55
|
+
when :boolean then ""
|
56
|
+
when :numeric then value.to_s
|
57
|
+
else "'#{value}'"
|
58
|
+
end + " "
|
59
|
+
end
|
60
|
+
|
61
|
+
def make_options(options, names, prefix="", type=:string)
|
62
|
+
names.collect {|o| make_option("#{prefix.blank? ? "" : prefix + "-"}#{o.to_s}", options[o], type) unless options[o].blank?}.join
|
63
|
+
end
|
64
|
+
|
65
|
+
def parse_header_footer(options)
|
66
|
+
r=""
|
67
|
+
[:header, :footer].collect do |hf|
|
68
|
+
unless options[hf].blank?
|
69
|
+
opt_hf = options[hf]
|
70
|
+
r += make_options(opt_hf, [:center, :font_name, :left, :right], "#{hf.to_s}")
|
71
|
+
r += make_options(opt_hf, [:font_size, :spacing], "#{hf.to_s}", :numeric)
|
72
|
+
r += make_options(opt_hf, [:line], "#{hf.to_s}", :boolean)
|
73
|
+
unless opt_hf[:html].blank?
|
74
|
+
r += make_option("#{hf.to_s}-html", opt_hf[:html][:url]) unless opt_hf[:html][:url].blank?
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end unless options.blank?
|
78
|
+
r
|
79
|
+
end
|
80
|
+
|
81
|
+
def parse_toc(options)
|
82
|
+
unless options.blank?
|
83
|
+
r = make_options(options, [ :font_name, :header_text], "toc")
|
84
|
+
r +=make_options(options, [ :depth,
|
85
|
+
:header_fs,
|
86
|
+
:l1_font_size,
|
87
|
+
:l2_font_size,
|
88
|
+
:l3_font_size,
|
89
|
+
:l4_font_size,
|
90
|
+
:l5_font_size,
|
91
|
+
:l6_font_size,
|
92
|
+
:l7_font_size,
|
93
|
+
:l1_indentation,
|
94
|
+
:l2_indentation,
|
95
|
+
:l3_indentation,
|
96
|
+
:l4_indentation,
|
97
|
+
:l5_indentation,
|
98
|
+
:l6_indentation,
|
99
|
+
:l7_indentation], "toc", :numeric)
|
100
|
+
r +=make_options(options, [ :no_dots,
|
101
|
+
:disable_links,
|
102
|
+
:disable_back_links], "toc", :boolean)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def parse_outline(options)
|
107
|
+
unless options.blank?
|
108
|
+
r = make_options(options, [:outline], "", :boolean)
|
109
|
+
r +=make_options(options, [:outline_depth], "", :numeric)
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def parse_margins(options)
|
114
|
+
make_options(options, [:top, :bottom, :left, :right], "margin", :numeric) unless options.blank?
|
115
|
+
end
|
116
|
+
|
117
|
+
def parse_others(options)
|
118
|
+
unless options.blank?
|
119
|
+
r = make_options(options, [ :orientation,
|
120
|
+
:page_size,
|
121
|
+
:proxy,
|
122
|
+
:username,
|
123
|
+
:password,
|
124
|
+
:cover,
|
125
|
+
:dpi,
|
126
|
+
:encoding,
|
127
|
+
:user_style_sheet])
|
128
|
+
r +=make_options(options, [ :redirect_delay,
|
129
|
+
:zoom,
|
130
|
+
:page_offset], "", :numeric)
|
131
|
+
r +=make_options(options, [ :book,
|
132
|
+
:default_header,
|
133
|
+
:disable_javascript,
|
134
|
+
:greyscale,
|
135
|
+
:lowquality,
|
136
|
+
:enable_plugins,
|
137
|
+
:disable_internal_links,
|
138
|
+
:disable_external_links,
|
139
|
+
:print_media_type,
|
140
|
+
:disable_smart_shrinking,
|
141
|
+
:use_xserver,
|
142
|
+
:no_background], "", :boolean)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module WickedPdfHelper
|
2
|
+
def wicked_pdf_stylesheet_link_tag(style)
|
3
|
+
stylesheet_link_tag style, "file://#{Rails.root.join('public','stylesheets',style)}"
|
4
|
+
end
|
5
|
+
|
6
|
+
def wicked_pdf_image_tag(img, options={})
|
7
|
+
image_tag "file://#{Rails.root.join('public', 'images', img)}", options
|
8
|
+
end
|
9
|
+
|
10
|
+
def wicked_pdf_javascript_src_tag(jsfile, options={})
|
11
|
+
javascript_src_tag "file://#{Rails.root.join('public','javascripts',jsfile)}", options
|
12
|
+
end
|
13
|
+
|
14
|
+
def wicked_pdf_javascript_include_tag(*sources)
|
15
|
+
sources.collect{ |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n").html_safe
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'pdf_helper'
|
2
|
+
require 'wicked_pdf_helper'
|
3
|
+
|
4
|
+
if defined?(Rails)
|
5
|
+
if Rails::VERSION::MAJOR == 2
|
6
|
+
unless ActionController::Base.instance_methods.include? "render_with_wicked_pdf"
|
7
|
+
ActionController::Base.send :include, PdfHelper
|
8
|
+
end
|
9
|
+
|
10
|
+
unless ActionView::Base.instance_methods.include? "wicked_pdf_stylesheet_link_tag"
|
11
|
+
ActionView::Base.send :include, WickedPdfHelper
|
12
|
+
end
|
13
|
+
|
14
|
+
Mime::Type.register 'application/pdf', :pdf
|
15
|
+
|
16
|
+
else
|
17
|
+
class WickedRailtie < Rails::Railtie
|
18
|
+
|
19
|
+
initializer "wicked_pdf.register" do |app|
|
20
|
+
ActionController::Base.send :include, PdfHelper
|
21
|
+
ActionView::Base.send :include, WickedPdfHelper
|
22
|
+
|
23
|
+
Mime::Type.register 'application/pdf', :pdf
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'tempfile'
|
2
|
+
|
3
|
+
class WickedPdfTempfile < Tempfile
|
4
|
+
# Replaces Tempfile's +make_tmpname+ with one that honors file extensions.
|
5
|
+
def make_tmpname(basename, n)
|
6
|
+
extension = File.extname(basename)
|
7
|
+
sprintf("%s_%d_%d%s", File.basename(basename, extension), $$, n, extension)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module ActionController
|
4
|
+
class Base
|
5
|
+
def render_to_string opts={}
|
6
|
+
opts.to_s
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
class PdfHelperTest < ActionController::TestCase
|
12
|
+
def setup
|
13
|
+
@ac = ActionController::Base.new
|
14
|
+
end
|
15
|
+
|
16
|
+
def teardown
|
17
|
+
@ac=nil
|
18
|
+
end
|
19
|
+
|
20
|
+
test "should prerender header and footer :template options" do
|
21
|
+
options = @ac.send( :prerender_header_and_footer,
|
22
|
+
:header => {:html => { :template => 'hf.html.erb'}});
|
23
|
+
assert !options[:header][:html].has_key?(:template)
|
24
|
+
assert_match /^file:\/\/.*wicked_pdf.*\.html/, options[:header][:html][:url]
|
25
|
+
end
|
26
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class WickedPdfHelperTest < ActionView::TestCase
|
4
|
+
test 'wicked_pdf_stylesheet_link_tag should return the same as stylesheet_link_tag when passed a full path' do
|
5
|
+
assert_equal stylesheet_link_tag('pdf', "file://#{Rails.root.join('public','stylesheets','pdf')}"),
|
6
|
+
wicked_pdf_stylesheet_link_tag('pdf')
|
7
|
+
end
|
8
|
+
|
9
|
+
test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
|
10
|
+
assert_equal image_tag("file://#{Rails.root.join('public','images','pdf')}"),
|
11
|
+
wicked_pdf_image_tag('pdf')
|
12
|
+
end
|
13
|
+
|
14
|
+
test 'wicked_pdf_javascript_src_tag should return the same as javascript_src_tag when passed a full path' do
|
15
|
+
assert_equal javascript_src_tag("file://#{Rails.root.join('public','javascripts','pdf')}", {}),
|
16
|
+
wicked_pdf_javascript_src_tag('pdf')
|
17
|
+
end
|
18
|
+
|
19
|
+
test 'wicked_pdf_include_tag should return many wicked_pdf_javascript_src_tags' do
|
20
|
+
assert_equal [wicked_pdf_javascript_src_tag('foo'), wicked_pdf_javascript_src_tag('bar')].join("\n"),
|
21
|
+
wicked_pdf_javascript_include_tag('foo', 'bar')
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,140 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
WickedPdf.config = { :exe_path => '/usr/local/bin/wkhtmltopdf' }
|
4
|
+
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
|
5
|
+
|
6
|
+
# Provide a public accessor to the normally-private parse_options function
|
7
|
+
class WickedPdf
|
8
|
+
def get_parsed_options(opts)
|
9
|
+
parse_options(opts)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
class WickedPdfTest < ActiveSupport::TestCase
|
14
|
+
|
15
|
+
test "should generate PDF from html document" do
|
16
|
+
wp = WickedPdf.new
|
17
|
+
pdf = wp.pdf_from_string HTML_DOCUMENT
|
18
|
+
assert pdf.start_with?("%PDF-1.4")
|
19
|
+
assert pdf.rstrip.end_with?("%%EOF")
|
20
|
+
assert pdf.length > 100
|
21
|
+
end
|
22
|
+
|
23
|
+
test "should raise exception when no path to wkhtmltopdf" do
|
24
|
+
assert_raise RuntimeError do
|
25
|
+
WickedPdf.new " "
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
test "should raise exception when wkhtmltopdf path is wrong" do
|
30
|
+
assert_raise RuntimeError do
|
31
|
+
WickedPdf.new "/i/do/not/exist/notwkhtmltopdf"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
test "should raise exception when wkhtmltopdf is not executable" do
|
36
|
+
begin
|
37
|
+
tmp = Tempfile.new('wkhtmltopdf')
|
38
|
+
fp = tmp.path
|
39
|
+
File.chmod 0000, fp
|
40
|
+
assert_raise RuntimeError do
|
41
|
+
WickedPdf.new fp
|
42
|
+
end
|
43
|
+
ensure
|
44
|
+
tmp.delete
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
test "should raise exception when pdf generation fails" do
|
49
|
+
begin
|
50
|
+
tmp = Tempfile.new('wkhtmltopdf')
|
51
|
+
fp = tmp.path
|
52
|
+
File.chmod 0777, fp
|
53
|
+
wp = WickedPdf.new fp
|
54
|
+
assert_raise RuntimeError do
|
55
|
+
wp.pdf_from_string HTML_DOCUMENT
|
56
|
+
end
|
57
|
+
ensure
|
58
|
+
tmp.delete
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
test "should parse header and footer options" do
|
63
|
+
wp = WickedPdf.new
|
64
|
+
|
65
|
+
[:header, :footer].each do |hf|
|
66
|
+
[:center, :font_name, :left, :right].each do |o|
|
67
|
+
assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} 'header_footer'",
|
68
|
+
wp.get_parsed_options(hf => {o => "header_footer"}).strip
|
69
|
+
end
|
70
|
+
|
71
|
+
[:font_size, :spacing].each do |o|
|
72
|
+
assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} 12",
|
73
|
+
wp.get_parsed_options(hf => {o => "12"}).strip
|
74
|
+
end
|
75
|
+
|
76
|
+
assert_equal "--#{hf.to_s}-line",
|
77
|
+
wp.get_parsed_options(hf => {:line => true}).strip
|
78
|
+
assert_equal "--#{hf.to_s}-html 'http://www.abc.com'",
|
79
|
+
wp.get_parsed_options(hf => {:html => {:url => 'http://www.abc.com'}}).strip
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
test "should parse toc options" do
|
84
|
+
wp = WickedPdf.new
|
85
|
+
|
86
|
+
[:font_name, :header_text].each do |o|
|
87
|
+
assert_equal "--toc-#{o.to_s.gsub('_', '-')} 'toc'",
|
88
|
+
wp.get_parsed_options(:toc => {o => "toc"}).strip
|
89
|
+
end
|
90
|
+
|
91
|
+
[ :depth, :header_fs, :l1_font_size, :l2_font_size, :l3_font_size, :l4_font_size,
|
92
|
+
:l5_font_size, :l6_font_size, :l7_font_size, :l1_indentation, :l2_indentation,
|
93
|
+
:l3_indentation, :l4_indentation, :l5_indentation, :l6_indentation, :l7_indentation
|
94
|
+
].each do |o|
|
95
|
+
assert_equal "--toc-#{o.to_s.gsub('_', '-')} 5",
|
96
|
+
wp.get_parsed_options(:toc => {o => 5}).strip
|
97
|
+
end
|
98
|
+
|
99
|
+
[:no_dots, :disable_links, :disable_back_links].each do |o|
|
100
|
+
assert_equal "--toc-#{o.to_s.gsub('_', '-')}",
|
101
|
+
wp.get_parsed_options(:toc => {o => true}).strip
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
test "should parse outline options" do
|
106
|
+
wp = WickedPdf.new
|
107
|
+
|
108
|
+
assert_equal "--outline", wp.get_parsed_options(:outline => {:outline => true}).strip
|
109
|
+
assert_equal "--outline-depth 5", wp.get_parsed_options(:outline => {:outline_depth => 5}).strip
|
110
|
+
end
|
111
|
+
|
112
|
+
test "should parse margins options" do
|
113
|
+
wp = WickedPdf.new
|
114
|
+
|
115
|
+
[:top, :bottom, :left, :right].each do |o|
|
116
|
+
assert_equal "--margin-#{o.to_s} 12", wp.get_parsed_options(:margin => {o => "12"}).strip
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
test "should parse other options" do
|
121
|
+
wp = WickedPdf.new
|
122
|
+
|
123
|
+
[ :orientation, :page_size, :proxy, :username, :password, :cover, :dpi,
|
124
|
+
:encoding, :user_style_sheet
|
125
|
+
].each do |o|
|
126
|
+
assert_equal "--#{o.to_s.gsub('_', '-')} 'opts'", wp.get_parsed_options(o => "opts").strip
|
127
|
+
end
|
128
|
+
|
129
|
+
[:redirect_delay, :zoom, :page_offset].each do |o|
|
130
|
+
assert_equal "--#{o.to_s.gsub('_', '-')} 5", wp.get_parsed_options(o => 5).strip
|
131
|
+
end
|
132
|
+
|
133
|
+
[ :book, :default_header, :disable_javascript, :greyscale, :lowquality,
|
134
|
+
:enable_plugins, :disable_internal_links, :disable_external_links,
|
135
|
+
:print_media_type, :disable_smart_shrinking, :use_xserver, :no_background
|
136
|
+
].each do |o|
|
137
|
+
assert_equal "--#{o.to_s.gsub('_', '-')}", wp.get_parsed_options(o => true).strip
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: wicked_pdf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.6.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Miles Z. Sterret
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-02-25 00:00:00.000000000 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
description: ! 'Wicked PDF uses the shell utility wkhtmltopdf to serve a PDF file
|
16
|
+
to a user from HTML.
|
17
|
+
|
18
|
+
In other words, rather than dealing with a PDF generation DSL of some sort,
|
19
|
+
|
20
|
+
you simply write an HTML view as you would normally, and let Wicked take care of
|
21
|
+
the hard stuff.
|
22
|
+
|
23
|
+
'
|
24
|
+
email: miles.sterrett@gmail.com
|
25
|
+
executables: []
|
26
|
+
extensions: []
|
27
|
+
extra_rdoc_files: []
|
28
|
+
files:
|
29
|
+
- README.md
|
30
|
+
- Rakefile
|
31
|
+
- MIT-LICENSE
|
32
|
+
- lib/pdf_helper.rb
|
33
|
+
- lib/wicked_pdf.rb
|
34
|
+
- lib/wicked_pdf_helper.rb
|
35
|
+
- lib/wicked_pdf_railtie.rb
|
36
|
+
- lib/wicked_pdf_tempfile.rb
|
37
|
+
- test/pdf_helper_test.rb
|
38
|
+
- test/test_helper.rb
|
39
|
+
- test/wicked_pdf_helper_test.rb
|
40
|
+
- test/wicked_pdf_test.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://github.com/mileszs/wicked_pdf
|
43
|
+
licenses: []
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
require_paths:
|
47
|
+
- lib
|
48
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ! '>='
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.5.2
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: PDF generator (from HTML) plugin for Ruby on Rails
|
66
|
+
test_files: []
|