wisepdf 1.1.3 → 1.2.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/VERSION +1 -1
- data/lib/wisepdf/configuration.rb +10 -9
- data/lib/wisepdf/parser.rb +61 -0
- data/lib/wisepdf/render.rb +30 -26
- data/lib/wisepdf/writer.rb +25 -80
- data/lib/wisepdf.rb +11 -7
- data/test/application_controller_test.rb +5 -0
- data/test/configuration_test.rb +52 -0
- data/test/dummy/app/controllers/application_controller.rb +5 -1
- data/test/dummy/app/views/application/index.pdf.erb +1 -0
- data/test/dummy/app/views/layouts/pdf.html.erb +14 -0
- data/test/dummy/config/database.yml +5 -1
- data/test/dummy/config/routes.rb +1 -0
- data/test/parser_test.rb +66 -0
- data/test/writer_test.rb +3 -145
- data/wisepdf.gemspec +7 -3
- metadata +20 -16
- data/lib/wisepdf/tempfile.rb +0 -11
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.2.0
|
@@ -3,17 +3,18 @@ require 'singleton'
|
|
3
3
|
module Wisepdf
|
4
4
|
class Configuration
|
5
5
|
include Singleton
|
6
|
-
cattr_accessor :options
|
7
|
-
cattr_accessor :wkhtmltopdf
|
8
6
|
|
9
7
|
class << self
|
8
|
+
attr_accessor :options
|
9
|
+
attr_accessor :wkhtmltopdf
|
10
|
+
|
10
11
|
def wkhtmltopdf
|
11
|
-
return
|
12
|
+
return @wkhtmltopdf if @wkhtmltopdf.present?
|
12
13
|
|
13
|
-
if
|
14
|
-
|
14
|
+
if @wkhtmltopdf.nil? && !self.windows?
|
15
|
+
@wkhtmltopdf = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
15
16
|
end
|
16
|
-
return
|
17
|
+
return @wkhtmltopdf
|
17
18
|
end
|
18
19
|
|
19
20
|
def configure
|
@@ -21,11 +22,11 @@ module Wisepdf
|
|
21
22
|
end
|
22
23
|
|
23
24
|
def reset!
|
24
|
-
|
25
|
-
:
|
25
|
+
@options = {
|
26
|
+
:encoding => "UTF-8",
|
26
27
|
:use_xserver => false
|
27
28
|
}
|
28
|
-
|
29
|
+
@wkhtmltopdf = nil
|
29
30
|
end
|
30
31
|
|
31
32
|
def development?
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Wisepdf
|
2
|
+
class Parser
|
3
|
+
include Singleton
|
4
|
+
|
5
|
+
ESCAPED_OPTIONS = [
|
6
|
+
:pdf, :layout, :template, :action, :partial,
|
7
|
+
:object, :collection, :as, :spacer_template,
|
8
|
+
:disposition, :locals, :status, :file, :text,
|
9
|
+
:xml, :json, :callback, :inline, :location
|
10
|
+
]
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def parse(options)
|
14
|
+
options = self.escape(options)
|
15
|
+
options = self.flatten(options)
|
16
|
+
parsed_options = {}
|
17
|
+
|
18
|
+
options.each do |key, value|
|
19
|
+
unless( value == false || value.nil? )
|
20
|
+
normalized_key = "--#{self.normalize_arg(key)}"
|
21
|
+
parsed_options[normalized_key] = self.normalize_value(value)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
parsed_options
|
25
|
+
end
|
26
|
+
|
27
|
+
protected
|
28
|
+
def escape(options)
|
29
|
+
options.delete_if{ |k,v| ESCAPED_OPTIONS.include?(k.to_sym) }
|
30
|
+
end
|
31
|
+
|
32
|
+
def flatten(options, prefix = nil)
|
33
|
+
hash = {}
|
34
|
+
options.each do |k,v|
|
35
|
+
key = prefix.nil? ? k : "#{prefix.to_s}-#{k}"
|
36
|
+
|
37
|
+
if v.is_a?(Hash)
|
38
|
+
hash.delete(k)
|
39
|
+
hash.merge!(self.flatten(v, key))
|
40
|
+
else
|
41
|
+
hash[key.to_s] = v
|
42
|
+
end
|
43
|
+
end
|
44
|
+
return hash
|
45
|
+
end
|
46
|
+
|
47
|
+
def normalize_arg(arg)
|
48
|
+
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
|
49
|
+
end
|
50
|
+
|
51
|
+
def normalize_value(value)
|
52
|
+
case value
|
53
|
+
when TrueClass
|
54
|
+
nil
|
55
|
+
else
|
56
|
+
value.to_s
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/wisepdf/render.rb
CHANGED
@@ -10,8 +10,11 @@ module Wisepdf
|
|
10
10
|
|
11
11
|
def render(options = nil, *args, &block)
|
12
12
|
if options.is_a?(Hash) && options.has_key?(:pdf)
|
13
|
-
|
14
|
-
|
13
|
+
options = self.default_pdf_render_options.merge(options)
|
14
|
+
super(options.merge(:content_type => "text/html"), *args, &block) and return if options[:show_as_html]
|
15
|
+
|
16
|
+
self.log_pdf_creation
|
17
|
+
self.make_and_send_pdf(options)
|
15
18
|
else
|
16
19
|
super
|
17
20
|
end
|
@@ -19,9 +22,8 @@ module Wisepdf
|
|
19
22
|
|
20
23
|
def render_to_string(options = nil, *args, &block)
|
21
24
|
if options.is_a?(Hash) && options.has_key?(:pdf)
|
22
|
-
log_pdf_creation
|
23
|
-
|
24
|
-
make_pdf(Wisepdf::Configuration.options.merge(options))
|
25
|
+
self.log_pdf_creation
|
26
|
+
self.make_pdf(self.default_pdf_render_options.merge(options))
|
25
27
|
else
|
26
28
|
super
|
27
29
|
end
|
@@ -29,7 +31,7 @@ module Wisepdf
|
|
29
31
|
|
30
32
|
protected
|
31
33
|
def log_pdf_creation
|
32
|
-
logger.info '*'*15 + '
|
34
|
+
logger.info '*'*15 + 'WISEPDF' + '*'*15
|
33
35
|
end
|
34
36
|
|
35
37
|
def clean_temp_files
|
@@ -37,28 +39,30 @@ module Wisepdf
|
|
37
39
|
@hf_tempfiles.each { |tf| tf.close! }
|
38
40
|
end
|
39
41
|
end
|
40
|
-
|
42
|
+
|
43
|
+
def default_pdf_render_options
|
44
|
+
Wisepdf::Configuration.options.merge({
|
45
|
+
:wkhtmltopdf => nil,
|
46
|
+
:layout => false,
|
47
|
+
:template => File.join(controller_path, action_name),
|
48
|
+
:disposition => "inline"
|
49
|
+
})
|
50
|
+
end
|
51
|
+
|
41
52
|
def make_pdf(options = {})
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
w.to_pdf(html_string, options)
|
53
|
+
options = self.prerender_header_and_footer(options)
|
54
|
+
html = render_to_string(:template => options[:template], :layout => options[:layout])
|
55
|
+
Wisepdf::Writer.new(options[:wkhtmltopdf], options.dup).to_pdf(html)
|
46
56
|
end
|
47
57
|
|
48
|
-
def make_and_send_pdf(
|
49
|
-
|
50
|
-
options[:
|
51
|
-
options[:template] ||= File.join(controller_path, action_name)
|
52
|
-
options[:disposition] ||= "inline"
|
53
|
-
if options[:show_as_html]
|
54
|
-
render :template => options[:template], :layout => options[:layout], :content_type => "text/html"
|
55
|
-
else
|
56
|
-
pdf_content = make_pdf(options)
|
57
|
-
File.open(options.delete(:save_to_file), 'wb') {|file| file << pdf_content } if options[:save_to_file].present?
|
58
|
+
def make_and_send_pdf(options = {})
|
59
|
+
pdf = self.make_pdf(options)
|
60
|
+
File.open(options[:save_to_file], 'wb') {|file| file << pdf } if options[:save_to_file].present?
|
58
61
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
+
filename = options.delete(:pdf)
|
63
|
+
filename += '.pdf' unless filename =~ /.pdf\z|.PDF\Z/
|
64
|
+
|
65
|
+
send_data(pdf, options.merge(:filename => filename, :type => 'application/pdf')) unless options[:save_only]
|
62
66
|
end
|
63
67
|
|
64
68
|
def prerender_header_and_footer(arguments)
|
@@ -67,13 +71,13 @@ module Wisepdf
|
|
67
71
|
opts = arguments[hf].delete(:html)
|
68
72
|
|
69
73
|
@hf_tempfiles = [] if ! defined?(@hf_tempfiles)
|
70
|
-
@hf_tempfiles.push( tf =
|
74
|
+
@hf_tempfiles.push( tf = Tempfile.new("wisepdf_#{hf}_pdf", '.html') )
|
71
75
|
opts[:layout] ||= arguments[:layout]
|
72
76
|
|
73
77
|
tf.write render_to_string(:template => opts[:template], :layout => opts[:layout], :locals => opts[:locals])
|
74
78
|
tf.flush
|
75
79
|
|
76
|
-
|
80
|
+
arguments[hf][:html] = "file://#{tf.path}"
|
77
81
|
end
|
78
82
|
end
|
79
83
|
arguments
|
data/lib/wisepdf/writer.rb
CHANGED
@@ -1,43 +1,21 @@
|
|
1
1
|
require 'open3'
|
2
2
|
|
3
3
|
module Wisepdf
|
4
|
-
class Writer
|
5
|
-
def initialize(
|
6
|
-
self.wkhtmltopdf =
|
4
|
+
class Writer
|
5
|
+
def initialize(wkhtmltopdf = nil, options = {})
|
6
|
+
self.wkhtmltopdf = wkhtmltopdf unless wkhtmltopdf.nil?
|
7
|
+
self.options = options
|
7
8
|
end
|
8
9
|
|
9
10
|
def to_pdf(string, options={})
|
10
|
-
|
11
|
-
|
11
|
+
invoke = self.command(options).join(' ')
|
12
|
+
self.log(invoke) if Wisepdf::Configuration.development? || Wisepdf::Configuration.test?
|
12
13
|
|
13
|
-
invoke = self.command.join(' ')
|
14
|
-
log(invoke) if Wisepdf::Configuration.development? || Wisepdf::Configuration.test?
|
15
|
-
|
16
|
-
# result = IO.popen(invoke, "wb+") do |f|
|
17
|
-
# # f.sync = true
|
18
|
-
# f.puts(string)
|
19
|
-
# f.close_write
|
20
|
-
# f.gets(nil)
|
21
|
-
#
|
22
|
-
# # pdf = f.gets(nil)
|
23
|
-
# # f.close
|
24
|
-
# # pdf
|
25
|
-
# end
|
26
14
|
result, err = Open3.popen3(invoke) do |stdin, stdout, stderr|
|
27
15
|
stdin.write(string)
|
28
16
|
stdin.close
|
29
17
|
[stdout.read, stderr.read]
|
30
18
|
end
|
31
|
-
# result, err = Open3.popen3(invoke) do |stdin, stdout, stderr|
|
32
|
-
# stdin.binmode
|
33
|
-
# stdout.binmode
|
34
|
-
# stderr.binmode
|
35
|
-
# stdin.write(string)
|
36
|
-
# stdin.close_write
|
37
|
-
#
|
38
|
-
# [stdout.read, stderr.read]
|
39
|
-
# end
|
40
|
-
|
41
19
|
|
42
20
|
raise Wisepdf::WriteError if result.to_s.strip.empty?
|
43
21
|
|
@@ -45,23 +23,30 @@ module Wisepdf
|
|
45
23
|
end
|
46
24
|
|
47
25
|
def wkhtmltopdf
|
48
|
-
|
49
|
-
|
50
|
-
@wkhtmltopdf = Wisepdf::Configuration.wkhtmltopdf
|
51
|
-
raise Wisepdf::NoExecutableError.new(@wkhtmltopdf) if @wkhtmltopdf.nil? || !File.exists?(@wkhtmltopdf)
|
52
|
-
|
53
|
-
return @wkhtmltopdf
|
26
|
+
@wkhtmltopdf ||= Wisepdf::Configuration.wkhtmltopdf
|
27
|
+
@wkhtmltopdf
|
54
28
|
end
|
55
29
|
|
56
|
-
def wkhtmltopdf=(value)
|
30
|
+
def wkhtmltopdf=(value)
|
57
31
|
@wkhtmltopdf = value
|
58
|
-
raise Wisepdf::NoExecutableError.new(@wkhtmltopdf) if @wkhtmltopdf.
|
32
|
+
raise Wisepdf::NoExecutableError.new(@wkhtmltopdf) if @wkhtmltopdf.blank? || !File.exists?(@wkhtmltopdf)
|
59
33
|
end
|
60
|
-
|
61
|
-
|
62
|
-
|
34
|
+
|
35
|
+
def options
|
36
|
+
@options ||= Wisepdf::Parser.parse(Wisepdf::Configuration.options.dup)
|
37
|
+
@options
|
38
|
+
end
|
39
|
+
|
40
|
+
def options=(value)
|
41
|
+
self.options.merge!(Wisepdf::Parser.parse(value))
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
def command(options = {})
|
46
|
+
options = Wisepdf::Parser.parse(options)
|
47
|
+
|
63
48
|
args = [self.wkhtmltopdf]
|
64
|
-
args +=
|
49
|
+
args += self.options.merge(options).to_a.flatten.compact
|
65
50
|
args << '--quiet'
|
66
51
|
|
67
52
|
args << '-'
|
@@ -70,46 +55,6 @@ module Wisepdf
|
|
70
55
|
args.map {|arg| %Q{"#{arg.gsub('"', '\"')}"}}
|
71
56
|
end
|
72
57
|
|
73
|
-
def normalize_options(options)
|
74
|
-
options = self.flatten(options)
|
75
|
-
normalized_options = {}
|
76
|
-
|
77
|
-
options.each do |key, value|
|
78
|
-
next if !value
|
79
|
-
normalized_key = "--#{self.normalize_arg(key)}"
|
80
|
-
normalized_options[normalized_key] = self.normalize_value(value)
|
81
|
-
end
|
82
|
-
normalized_options
|
83
|
-
end
|
84
|
-
|
85
|
-
def flatten(options, prefix = nil)
|
86
|
-
hash = {}
|
87
|
-
options.each do |k,v|
|
88
|
-
key = prefix.nil? ? k : "#{prefix.to_s}-#{k}"
|
89
|
-
|
90
|
-
if v.is_a?(Hash)
|
91
|
-
hash.delete(k)
|
92
|
-
hash.merge!(self.flatten(v, key))
|
93
|
-
else
|
94
|
-
hash[key.to_s] = v
|
95
|
-
end
|
96
|
-
end
|
97
|
-
return hash
|
98
|
-
end
|
99
|
-
|
100
|
-
def normalize_arg(arg)
|
101
|
-
arg.to_s.downcase.gsub(/[^a-z0-9]/,'-')
|
102
|
-
end
|
103
|
-
|
104
|
-
def normalize_value(value)
|
105
|
-
case value
|
106
|
-
when TrueClass
|
107
|
-
nil
|
108
|
-
else
|
109
|
-
value.to_s
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
58
|
def log(command)
|
114
59
|
puts "*"*15
|
115
60
|
puts command
|
data/lib/wisepdf.rb
CHANGED
@@ -1,7 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require '
|
6
|
-
require '
|
7
|
-
require '
|
1
|
+
WISEPDF_PATH = File.dirname(__FILE__) + "/wisepdf/"
|
2
|
+
|
3
|
+
require WISEPDF_PATH + 'errors'
|
4
|
+
require WISEPDF_PATH + 'configuration'
|
5
|
+
require WISEPDF_PATH + 'parser'
|
6
|
+
require WISEPDF_PATH + 'writer'
|
7
|
+
require WISEPDF_PATH + 'helper'
|
8
|
+
require WISEPDF_PATH + 'render'
|
9
|
+
require WISEPDF_PATH + 'rails' if defined?(Rails)
|
10
|
+
|
11
|
+
module Wisepdf; end
|
@@ -23,5 +23,10 @@ class ApplicationControllerTest < ActionController::TestCase
|
|
23
23
|
should "respond to #prerender_header_and_footer" do
|
24
24
|
assert_respond_to @controller, :prerender_header_and_footer
|
25
25
|
end
|
26
|
+
|
27
|
+
should 'render pdf' do
|
28
|
+
get :index, :format => :pdf
|
29
|
+
assert_response 200
|
30
|
+
end
|
26
31
|
end
|
27
32
|
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
class ConfigurationTest < Test::Unit::TestCase
|
2
|
+
context "Default configuration" do
|
3
|
+
setup do
|
4
|
+
Wisepdf::Configuration.reset!
|
5
|
+
end
|
6
|
+
|
7
|
+
should 'read default configuration' do
|
8
|
+
assert_equal "UTF-8", Wisepdf::Configuration.options[:encoding]
|
9
|
+
assert_equal false, Wisepdf::Configuration.options[:use_xserver]
|
10
|
+
end
|
11
|
+
|
12
|
+
if RbConfig::CONFIG['target_os'] != 'mingw32'
|
13
|
+
should 'try to find wkhtmltopdf if not on windows' do
|
14
|
+
path = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
15
|
+
|
16
|
+
assert_equal path, Wisepdf::Configuration.wkhtmltopdf
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
context "Configuration" do
|
22
|
+
setup do
|
23
|
+
Wisepdf::Configuration.reset!
|
24
|
+
end
|
25
|
+
|
26
|
+
should 'accept and override default configuration' do
|
27
|
+
Wisepdf::Configuration.configure do |config|
|
28
|
+
config.wkhtmltopdf = '/path/to/wkhtmltopdf'
|
29
|
+
config.options = {
|
30
|
+
:layout => "layout.html",
|
31
|
+
:use_xserver => true,
|
32
|
+
:footer => {
|
33
|
+
:right => "#{Date.today.year}",
|
34
|
+
:font_size => 8,
|
35
|
+
:spacing => 8
|
36
|
+
},
|
37
|
+
:margin => {
|
38
|
+
:bottom => 15
|
39
|
+
}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
assert_equal '/path/to/wkhtmltopdf', Wisepdf::Configuration.wkhtmltopdf
|
43
|
+
|
44
|
+
assert_equal 'layout.html', Wisepdf::Configuration.options[:layout]
|
45
|
+
assert_equal true, Wisepdf::Configuration.options[:use_xserver]
|
46
|
+
assert_equal "#{Date.today.year}", Wisepdf::Configuration.options[:footer][:right]
|
47
|
+
assert_equal 8, Wisepdf::Configuration.options[:footer][:font_size]
|
48
|
+
assert_equal 8, Wisepdf::Configuration.options[:footer][:spacing]
|
49
|
+
assert_equal 15, Wisepdf::Configuration.options[:margin][:bottom]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
HELLO
|
data/test/dummy/config/routes.rb
CHANGED
data/test/parser_test.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
class ParserTest < Test::Unit::TestCase
|
2
|
+
context "Options normalization" do
|
3
|
+
setup do
|
4
|
+
Wisepdf::Configuration.reset!
|
5
|
+
|
6
|
+
@options = { Wisepdf::Parser::ESCAPED_OPTIONS.sample => 'value' }
|
7
|
+
end
|
8
|
+
|
9
|
+
should 'escape and parse digit options' do
|
10
|
+
@options.merge!({
|
11
|
+
:key => 10
|
12
|
+
})
|
13
|
+
expected = {
|
14
|
+
'--key' => '10'
|
15
|
+
}
|
16
|
+
|
17
|
+
assert_equal expected, Wisepdf::Parser.parse(@options)
|
18
|
+
end
|
19
|
+
|
20
|
+
should 'escape and parse string options' do
|
21
|
+
@options.merge!({
|
22
|
+
:key => 'value'
|
23
|
+
})
|
24
|
+
expected = {
|
25
|
+
'--key' => 'value'
|
26
|
+
}
|
27
|
+
|
28
|
+
assert_equal expected, Wisepdf::Parser.parse(@options)
|
29
|
+
end
|
30
|
+
|
31
|
+
should 'escape and parse boolean (true) options' do
|
32
|
+
@options.merge!({
|
33
|
+
:key => true
|
34
|
+
})
|
35
|
+
expected = {
|
36
|
+
'--key' => nil
|
37
|
+
}
|
38
|
+
|
39
|
+
assert_equal expected, Wisepdf::Parser.parse(@options)
|
40
|
+
end
|
41
|
+
|
42
|
+
should 'escape and parse boolean (false) options' do
|
43
|
+
@options.merge!({
|
44
|
+
:key => false
|
45
|
+
})
|
46
|
+
expected = {}
|
47
|
+
|
48
|
+
assert_equal expected, Wisepdf::Parser.parse(@options)
|
49
|
+
end
|
50
|
+
|
51
|
+
should 'escape and parse nested options' do
|
52
|
+
@options.merge!({
|
53
|
+
:key => 'value',
|
54
|
+
:nested => {
|
55
|
+
:key => 'value'
|
56
|
+
}
|
57
|
+
})
|
58
|
+
expected = {
|
59
|
+
'--key' => 'value',
|
60
|
+
'--nested-key' => 'value'
|
61
|
+
}
|
62
|
+
|
63
|
+
assert_equal expected, Wisepdf::Parser.parse(@options)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/test/writer_test.rb
CHANGED
@@ -2,153 +2,11 @@ require 'helper'
|
|
2
2
|
|
3
3
|
HTML_DOCUMENT = "<html><body>Hello World</body></html>"
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# end
|
5
|
+
class Wisepdf::Writer
|
6
|
+
public :command
|
7
|
+
end
|
9
8
|
|
10
9
|
class WriterTest < Test::Unit::TestCase
|
11
|
-
context "Default configuration" do
|
12
|
-
setup do
|
13
|
-
Wisepdf::Configuration.reset!
|
14
|
-
end
|
15
|
-
|
16
|
-
should 'read default configuration' do
|
17
|
-
assert_equal 'pdf.html', Wisepdf::Configuration.options[:layout]
|
18
|
-
assert_equal false, Wisepdf::Configuration.options[:use_xserver]
|
19
|
-
end
|
20
|
-
|
21
|
-
if RbConfig::CONFIG['target_os'] != 'mingw32'
|
22
|
-
should 'try to find wkhtmltopdf if not on windows' do
|
23
|
-
path = (defined?(Bundler) ? `bundle exec which wkhtmltopdf` : `which wkhtmltopdf`).chomp
|
24
|
-
|
25
|
-
assert_equal path, Wisepdf::Configuration.wkhtmltopdf
|
26
|
-
end
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
context "Configuration" do
|
31
|
-
setup do
|
32
|
-
Wisepdf::Configuration.reset!
|
33
|
-
end
|
34
|
-
|
35
|
-
should 'accept and override default configuration' do
|
36
|
-
Wisepdf::Configuration.configure do |config|
|
37
|
-
config.wkhtmltopdf = '/path/to/wkhtmltopdf'
|
38
|
-
config.options = {
|
39
|
-
:layout => "layout.html",
|
40
|
-
:use_xserver => true,
|
41
|
-
:footer => {
|
42
|
-
:right => "#{Date.today.year}",
|
43
|
-
:font_size => 8,
|
44
|
-
:spacing => 8
|
45
|
-
},
|
46
|
-
:margin => {
|
47
|
-
:bottom => 15
|
48
|
-
}
|
49
|
-
}
|
50
|
-
end
|
51
|
-
assert_equal '/path/to/wkhtmltopdf', Wisepdf::Configuration.wkhtmltopdf
|
52
|
-
|
53
|
-
assert_equal 'layout.html', Wisepdf::Configuration.options[:layout]
|
54
|
-
assert_equal true, Wisepdf::Configuration.options[:use_xserver]
|
55
|
-
assert_equal "#{Date.today.year}", Wisepdf::Configuration.options[:footer][:right]
|
56
|
-
assert_equal 8, Wisepdf::Configuration.options[:footer][:font_size]
|
57
|
-
assert_equal 8, Wisepdf::Configuration.options[:footer][:spacing]
|
58
|
-
assert_equal 15, Wisepdf::Configuration.options[:margin][:bottom]
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
# context "Option parsing" do
|
63
|
-
# setup do
|
64
|
-
# Wisepdf::Configuration.reset!
|
65
|
-
# end
|
66
|
-
#
|
67
|
-
# should "parse header and footer options" do
|
68
|
-
# wp = Wisepdf::Writer.new
|
69
|
-
#
|
70
|
-
# [:header, :footer].each do |hf|
|
71
|
-
# [:center, :font_name, :left, :right].each do |o|
|
72
|
-
# assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} \"header_footer\"",
|
73
|
-
# wp.parse_options(hf => {o => "header_footer"}).strip
|
74
|
-
# end
|
75
|
-
#
|
76
|
-
# [:font_size, :spacing].each do |o|
|
77
|
-
# assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} 12",
|
78
|
-
# wp.parse_options(hf => {o => "12"}).strip
|
79
|
-
# end
|
80
|
-
#
|
81
|
-
# assert_equal "--#{hf.to_s}-line",
|
82
|
-
# wp.parse_options(hf => {:line => true}).strip
|
83
|
-
# assert_equal "--#{hf.to_s}-html \"http://www.abc.com\"",
|
84
|
-
# wp.parse_options(hf => {:html => {:url => 'http://www.abc.com'}}).strip
|
85
|
-
# end
|
86
|
-
# end
|
87
|
-
#
|
88
|
-
# should "parse toc options" do
|
89
|
-
# wp = Wisepdf::Writer.new
|
90
|
-
#
|
91
|
-
# [:level_indentation, :header_text].each do |o|
|
92
|
-
# assert_equal "toc --toc-#{o.to_s.gsub('_', '-')} \"toc\"",
|
93
|
-
# wp.parse_options(:toc => {o => "toc"}).strip
|
94
|
-
# end
|
95
|
-
#
|
96
|
-
# [:text_size_shrink].each do |o|
|
97
|
-
# assert_equal "toc --toc-#{o.to_s.gsub('_', '-')} 5",
|
98
|
-
# wp.parse_options(:toc => {o => 5}).strip
|
99
|
-
# end
|
100
|
-
#
|
101
|
-
# [:disable_toc_links, :disable_dotted_lines].each do |o|
|
102
|
-
# assert_equal "toc --#{o.to_s.gsub('_', '-')}",
|
103
|
-
# wp.parse_options(:toc => {o => true}).strip
|
104
|
-
# end
|
105
|
-
# end
|
106
|
-
#
|
107
|
-
# should "parse outline options" do
|
108
|
-
# wp = Wisepdf::Writer.new
|
109
|
-
#
|
110
|
-
# assert_equal "--outline", wp.parse_options(:outline => {:outline => true}).strip
|
111
|
-
# assert_equal "--outline-depth 5", wp.parse_options(:outline => {:outline_depth => 5}).strip
|
112
|
-
# end
|
113
|
-
#
|
114
|
-
# should "parse margins options" do
|
115
|
-
# wp = Wisepdf::Writer.new
|
116
|
-
#
|
117
|
-
# [:top, :bottom, :left, :right].each do |o|
|
118
|
-
# assert_equal "--margin-#{o.to_s} 12", wp.parse_options(:margin => {o => "12"}).strip
|
119
|
-
# end
|
120
|
-
# end
|
121
|
-
#
|
122
|
-
# should "parse other options" do
|
123
|
-
# wp = Wisepdf::Writer.new
|
124
|
-
#
|
125
|
-
# [ :orientation, :page_size, :proxy, :username, :password, :cover, :dpi,
|
126
|
-
# :encoding, :user_style_sheet
|
127
|
-
# ].each do |o|
|
128
|
-
# assert_equal "--#{o.to_s.gsub('_', '-')} \"opts\"", wp.parse_options(o => "opts").strip
|
129
|
-
# end
|
130
|
-
#
|
131
|
-
# [:cookie, :post].each do |o|
|
132
|
-
# assert_equal "--#{o.to_s.gsub('_', '-')} name value", wp.parse_options(o => "name value").strip
|
133
|
-
#
|
134
|
-
# nv_formatter = ->(number){ "--#{o.to_s.gsub('_', '-')} par#{number} val#{number}" }
|
135
|
-
# assert_equal "#{nv_formatter.call(1)} #{nv_formatter.call(2)}", wp.parse_options(o => ['par1 val1', 'par2 val2']).strip
|
136
|
-
# end
|
137
|
-
#
|
138
|
-
# [:redirect_delay, :zoom, :page_offset].each do |o|
|
139
|
-
# assert_equal "--#{o.to_s.gsub('_', '-')} 5", wp.parse_options(o => 5).strip
|
140
|
-
# end
|
141
|
-
#
|
142
|
-
# [ :book, :default_header, :disable_javascript, :greyscale, :lowquality,
|
143
|
-
# :enable_plugins, :disable_internal_links, :disable_external_links,
|
144
|
-
# :print_media_type, :disable_smart_shrinking, :use_xserver, :no_background
|
145
|
-
# ].each do |o|
|
146
|
-
# assert_equal "--#{o.to_s.gsub('_', '-')}", wp.parse_options(o => true).strip
|
147
|
-
# end
|
148
|
-
# end
|
149
|
-
#
|
150
|
-
# end
|
151
|
-
|
152
10
|
context "PDF generation" do
|
153
11
|
should "generate PDF from html document" do
|
154
12
|
writer = Wisepdf::Writer.new
|
data/wisepdf.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "wisepdf"
|
8
|
-
s.version = "1.
|
8
|
+
s.version = "1.2.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Igor Alexandrov"]
|
12
|
-
s.date = "2012-
|
12
|
+
s.date = "2012-05-02"
|
13
13
|
s.description = "wisepdf uses the shell utility 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 pdf take care of the hard stuff."
|
14
14
|
s.email = "igor.alexandrov@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -30,15 +30,16 @@ Gem::Specification.new do |s|
|
|
30
30
|
"lib/wisepdf/configuration.rb",
|
31
31
|
"lib/wisepdf/errors.rb",
|
32
32
|
"lib/wisepdf/helper.rb",
|
33
|
+
"lib/wisepdf/parser.rb",
|
33
34
|
"lib/wisepdf/rails.rb",
|
34
35
|
"lib/wisepdf/rails/engine.rb",
|
35
36
|
"lib/wisepdf/rails/legacy.rb",
|
36
37
|
"lib/wisepdf/rails/railtie.rb",
|
37
38
|
"lib/wisepdf/render.rb",
|
38
|
-
"lib/wisepdf/tempfile.rb",
|
39
39
|
"lib/wisepdf/writer.rb",
|
40
40
|
"pdf.gemspec",
|
41
41
|
"test/application_controller_test.rb",
|
42
|
+
"test/configuration_test.rb",
|
42
43
|
"test/dummy/README.rdoc",
|
43
44
|
"test/dummy/Rakefile",
|
44
45
|
"test/dummy/app/assets/javascripts/application.js",
|
@@ -49,7 +50,9 @@ Gem::Specification.new do |s|
|
|
49
50
|
"test/dummy/app/helpers/application_helper.rb",
|
50
51
|
"test/dummy/app/mailers/.gitkeep",
|
51
52
|
"test/dummy/app/models/.gitkeep",
|
53
|
+
"test/dummy/app/views/application/index.pdf.erb",
|
52
54
|
"test/dummy/app/views/layouts/application.html.erb",
|
55
|
+
"test/dummy/app/views/layouts/pdf.html.erb",
|
53
56
|
"test/dummy/config.ru",
|
54
57
|
"test/dummy/config/application.rb",
|
55
58
|
"test/dummy/config/boot.rb",
|
@@ -84,6 +87,7 @@ Gem::Specification.new do |s|
|
|
84
87
|
"test/helper.rb",
|
85
88
|
"test/helper_assets_test.rb",
|
86
89
|
"test/helper_legacy_test.rb",
|
90
|
+
"test/parser_test.rb",
|
87
91
|
"test/writer_test.rb",
|
88
92
|
"wisepdf.gemspec"
|
89
93
|
]
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wisepdf
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-05-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
16
|
-
requirement: &
|
16
|
+
requirement: &70283084149760 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: 3.0.0
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70283084149760
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3
|
27
|
-
requirement: &
|
27
|
+
requirement: &70283084148760 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70283084148760
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: wkhtmltopdf-binary
|
38
|
-
requirement: &
|
38
|
+
requirement: &70283084147440 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ! '>='
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: '0'
|
44
44
|
type: :development
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70283084147440
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: shoulda
|
49
|
-
requirement: &
|
49
|
+
requirement: &70283084146520 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: '0'
|
55
55
|
type: :development
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70283084146520
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: bundler
|
60
|
-
requirement: &
|
60
|
+
requirement: &70283084144820 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ~>
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 1.0.0
|
66
66
|
type: :development
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70283084144820
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: jeweler
|
71
|
-
requirement: &
|
71
|
+
requirement: &70283084144340 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ~>
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
version: 1.6.4
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70283084144340
|
80
80
|
description: wisepdf uses the shell utility wkhtmltopdf to serve a PDF file to a user
|
81
81
|
from HTML. In other words, rather than dealing with a PDF generation DSL of some
|
82
82
|
sort, you simply write an HTML view as you would normally, and let pdf take care
|
@@ -101,15 +101,16 @@ files:
|
|
101
101
|
- lib/wisepdf/configuration.rb
|
102
102
|
- lib/wisepdf/errors.rb
|
103
103
|
- lib/wisepdf/helper.rb
|
104
|
+
- lib/wisepdf/parser.rb
|
104
105
|
- lib/wisepdf/rails.rb
|
105
106
|
- lib/wisepdf/rails/engine.rb
|
106
107
|
- lib/wisepdf/rails/legacy.rb
|
107
108
|
- lib/wisepdf/rails/railtie.rb
|
108
109
|
- lib/wisepdf/render.rb
|
109
|
-
- lib/wisepdf/tempfile.rb
|
110
110
|
- lib/wisepdf/writer.rb
|
111
111
|
- pdf.gemspec
|
112
112
|
- test/application_controller_test.rb
|
113
|
+
- test/configuration_test.rb
|
113
114
|
- test/dummy/README.rdoc
|
114
115
|
- test/dummy/Rakefile
|
115
116
|
- test/dummy/app/assets/javascripts/application.js
|
@@ -120,7 +121,9 @@ files:
|
|
120
121
|
- test/dummy/app/helpers/application_helper.rb
|
121
122
|
- test/dummy/app/mailers/.gitkeep
|
122
123
|
- test/dummy/app/models/.gitkeep
|
124
|
+
- test/dummy/app/views/application/index.pdf.erb
|
123
125
|
- test/dummy/app/views/layouts/application.html.erb
|
126
|
+
- test/dummy/app/views/layouts/pdf.html.erb
|
124
127
|
- test/dummy/config.ru
|
125
128
|
- test/dummy/config/application.rb
|
126
129
|
- test/dummy/config/boot.rb
|
@@ -155,6 +158,7 @@ files:
|
|
155
158
|
- test/helper.rb
|
156
159
|
- test/helper_assets_test.rb
|
157
160
|
- test/helper_legacy_test.rb
|
161
|
+
- test/parser_test.rb
|
158
162
|
- test/writer_test.rb
|
159
163
|
- wisepdf.gemspec
|
160
164
|
homepage: http://github.com/igor-alexandrov/wisepdf
|
@@ -172,7 +176,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
172
176
|
version: '0'
|
173
177
|
segments:
|
174
178
|
- 0
|
175
|
-
hash: -
|
179
|
+
hash: -3344427370032180930
|
176
180
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
177
181
|
none: false
|
178
182
|
requirements:
|
data/lib/wisepdf/tempfile.rb
DELETED
@@ -1,11 +0,0 @@
|
|
1
|
-
require 'tempfile'
|
2
|
-
|
3
|
-
module Wisepdf
|
4
|
-
class Tempfile < Tempfile
|
5
|
-
# Replaces Tempfile's +make_tmpname+ with one that honors file extensions.
|
6
|
-
def make_tmpname(basename, n)
|
7
|
-
extension = File.extname(basename)
|
8
|
-
sprintf("%s_%d_%d%s", File.basename(basename, extension), $$, n.to_i, extension)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|