wicked_pdf 1.0.6 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +4 -1
- data/.travis.yml +4 -13
- data/CHANGELOG.md +13 -1
- data/README.md +64 -11
- data/Rakefile +4 -4
- data/gemfiles/2.3.gemfile +1 -1
- data/gemfiles/3.0.gemfile +6 -1
- data/gemfiles/3.1.gemfile +6 -1
- data/gemfiles/3.2.gemfile +6 -1
- data/gemfiles/4.0.gemfile +1 -1
- data/gemfiles/4.1.gemfile +1 -1
- data/gemfiles/4.2.gemfile +1 -1
- data/gemfiles/5.0.gemfile +6 -0
- data/gemfiles/rails_edge.gemfile +1 -1
- data/lib/wicked_pdf.rb +11 -8
- data/lib/wicked_pdf/pdf_helper.rb +111 -90
- data/lib/wicked_pdf/railtie.rb +35 -24
- data/lib/wicked_pdf/tempfile.rb +9 -7
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper.rb +28 -26
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +106 -96
- data/test/fixtures/wicked.js +1 -0
- data/test/functional/pdf_helper_test.rb +4 -6
- data/test/functional/wicked_pdf_helper_assets_test.rb +27 -9
- data/test/functional/wicked_pdf_helper_test.rb +15 -13
- data/test/test_helper.rb +6 -0
- data/test/unit/wicked_pdf_test.rb +3 -3
- data/wicked_pdf.gemspec +3 -2
- metadata +6 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c29352b7951b2cb5013de56cc41c3929362a248
|
4
|
+
data.tar.gz: 7fe7fa7843fc6b9ac7bfe6ec98cf0676c274fef8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d83f67d5a93d3579f81786d2aeaca8b8f0332afa48f3cee3274614ef051f6edff25a8486bcd2965ec5b1e6996c6dbf700ab71d678d3bf83f6ec2a391811c1d8
|
7
|
+
data.tar.gz: 41c7b3744daa9bcd60f4bd68f2c13173d44496be0dc7668b42de651e91c9eaa0ad68d826583c1475cd88e587f6eb3efcc4a376b8ca9e6e31554efec5812390c9
|
data/.rubocop_todo.yml
CHANGED
data/.travis.yml
CHANGED
@@ -24,8 +24,7 @@ before_script:
|
|
24
24
|
- "sh -e /etc/init.d/xvfb start"
|
25
25
|
env: WKHTMLTOPDF_BIN=/usr/bin/wkhtmltopdf
|
26
26
|
script:
|
27
|
-
- bundle exec rake
|
28
|
-
- bundle exec rake test
|
27
|
+
- bundle exec rake
|
29
28
|
matrix:
|
30
29
|
include:
|
31
30
|
- rvm: 1.8.7
|
@@ -34,17 +33,15 @@ matrix:
|
|
34
33
|
- gem update --system 1.8.25
|
35
34
|
- gem --version
|
36
35
|
gemfile: gemfiles/2.3.gemfile
|
37
|
-
- rvm: 1.9.3
|
38
|
-
gemfile: gemfiles/rails_edge.gemfile
|
39
36
|
- rvm: 2.0.0
|
40
37
|
gemfile: gemfiles/4.0.gemfile
|
41
|
-
- rvm: 2.0.0
|
42
|
-
gemfile: gemfiles/rails_edge.gemfile
|
43
38
|
- rvm: 2.2.0
|
44
39
|
gemfile: gemfiles/4.1.gemfile
|
45
40
|
- rvm: 2.2.0
|
46
41
|
gemfile: gemfiles/4.2.gemfile
|
47
|
-
- rvm: 2.
|
42
|
+
- rvm: 2.3.0
|
43
|
+
gemfile: gemfiles/5.0.gemfile
|
44
|
+
- rvm: 2.2.2
|
48
45
|
gemfile: gemfiles/rails_edge.gemfile
|
49
46
|
- rvm: 2.3.0
|
50
47
|
gemfile: gemfiles/rails_edge.gemfile
|
@@ -57,10 +54,4 @@ matrix:
|
|
57
54
|
gemfile: gemfiles/3.2.gemfile
|
58
55
|
- rvm: 1.9.3
|
59
56
|
gemfile: gemfiles/4.0.gemfile
|
60
|
-
- rvm: 1.9.3
|
61
|
-
gemfile: gemfiles/rails_edge.gemfile
|
62
|
-
- rvm: 2.0.0
|
63
|
-
gemfile: gemfiles/rails_edge.gemfile
|
64
|
-
- rvm: 2.2.0
|
65
|
-
gemfile: gemfiles/rails_edge.gemfile
|
66
57
|
- rvm: ruby-head
|
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,17 @@
|
|
2
2
|
All notable changes to this project will be documented in this file.
|
3
3
|
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
4
|
|
5
|
+
## [1.1.0] - 2016-09-30
|
6
|
+
### Added
|
7
|
+
- Support Rails 5.x and Sprockets 3.x
|
8
|
+
- Support `window_status: 'somestring'` option, to instruct wkhtmltopdf to wait until the browser `window.status` is equal to the supplied string. This can be useful to force rendering to wait [as explained quite well here](https://spin.atomicobject.com/2015/08/29/ember-app-done-loading/)
|
9
|
+
- Support `no_stop_slow_scripts: true` to let slow running scripts delay rendering
|
10
|
+
|
11
|
+
### Changed
|
12
|
+
- [Improved error handling](https://github.com/mileszs/wicked_pdf/pull/543)
|
13
|
+
- [Namespace helper classes under WickedPdf namespace](https://github.com/mileszs/wicked_pdf/pull/538)
|
14
|
+
- [Changes to asset finding to support Rails 5](https://github.com/mileszs/wicked_pdf/pull/561)
|
15
|
+
|
5
16
|
## [1.0.6] - 2016-04-04
|
6
17
|
### Changed
|
7
18
|
- Revert shellescaping of options. The fix was causing more issues than it solved (like "[page] of [topage]" being escaped, and thus not parsed by `wkhtmltopdf`). See #514 for details.
|
@@ -69,7 +80,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
69
80
|
- Asset helpers no longer add a file extension if it already is specified with one
|
70
81
|
|
71
82
|
# Compare Releases
|
72
|
-
- [1.0
|
83
|
+
- [1.1.0...HEAD](https://github.com/mileszs/wicked_pdf/compare/1.1.0...HEAD)
|
84
|
+
- [1.0.6...1.1.0](https://github.com/mileszs/wicked_pdf/compare/1.0.6...1.1.0)
|
73
85
|
- [1.0.5...1.0.6](https://github.com/mileszs/wicked_pdf/compare/1.0.5...1.0.6)
|
74
86
|
- [1.0.4...1.0.5](https://github.com/mileszs/wicked_pdf/compare/1.0.4...1.0.5)
|
75
87
|
- [1.0.3...1.0.4](https://github.com/mileszs/wicked_pdf/compare/1.0.3...1.0.4)
|
data/README.md
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
Wicked PDF uses the shell utility [wkhtmltopdf](http://wkhtmltopdf.org) 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 PDF take care of the hard stuff.
|
6
6
|
|
7
|
-
_Wicked PDF has been verified to work on Ruby versions 1.8.7 through 2.
|
7
|
+
_Wicked PDF has been verified to work on Ruby versions 1.8.7 through 2.3; Rails 2 through 5.0_
|
8
8
|
|
9
9
|
### Installation
|
10
10
|
|
@@ -35,7 +35,7 @@ gem 'wkhtmltopdf-binary'
|
|
35
35
|
|
36
36
|
To your Gemfile and run `bundle install`.
|
37
37
|
|
38
|
-
This wrapper may trail in versions, at the moment it wraps the 0.9 version of `wkhtmltopdf` while there is 0.12 version available. Some of the advanced options listed below
|
38
|
+
This wrapper may trail in versions, at the moment it wraps the 0.9 version of `wkhtmltopdf` while there is 0.12 version available. Some of the advanced options listed below are not available with 0.9.
|
39
39
|
|
40
40
|
If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer:
|
41
41
|
|
@@ -62,7 +62,7 @@ end
|
|
62
62
|
```
|
63
63
|
### Usage Conditions - Important!
|
64
64
|
|
65
|
-
The wkhtmltopdf binary is run outside of your Rails application; therefore, your normal layouts will not work. If you plan to use any CSS,
|
65
|
+
The wkhtmltopdf binary is run outside of your Rails application; therefore, your normal layouts will not work. If you plan to use any CSS, JavaScript, or image files, you must modify your layout so that you provide an absolute reference to these files. The best option for Rails without the asset pipeline is to use the `wicked_pdf_stylesheet_link_tag`, `wicked_pdf_image_tag`, and `wicked_pdf_javascript_include_tag` helpers or to go straight to a CDN (Content Delivery Network) for popular libraries such as jQuery.
|
66
66
|
|
67
67
|
#### wicked_pdf helpers
|
68
68
|
```html
|
@@ -83,6 +83,29 @@ The wkhtmltopdf binary is run outside of your Rails application; therefore, your
|
|
83
83
|
</body>
|
84
84
|
</html>
|
85
85
|
```
|
86
|
+
|
87
|
+
Using wicked_pdf_helpers with asset pipeline raises `Asset names passed to helpers should not include the "/assets/" prefix.` error. To work around this, you can use `wicked_pdf_asset_base64` with the normal Rails helpers, but be aware that this will base64 encode your content and inline it in the page. This is very quick for small assets, but large ones can take a long time.
|
88
|
+
|
89
|
+
```html
|
90
|
+
<!doctype html>
|
91
|
+
<html>
|
92
|
+
<head>
|
93
|
+
<meta charset='utf-8' />
|
94
|
+
<%= stylesheet_link_tag wicked_pdf_asset_base64("pdf") %>
|
95
|
+
<%= javascript_include_tag wicked_pdf_asset_base64("number_pages") %>
|
96
|
+
|
97
|
+
</head>
|
98
|
+
<body onload='number_pages'>
|
99
|
+
<div id="header">
|
100
|
+
<%= image_tag wicked_pdf_asset_base64('mysite.jpg') %>
|
101
|
+
</div>
|
102
|
+
<div id="content">
|
103
|
+
<%= yield %>
|
104
|
+
</div>
|
105
|
+
</body>
|
106
|
+
</html>
|
107
|
+
```
|
108
|
+
|
86
109
|
#### CDN reference
|
87
110
|
|
88
111
|
In this case, you can use that standard Rails helpers and point to the current CDN for whichever framework you are using. For jQuery, it would look somethng like this, given the current versions at the time of this writing.
|
@@ -108,9 +131,9 @@ class ThingsController < ApplicationController
|
|
108
131
|
format.pdf do
|
109
132
|
render pdf: 'file_name',
|
110
133
|
disposition: 'attachment', # default 'inline'
|
111
|
-
template: 'things/show
|
134
|
+
template: 'things/show',
|
112
135
|
file: "#{Rails.root}/files/foo.erb"
|
113
|
-
layout: 'pdf
|
136
|
+
layout: 'pdf', # for a pdf.html.erb file
|
114
137
|
wkhtmltopdf: '/usr/local/bin/wkhtmltopdf', # path to binary
|
115
138
|
show_as_html: params.key?('debug'), # allow debugging based on url param
|
116
139
|
orientation: 'Landscape', # default Portrait
|
@@ -132,6 +155,7 @@ class ThingsController < ApplicationController
|
|
132
155
|
post: ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
|
133
156
|
redirect_delay: NUMBER,
|
134
157
|
javascript_delay: NUMBER,
|
158
|
+
window_status: 'TEXT', # wait to render until some JS sets window.status to the given string
|
135
159
|
image_quality: NUMBER,
|
136
160
|
no_pdf_compression: true,
|
137
161
|
zoom: FLOAT,
|
@@ -157,8 +181,8 @@ class ThingsController < ApplicationController
|
|
157
181
|
bottom: SIZE,
|
158
182
|
left: SIZE,
|
159
183
|
right: SIZE },
|
160
|
-
header: { html: { template: 'users/header
|
161
|
-
layout: 'pdf_plain
|
184
|
+
header: { html: { template: 'users/header', # use :template OR :url
|
185
|
+
layout: 'pdf_plain', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout
|
162
186
|
url: 'www.example.com',
|
163
187
|
locals: { foo: @bar }},
|
164
188
|
center: 'TEXT',
|
@@ -169,8 +193,8 @@ class ThingsController < ApplicationController
|
|
169
193
|
spacing: REAL,
|
170
194
|
line: true,
|
171
195
|
content: 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
|
172
|
-
footer: { html: { template:'shared/footer
|
173
|
-
layout: 'pdf_plain.html', # optional, use 'pdf_plain
|
196
|
+
footer: { html: { template:'shared/footer', # use :template OR :url
|
197
|
+
layout: 'pdf_plain.html', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout
|
174
198
|
url: 'www.example.com',
|
175
199
|
locals: { foo: @bar }},
|
176
200
|
center: 'TEXT',
|
@@ -214,6 +238,19 @@ end
|
|
214
238
|
```
|
215
239
|
By default, it will render without a layout (layout: false) and the template for the current controller and action.
|
216
240
|
|
241
|
+
#### wkhtmltopdf Binary Options
|
242
|
+
|
243
|
+
Some of the options above are being passed to `wkhtmltopdf` binary. They can be used to control the options used in Webkit rendering before generating the PDF.
|
244
|
+
|
245
|
+
Examples of those options are:
|
246
|
+
|
247
|
+
```ruby
|
248
|
+
print_media_type: true # Passes `--print-media-type`
|
249
|
+
no_background: true # Passes `--no-background`
|
250
|
+
```
|
251
|
+
|
252
|
+
You can see the complete list of options under "Global Options" in wkhtmltopdf usage [docs](http://wkhtmltopdf.org/usage/wkhtmltopdf.txt).
|
253
|
+
|
217
254
|
### Super Advanced Usage ###
|
218
255
|
|
219
256
|
If you need to just create a pdf and not display it:
|
@@ -230,14 +267,14 @@ pdf = WickedPdf.new.pdf_from_url('https://github.com/mileszs/wicked_pdf')
|
|
230
267
|
|
231
268
|
# create a pdf from string using templates, layouts and content option for header or footer
|
232
269
|
pdf = WickedPdf.new.pdf_from_string(
|
233
|
-
render_to_string('templates/pdf
|
270
|
+
render_to_string('templates/pdf', layout: 'pdfs/layout_pdf'),
|
234
271
|
footer: {
|
235
272
|
content: render_to_string(layout: 'pdfs/layout_pdf')
|
236
273
|
}
|
237
274
|
)
|
238
275
|
|
239
276
|
# or from your controller, using views & templates and all wicked_pdf options as normal
|
240
|
-
pdf = render_to_string pdf: "some_file_name", template: "templates/pdf
|
277
|
+
pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8"
|
241
278
|
|
242
279
|
# then save to a file
|
243
280
|
save_path = Rails.root.join('pdfs','filename.pdf')
|
@@ -249,6 +286,18 @@ If you need to display utf encoded characters, add this to your pdf views or lay
|
|
249
286
|
```html
|
250
287
|
<meta charset="utf-8" />
|
251
288
|
```
|
289
|
+
|
290
|
+
### Page Breaks
|
291
|
+
|
292
|
+
You can control page breaks with CSS.
|
293
|
+
|
294
|
+
Add a few styles like this to your stylesheet or page:
|
295
|
+
```css
|
296
|
+
div.alwaysbreak { page-break-before: always; }
|
297
|
+
div.nobreak:before { clear:both; }
|
298
|
+
div.nobreak { page-break-inside: avoid; }
|
299
|
+
```
|
300
|
+
|
252
301
|
### Page Numbering
|
253
302
|
|
254
303
|
A bit of javascript can help you number your pages. Create a template or header/footer file with this:
|
@@ -301,10 +350,14 @@ If you use the standard `render pdf: 'some_pdf'` in your app, you will want to e
|
|
301
350
|
|
302
351
|
### Further Reading
|
303
352
|
|
353
|
+
Mike Ackerman's post [How To Create PDFs in Rails](https://www.viget.com/articles/how-to-create-pdfs-in-rails)
|
354
|
+
|
304
355
|
Andreas Happe's post [Generating PDFs from Ruby on Rails](http://www.snikt.net/blog/2012/04/26/wicked-pdf/)
|
305
356
|
|
306
357
|
JESii's post [WickedPDF, wkhtmltopdf, and Heroku...a tricky combination](http://www.nubyrubyrailstales.com/2013/06/wickedpdf-wkhtmltopdf-and-herokua.html)
|
307
358
|
|
359
|
+
Berislav Babic's post [Send PDF attachments from Rails with WickedPdf and ActionMailer](http://berislavbabic.com/send-pdf-attachments-from-rails-with-wickedpdf-and-actionmailer/)
|
360
|
+
|
308
361
|
StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf)
|
309
362
|
|
310
363
|
### Debugging
|
data/Rakefile
CHANGED
@@ -17,7 +17,7 @@ end
|
|
17
17
|
|
18
18
|
desc 'Run RuboCop'
|
19
19
|
task :rubocop do
|
20
|
-
|
20
|
+
next unless RUBY_VERSION >= '2.0.0'
|
21
21
|
require 'rubocop/rake_task'
|
22
22
|
RuboCop::RakeTask.new
|
23
23
|
end
|
@@ -34,9 +34,9 @@ desc 'Generate dummy application for test cases'
|
|
34
34
|
task :dummy_generate do
|
35
35
|
Rake::Task[:dummy_remove].invoke
|
36
36
|
puts 'Creating dummy application to run tests'
|
37
|
-
|
38
|
-
system("rails #{
|
39
|
-
system(
|
37
|
+
command = Rails::VERSION::MAJOR == 2 ? '' : 'new'
|
38
|
+
system("rails #{command} test/dummy")
|
39
|
+
system('touch test/dummy/db/schema.rb')
|
40
40
|
FileUtils.rm_r Dir.glob('test/dummy/test/*')
|
41
41
|
end
|
42
42
|
|
data/gemfiles/2.3.gemfile
CHANGED
data/gemfiles/3.0.gemfile
CHANGED
data/gemfiles/3.1.gemfile
CHANGED
data/gemfiles/3.2.gemfile
CHANGED
data/gemfiles/4.0.gemfile
CHANGED
data/gemfiles/4.1.gemfile
CHANGED
data/gemfiles/4.2.gemfile
CHANGED
data/gemfiles/rails_edge.gemfile
CHANGED
data/lib/wicked_pdf.rb
CHANGED
@@ -59,8 +59,6 @@ class WickedPdf
|
|
59
59
|
|
60
60
|
pdf = pdf_from_html_file(string_file.path, options)
|
61
61
|
pdf
|
62
|
-
rescue => e
|
63
|
-
raise "Error: #{e}"
|
64
62
|
ensure
|
65
63
|
string_file.close! if string_file
|
66
64
|
end
|
@@ -87,7 +85,7 @@ class WickedPdf
|
|
87
85
|
generated_pdf_file.rewind
|
88
86
|
generated_pdf_file.binmode
|
89
87
|
pdf = generated_pdf_file.read
|
90
|
-
|
88
|
+
raise "PDF could not be generated!\n Command Error: #{err}" if pdf && pdf.rstrip.empty?
|
91
89
|
pdf
|
92
90
|
rescue => e
|
93
91
|
raise "Failed to execute:\n#{command}\nError: #{e}"
|
@@ -113,7 +111,7 @@ class WickedPdf
|
|
113
111
|
def retrieve_binary_version
|
114
112
|
_stdin, stdout, _stderr = Open3.popen3(@exe_path + ' -V')
|
115
113
|
@binary_version = parse_version(stdout.gets(nil))
|
116
|
-
rescue
|
114
|
+
rescue
|
117
115
|
DEFAULT_BINARY_VERSION
|
118
116
|
end
|
119
117
|
|
@@ -164,7 +162,11 @@ class WickedPdf
|
|
164
162
|
parts = value.to_s.split(' ')
|
165
163
|
["--#{name.tr('_', '-')}", *parts]
|
166
164
|
elsif type == :boolean
|
167
|
-
|
165
|
+
if value
|
166
|
+
["--#{name.tr('_', '-')}"]
|
167
|
+
else
|
168
|
+
[]
|
169
|
+
end
|
168
170
|
else
|
169
171
|
["--#{name.tr('_', '-')}", value.to_s]
|
170
172
|
end
|
@@ -302,7 +304,8 @@ class WickedPdf
|
|
302
304
|
:password,
|
303
305
|
:encoding,
|
304
306
|
:user_style_sheet,
|
305
|
-
:viewport_size
|
307
|
+
:viewport_size,
|
308
|
+
:window_status])
|
306
309
|
r += make_options(options, [:cookie,
|
307
310
|
:post], '', :name_value)
|
308
311
|
r += make_options(options, [:redirect_delay,
|
@@ -318,8 +321,8 @@ class WickedPdf
|
|
318
321
|
:print_media_type,
|
319
322
|
:disable_smart_shrinking,
|
320
323
|
:use_xserver,
|
321
|
-
:no_background
|
322
|
-
|
324
|
+
:no_background,
|
325
|
+
:no_stop_slow_scripts], '', :boolean)
|
323
326
|
end
|
324
327
|
r
|
325
328
|
end
|
@@ -1,114 +1,135 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
class WickedPdf
|
2
|
+
module PdfHelper
|
3
|
+
def self.included(base)
|
4
|
+
# Protect from trying to augment modules that appear
|
5
|
+
# as the result of adding other gems.
|
6
|
+
return if base != ActionController::Base
|
3
7
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
base.class_eval do
|
10
|
-
alias_method_chain :render, :wicked_pdf
|
11
|
-
alias_method_chain :render_to_string, :wicked_pdf
|
12
|
-
after_filter :clean_temp_files
|
8
|
+
base.class_eval do
|
9
|
+
alias_method_chain :render, :wicked_pdf
|
10
|
+
alias_method_chain :render_to_string, :wicked_pdf
|
11
|
+
after_filter :clean_temp_files
|
12
|
+
end
|
13
13
|
end
|
14
|
-
end
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
15
|
+
def self.prepended(base)
|
16
|
+
# Protect from trying to augment modules that appear
|
17
|
+
# as the result of adding other gems.
|
18
|
+
return if base != ActionController::Base
|
19
|
+
|
20
|
+
base.class_eval do
|
21
|
+
after_action :clean_temp_files
|
22
|
+
|
23
|
+
alias_method :render_without_wicked_pdf, :render
|
24
|
+
alias_method :render_to_string_without_wicked_pdf, :render_to_string
|
25
|
+
|
26
|
+
def render(options = nil, *args, &block)
|
27
|
+
render_with_wicked_pdf(options, *args, &block)
|
28
|
+
end
|
29
|
+
|
30
|
+
def render_to_string(options = nil, *args, &block)
|
31
|
+
render_to_string_with_wicked_pdf(options, *args, &block)
|
32
|
+
end
|
33
|
+
end
|
23
34
|
end
|
24
|
-
end
|
25
35
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
36
|
+
def render_with_wicked_pdf(options = nil, *args, &block)
|
37
|
+
if options.is_a?(Hash) && options.key?(:pdf)
|
38
|
+
log_pdf_creation
|
39
|
+
options[:basic_auth] = set_basic_auth(options)
|
40
|
+
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
|
41
|
+
else
|
42
|
+
render_without_wicked_pdf(options, *args, &block)
|
43
|
+
end
|
34
44
|
end
|
35
|
-
end
|
36
45
|
|
37
|
-
|
46
|
+
def render_to_string_with_wicked_pdf(options = nil, *args, &block)
|
47
|
+
if options.is_a?(Hash) && options.key?(:pdf)
|
48
|
+
log_pdf_creation
|
49
|
+
options[:basic_auth] = set_basic_auth(options)
|
50
|
+
options.delete :pdf
|
51
|
+
make_pdf((WickedPdf.config || {}).merge(options))
|
52
|
+
else
|
53
|
+
render_to_string_without_wicked_pdf(options, *args, &block)
|
54
|
+
end
|
55
|
+
end
|
38
56
|
|
39
|
-
|
40
|
-
logger.info '*' * 15 + 'WICKED' + '*' * 15 if logger && logger.respond_to?(:info)
|
41
|
-
end
|
57
|
+
private
|
42
58
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
request.env['HTTP_AUTHORIZATION'].split(' ').last
|
47
|
-
end
|
59
|
+
def log_pdf_creation
|
60
|
+
logger.info '*' * 15 + 'WICKED' + '*' * 15 if logger && logger.respond_to?(:info)
|
61
|
+
end
|
48
62
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
63
|
+
def set_basic_auth(options = {})
|
64
|
+
options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
|
65
|
+
return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']
|
66
|
+
request.env['HTTP_AUTHORIZATION'].split(' ').last
|
67
|
+
end
|
53
68
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
:formats => options[:formats],
|
59
|
-
:handlers => options[:handlers]
|
60
|
-
}
|
61
|
-
render_opts[:locals] = options[:locals] if options[:locals]
|
62
|
-
render_opts[:file] = options[:file] if options[:file]
|
63
|
-
html_string = render_to_string(render_opts)
|
64
|
-
options = prerender_header_and_footer(options)
|
65
|
-
w = WickedPdf.new(options[:wkhtmltopdf])
|
66
|
-
w.pdf_from_string(html_string, options)
|
67
|
-
end
|
69
|
+
def clean_temp_files
|
70
|
+
return unless defined?(@hf_tempfiles)
|
71
|
+
@hf_tempfiles.each(&:close!)
|
72
|
+
end
|
68
73
|
|
69
|
-
|
70
|
-
options[:wkhtmltopdf] ||= nil
|
71
|
-
options[:layout] ||= false
|
72
|
-
options[:template] ||= File.join(controller_path, action_name)
|
73
|
-
options[:disposition] ||= 'inline'
|
74
|
-
if options[:show_as_html]
|
74
|
+
def make_pdf(options = {})
|
75
75
|
render_opts = {
|
76
76
|
:template => options[:template],
|
77
77
|
:layout => options[:layout],
|
78
78
|
:formats => options[:formats],
|
79
|
-
:handlers => options[:handlers]
|
80
|
-
:content_type => 'text/html'
|
79
|
+
:handlers => options[:handlers]
|
81
80
|
}
|
82
81
|
render_opts[:locals] = options[:locals] if options[:locals]
|
83
82
|
render_opts[:file] = options[:file] if options[:file]
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
|
83
|
+
html_string = render_to_string(render_opts)
|
84
|
+
options = prerender_header_and_footer(options)
|
85
|
+
w = WickedPdf.new(options[:wkhtmltopdf])
|
86
|
+
w.pdf_from_string(html_string, options)
|
89
87
|
end
|
90
|
-
end
|
91
88
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
89
|
+
def make_and_send_pdf(pdf_name, options = {})
|
90
|
+
options[:wkhtmltopdf] ||= nil
|
91
|
+
options[:layout] ||= false
|
92
|
+
options[:template] ||= File.join(controller_path, action_name)
|
93
|
+
options[:disposition] ||= 'inline'
|
94
|
+
if options[:show_as_html]
|
95
|
+
render_opts = {
|
96
|
+
:template => options[:template],
|
97
|
+
:layout => options[:layout],
|
98
|
+
:formats => options[:formats],
|
99
|
+
:handlers => options[:handlers],
|
100
|
+
:content_type => 'text/html'
|
101
|
+
}
|
102
|
+
render_opts[:locals] = options[:locals] if options[:locals]
|
103
|
+
render_opts[:file] = options[:file] if options[:file]
|
104
|
+
render(render_opts)
|
105
|
+
else
|
106
|
+
pdf_content = make_pdf(options)
|
107
|
+
File.open(options[:save_to_file], 'wb') { |file| file << pdf_content } if options[:save_to_file]
|
108
|
+
send_data(pdf_content, :filename => pdf_name + '.pdf', :type => 'application/pdf', :disposition => options[:disposition]) unless options[:save_only]
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
# Given an options hash, prerenders content for the header and footer sections
|
113
|
+
# to temp files and return a new options hash including the URLs to these files.
|
114
|
+
def prerender_header_and_footer(options)
|
115
|
+
[:header, :footer].each do |hf|
|
116
|
+
next unless options[hf] && options[hf][:html] && options[hf][:html][:template]
|
117
|
+
@hf_tempfiles = [] unless defined?(@hf_tempfiles)
|
118
|
+
@hf_tempfiles.push(tf = WickedPdfTempfile.new("wicked_#{hf}_pdf.html"))
|
119
|
+
options[hf][:html][:layout] ||= options[:layout]
|
120
|
+
render_opts = {
|
121
|
+
:template => options[hf][:html][:template],
|
122
|
+
:layout => options[hf][:html][:layout],
|
123
|
+
:formats => options[hf][:html][:formats],
|
124
|
+
:handlers => options[hf][:html][:handlers]
|
125
|
+
}
|
126
|
+
render_opts[:locals] = options[hf][:html][:locals] if options[hf][:html][:locals]
|
127
|
+
render_opts[:file] = options[hf][:html][:file] if options[:file]
|
128
|
+
tf.write render_to_string(render_opts)
|
129
|
+
tf.flush
|
130
|
+
options[hf][:html][:url] = "file:///#{tf.path}"
|
131
|
+
end
|
132
|
+
options
|
111
133
|
end
|
112
|
-
options
|
113
134
|
end
|
114
135
|
end
|