wicked_pdf 0.10.2 → 0.11.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.
- checksums.yaml +4 -4
- data/README.md +11 -6
- data/lib/wicked_pdf.rb +62 -31
- data/lib/wicked_pdf/version.rb +1 -1
- data/test/unit/wicked_pdf_test.rb +18 -7
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 54d0ec248323c907fe336b0dea9afff757737f6d
|
|
4
|
+
data.tar.gz: 35626a59f71132ef14fb866f453e23998e1c1982
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 41d0c2d5e21c6b1086c425e0e4ebe65f09421694f054435b0d97d9d10622dd493a6dc89c606bbbe6afe9a6261a7639d1545efeb7a0faae1d157518c8e6654886
|
|
7
|
+
data.tar.gz: 38cf8a6e92c561713effa3e3f28259a19b8252f9d9810b46f9ee6c31801f895ffbe229748ad10dc81bd215c2db79b78aba8896fbdb81a735d0813c30d83ac077
|
data/README.md
CHANGED
|
@@ -30,7 +30,7 @@ Note that versions before 0.9.0 [have problems](http://code.google.com/p/wkhtmlt
|
|
|
30
30
|
This plugin relies on streams to communicate with wkhtmltopdf.
|
|
31
31
|
|
|
32
32
|
For more information about wkhtmltopdf, see the project's [homepage](http://code.google.com/p/wkhtmltopdf/) and
|
|
33
|
-
[github repo](https://github.com/antialize/wkhtmltopdf). There's also some documentation for a recent, stable version
|
|
33
|
+
[github repo](https://github.com/antialize/wkhtmltopdf). There's also some documentation for a recent, stable version
|
|
34
34
|
on the author's website, [here](http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html).
|
|
35
35
|
|
|
36
36
|
Next:
|
|
@@ -43,7 +43,7 @@ or add this to your Gemfile:
|
|
|
43
43
|
```ruby
|
|
44
44
|
gem 'wicked_pdf'
|
|
45
45
|
```
|
|
46
|
-
|
|
46
|
+
|
|
47
47
|
You may also need to add
|
|
48
48
|
```ruby
|
|
49
49
|
Mime::Type.register "application/pdf", :pdf
|
|
@@ -111,12 +111,12 @@ class ThingsController < ApplicationController
|
|
|
111
111
|
format.html
|
|
112
112
|
format.pdf do
|
|
113
113
|
render :pdf => 'file_name',
|
|
114
|
-
:disposition => 'attachment', # default 'inline'
|
|
114
|
+
:disposition => 'attachment', # default 'inline'
|
|
115
115
|
:template => 'things/show.pdf.erb',
|
|
116
116
|
:file => "#{Rails.root}/files/foo.erb"
|
|
117
117
|
:layout => 'pdf.html', # use 'pdf.html' for a pdf.html.erb file
|
|
118
118
|
:wkhtmltopdf => '/usr/local/bin/wkhtmltopdf', # path to binary
|
|
119
|
-
:show_as_html => params[:debug].present?, # allow
|
|
119
|
+
:show_as_html => params[:debug].present?, # allow debugging based on url param
|
|
120
120
|
:orientation => 'Landscape', # default Portrait
|
|
121
121
|
:page_size => 'A4, Letter, ...', # default A4
|
|
122
122
|
:save_to_file => Rails.root.join('pdfs', "#{filename}.pdf"),
|
|
@@ -149,6 +149,7 @@ class ThingsController < ApplicationController
|
|
|
149
149
|
:disable_smart_shrinking => true,
|
|
150
150
|
:use_xserver => true,
|
|
151
151
|
:no_background => true,
|
|
152
|
+
:viewport_size => 'TEXT' # available only with use_xserver or patched QT
|
|
152
153
|
:extra => '' # directly inserted into the command to wkhtmltopdf
|
|
153
154
|
:margin => {:top => SIZE, # default 10 (mm)
|
|
154
155
|
:bottom => SIZE,
|
|
@@ -215,6 +216,10 @@ If you need to just create a pdf and not display it:
|
|
|
215
216
|
# create a pdf from a string
|
|
216
217
|
pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
|
|
217
218
|
|
|
219
|
+
# create a pdf file from a html file without converting it to string
|
|
220
|
+
# Path must be absolute path
|
|
221
|
+
pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')
|
|
222
|
+
|
|
218
223
|
# create a pdf from string using templates, layouts and content option for header or footer
|
|
219
224
|
WickedPdf.new.pdf_from_string(
|
|
220
225
|
render_to_string('templates/pdf.html.erb', :layout => 'pdfs/layout_pdf'),
|
|
@@ -222,10 +227,10 @@ WickedPdf.new.pdf_from_string(
|
|
|
222
227
|
:content => render_to_string(:layout => 'pdfs/layout_pdf')
|
|
223
228
|
}
|
|
224
229
|
)
|
|
225
|
-
|
|
230
|
+
|
|
226
231
|
# or from your controller, using views & templates and all wicked_pdf options as normal
|
|
227
232
|
pdf = render_to_string :pdf => "some_file_name"
|
|
228
|
-
|
|
233
|
+
|
|
229
234
|
# then save to a file
|
|
230
235
|
save_path = Rails.root.join('pdfs','filename.pdf')
|
|
231
236
|
File.open(save_path, 'wb') do |file|
|
data/lib/wicked_pdf.rb
CHANGED
|
@@ -37,7 +37,7 @@ class WickedPdf
|
|
|
37
37
|
def initialize(wkhtmltopdf_binary_path = nil)
|
|
38
38
|
@exe_path = wkhtmltopdf_binary_path || find_wkhtmltopdf_binary_path
|
|
39
39
|
raise "Location of #{EXE_NAME} unknown" if @exe_path.empty?
|
|
40
|
-
raise "Bad #{EXE_NAME}'s path" unless File.exists?(@exe_path)
|
|
40
|
+
raise "Bad #{EXE_NAME}'s path: #{@exe_path}" unless File.exists?(@exe_path)
|
|
41
41
|
raise "#{EXE_NAME} is not executable" unless File.executable?(@exe_path)
|
|
42
42
|
|
|
43
43
|
@binary_version = DEFAULT_BINARY_VERSION
|
|
@@ -51,20 +51,22 @@ class WickedPdf
|
|
|
51
51
|
end
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
def
|
|
54
|
+
def pdf_from_html_file(filepath, options={})
|
|
55
55
|
if WickedPdf.config[:retreive_version]
|
|
56
56
|
retreive_binary_version
|
|
57
57
|
end
|
|
58
58
|
|
|
59
59
|
temp_path = options.delete(:temp_path)
|
|
60
|
-
string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
|
|
61
|
-
string_file.binmode
|
|
62
|
-
string_file.write(string)
|
|
63
|
-
string_file.close
|
|
64
60
|
generated_pdf_file = WickedPdfTempfile.new("wicked_pdf_generated_file.pdf", temp_path)
|
|
65
|
-
command =
|
|
66
|
-
|
|
67
|
-
|
|
61
|
+
command = [@exe_path]
|
|
62
|
+
command << '-q' unless on_windows? # suppress errors on stdout
|
|
63
|
+
command += parse_options(options)
|
|
64
|
+
command << "file://#{filepath}"
|
|
65
|
+
command << generated_pdf_file.path.to_s
|
|
66
|
+
|
|
67
|
+
print_command(command.inspect) if in_development_mode?
|
|
68
|
+
|
|
69
|
+
err = Open3.popen3(*command) do |stdin, stdout, stderr|
|
|
68
70
|
stderr.read
|
|
69
71
|
end
|
|
70
72
|
if return_file = options.delete(:return_file)
|
|
@@ -78,10 +80,24 @@ class WickedPdf
|
|
|
78
80
|
rescue Exception => e
|
|
79
81
|
raise "Failed to execute:\n#{command}\nError: #{e}"
|
|
80
82
|
ensure
|
|
81
|
-
string_file.close! if string_file
|
|
82
83
|
generated_pdf_file.close! if generated_pdf_file && !return_file
|
|
83
84
|
end
|
|
84
85
|
|
|
86
|
+
def pdf_from_string(string, options={})
|
|
87
|
+
temp_path = options.delete(:temp_path)
|
|
88
|
+
string_file = WickedPdfTempfile.new("wicked_pdf.html", temp_path)
|
|
89
|
+
string_file.binmode
|
|
90
|
+
string_file.write(string)
|
|
91
|
+
string_file.close
|
|
92
|
+
|
|
93
|
+
pdf = pdf_from_html_file(string_file.path, options)
|
|
94
|
+
pdf
|
|
95
|
+
rescue Exception => e
|
|
96
|
+
raise "Error: #{e}"
|
|
97
|
+
ensure
|
|
98
|
+
string_file.close! if string_file
|
|
99
|
+
end
|
|
100
|
+
|
|
85
101
|
private
|
|
86
102
|
|
|
87
103
|
def in_development_mode?
|
|
@@ -122,40 +138,50 @@ class WickedPdf
|
|
|
122
138
|
parse_margins(options.delete(:margin)),
|
|
123
139
|
parse_others(options),
|
|
124
140
|
parse_basic_auth(options)
|
|
125
|
-
].
|
|
141
|
+
].flatten
|
|
126
142
|
end
|
|
127
143
|
|
|
128
144
|
def parse_extra(options)
|
|
129
|
-
|
|
145
|
+
return [] if options[:extra].nil?
|
|
146
|
+
return options[:extra].split if options[:extra].respond_to?(:split)
|
|
147
|
+
return options[:extra]
|
|
130
148
|
end
|
|
131
149
|
|
|
132
150
|
def parse_basic_auth(options)
|
|
133
151
|
if options[:basic_auth]
|
|
134
152
|
user, passwd = Base64.decode64(options[:basic_auth]).split(":")
|
|
135
|
-
"--username
|
|
153
|
+
["--username", user, "--password", passwd]
|
|
136
154
|
else
|
|
137
|
-
|
|
155
|
+
[]
|
|
138
156
|
end
|
|
139
157
|
end
|
|
140
158
|
|
|
141
159
|
def make_option(name, value, type=:string)
|
|
142
160
|
if value.is_a?(Array)
|
|
143
|
-
return value.collect { |v| make_option(name, v, type) }
|
|
161
|
+
return value.collect { |v| make_option(name, v, type) }
|
|
162
|
+
end
|
|
163
|
+
if type == :boolean
|
|
164
|
+
["--#{name.gsub('_', '-')}"]
|
|
165
|
+
else
|
|
166
|
+
["--#{name.gsub('_', '-')}", value.to_s]
|
|
144
167
|
end
|
|
145
|
-
"--#{name.gsub('_', '-')} " + case type
|
|
146
|
-
when :boolean then ""
|
|
147
|
-
when :numeric then value.to_s
|
|
148
|
-
when :name_value then value.to_s
|
|
149
|
-
else "\"#{value}\""
|
|
150
|
-
end + " "
|
|
151
168
|
end
|
|
152
169
|
|
|
153
170
|
def make_options(options, names, prefix="", type=:string)
|
|
154
|
-
|
|
171
|
+
return [] if options.nil?
|
|
172
|
+
names.collect do |o|
|
|
173
|
+
if options[o].blank?
|
|
174
|
+
[]
|
|
175
|
+
else
|
|
176
|
+
make_option("#{prefix.blank? ? "" : prefix + "-"}#{o.to_s}",
|
|
177
|
+
options[o],
|
|
178
|
+
type)
|
|
179
|
+
end
|
|
180
|
+
end
|
|
155
181
|
end
|
|
156
182
|
|
|
157
183
|
def parse_header_footer(options)
|
|
158
|
-
r=
|
|
184
|
+
r=[]
|
|
159
185
|
[:header, :footer].collect do |hf|
|
|
160
186
|
unless options[hf].blank?
|
|
161
187
|
opt_hf = options[hf]
|
|
@@ -181,22 +207,22 @@ class WickedPdf
|
|
|
181
207
|
|
|
182
208
|
def parse_cover(argument)
|
|
183
209
|
arg = argument.to_s
|
|
184
|
-
return
|
|
185
|
-
r = '--cover '
|
|
210
|
+
return [] if arg.blank?
|
|
186
211
|
# Filesystem path or URL - hand off to wkhtmltopdf
|
|
187
212
|
if argument.is_a?(Pathname) || (arg[0,4] == 'http')
|
|
188
|
-
|
|
213
|
+
['--cover', arg]
|
|
189
214
|
else # HTML content
|
|
190
215
|
@hf_tempfiles ||= []
|
|
191
216
|
@hf_tempfiles << tf=WickedPdfTempfile.new("wicked_cover_pdf.html")
|
|
192
217
|
tf.write arg
|
|
193
218
|
tf.flush
|
|
194
|
-
|
|
219
|
+
['--cover', tf.path]
|
|
195
220
|
end
|
|
196
221
|
end
|
|
197
222
|
|
|
198
223
|
def parse_toc(options)
|
|
199
|
-
|
|
224
|
+
return [] if options.nil?
|
|
225
|
+
r = ['--toc']
|
|
200
226
|
unless options.blank?
|
|
201
227
|
r += make_options(options, [ :font_name, :header_text], "toc")
|
|
202
228
|
r +=make_options(options, [ :depth,
|
|
@@ -223,19 +249,22 @@ class WickedPdf
|
|
|
223
249
|
end
|
|
224
250
|
|
|
225
251
|
def parse_outline(options)
|
|
252
|
+
r = []
|
|
226
253
|
unless options.blank?
|
|
227
254
|
r = make_options(options, [:outline], "", :boolean)
|
|
228
255
|
r +=make_options(options, [:outline_depth], "", :numeric)
|
|
229
256
|
end
|
|
257
|
+
r
|
|
230
258
|
end
|
|
231
259
|
|
|
232
260
|
def parse_margins(options)
|
|
233
|
-
make_options(options, [:top, :bottom, :left, :right], "margin", :numeric)
|
|
261
|
+
make_options(options, [:top, :bottom, :left, :right], "margin", :numeric)
|
|
234
262
|
end
|
|
235
263
|
|
|
236
264
|
def parse_others(options)
|
|
265
|
+
r = []
|
|
237
266
|
unless options.blank?
|
|
238
|
-
r
|
|
267
|
+
r += make_options(options, [ :orientation,
|
|
239
268
|
:page_size,
|
|
240
269
|
:page_width,
|
|
241
270
|
:page_height,
|
|
@@ -244,7 +273,8 @@ class WickedPdf
|
|
|
244
273
|
:password,
|
|
245
274
|
:dpi,
|
|
246
275
|
:encoding,
|
|
247
|
-
:user_style_sheet
|
|
276
|
+
:user_style_sheet,
|
|
277
|
+
:viewport_size])
|
|
248
278
|
r +=make_options(options, [ :cookie,
|
|
249
279
|
:post], "", :name_value)
|
|
250
280
|
r +=make_options(options, [ :redirect_delay,
|
|
@@ -266,6 +296,7 @@ class WickedPdf
|
|
|
266
296
|
:no_background], "", :boolean)
|
|
267
297
|
r +=make_options(options, [ :no_stop_slow_scripts ], "", nil)
|
|
268
298
|
end
|
|
299
|
+
r
|
|
269
300
|
end
|
|
270
301
|
|
|
271
302
|
def find_wkhtmltopdf_binary_path
|
data/lib/wicked_pdf/version.rb
CHANGED
|
@@ -3,10 +3,12 @@ require 'test_helper'
|
|
|
3
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
|
-
# Provide a public accessor to the normally-private parse_options function
|
|
6
|
+
# Provide a public accessor to the normally-private parse_options function.
|
|
7
|
+
# Also, smash the returned array of options into a single string for
|
|
8
|
+
# convenience in testing below.
|
|
7
9
|
class WickedPdf
|
|
8
10
|
def get_parsed_options(opts)
|
|
9
|
-
parse_options(opts)
|
|
11
|
+
parse_options(opts).join(' ')
|
|
10
12
|
end
|
|
11
13
|
end
|
|
12
14
|
|
|
@@ -22,7 +24,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
22
24
|
assert pdf.rstrip.end_with?("%%EOF")
|
|
23
25
|
assert pdf.length > 100
|
|
24
26
|
end
|
|
25
|
-
|
|
27
|
+
|
|
26
28
|
test "should generate PDF from html document with long lines" do
|
|
27
29
|
wp = WickedPdf.new
|
|
28
30
|
document_with_long_line_file = File.new("test/fixtures/document_with_long_line.html", "r")
|
|
@@ -32,6 +34,15 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
32
34
|
assert pdf.length > 100
|
|
33
35
|
end
|
|
34
36
|
|
|
37
|
+
test "should generate PDF from html existing HTML file without converting it to string" do
|
|
38
|
+
wp = WickedPdf.new
|
|
39
|
+
filepath = File.join(Dir.pwd, "test/fixtures/document_with_long_line.html")
|
|
40
|
+
pdf = wp.pdf_from_html_file(filepath)
|
|
41
|
+
assert pdf.start_with?("%PDF-1.4")
|
|
42
|
+
assert pdf.rstrip.end_with?("%%EOF")
|
|
43
|
+
assert pdf.length > 100
|
|
44
|
+
end
|
|
45
|
+
|
|
35
46
|
test "should raise exception when no path to wkhtmltopdf" do
|
|
36
47
|
assert_raise RuntimeError do
|
|
37
48
|
WickedPdf.new " "
|
|
@@ -76,7 +87,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
76
87
|
|
|
77
88
|
[:header, :footer].each do |hf|
|
|
78
89
|
[:center, :font_name, :left, :right].each do |o|
|
|
79
|
-
assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')}
|
|
90
|
+
assert_equal "--#{hf.to_s}-#{o.to_s.gsub('_', '-')} header_footer",
|
|
80
91
|
wp.get_parsed_options(hf => {o => "header_footer"}).strip
|
|
81
92
|
end
|
|
82
93
|
|
|
@@ -87,7 +98,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
87
98
|
|
|
88
99
|
assert_equal "--#{hf.to_s}-line",
|
|
89
100
|
wp.get_parsed_options(hf => {:line => true}).strip
|
|
90
|
-
assert_equal "--#{hf.to_s}-html
|
|
101
|
+
assert_equal "--#{hf.to_s}-html http://www.abc.com",
|
|
91
102
|
wp.get_parsed_options(hf => {:html => {:url => 'http://www.abc.com'}}).strip
|
|
92
103
|
end
|
|
93
104
|
end
|
|
@@ -96,7 +107,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
96
107
|
wp = WickedPdf.new
|
|
97
108
|
|
|
98
109
|
[:font_name, :header_text].each do |o|
|
|
99
|
-
assert_equal "--toc --toc-#{o.to_s.gsub('_', '-')}
|
|
110
|
+
assert_equal "--toc --toc-#{o.to_s.gsub('_', '-')} toc",
|
|
100
111
|
wp.get_parsed_options(:toc => {o => "toc"}).strip
|
|
101
112
|
end
|
|
102
113
|
|
|
@@ -143,7 +154,7 @@ class WickedPdfTest < ActiveSupport::TestCase
|
|
|
143
154
|
[ :orientation, :page_size, :proxy, :username, :password, :dpi,
|
|
144
155
|
:encoding, :user_style_sheet
|
|
145
156
|
].each do |o|
|
|
146
|
-
assert_equal "--#{o.to_s.gsub('_', '-')}
|
|
157
|
+
assert_equal "--#{o.to_s.gsub('_', '-')} opts", wp.get_parsed_options(o => "opts").strip
|
|
147
158
|
end
|
|
148
159
|
|
|
149
160
|
[:cookie, :post].each do |o|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: wicked_pdf
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.11.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Miles Z. Sterrett
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2014-04
|
|
11
|
+
date: 2014-07-04 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: rails
|