wicked_pdf 1.1.0 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/issue_template.md +15 -0
- data/.rubocop.yml +6 -0
- data/.rubocop_todo.yml +160 -30
- data/.travis.yml +56 -34
- data/CHANGELOG.md +98 -40
- data/README.md +84 -19
- data/Rakefile +14 -4
- data/gemfiles/4.0.gemfile +1 -0
- data/gemfiles/4.1.gemfile +1 -0
- data/gemfiles/4.2.gemfile +3 -0
- data/gemfiles/5.0.gemfile +2 -0
- data/gemfiles/5.1.gemfile +8 -0
- data/gemfiles/5.2.gemfile +10 -0
- data/gemfiles/6.0.gemfile +11 -0
- data/gemfiles/rails_edge.gemfile +5 -2
- data/lib/generators/wicked_pdf_generator.rb +5 -9
- data/lib/wicked_pdf/middleware.rb +1 -1
- data/lib/wicked_pdf/pdf_helper.rb +29 -21
- data/lib/wicked_pdf/progress.rb +33 -0
- data/lib/wicked_pdf/railtie.rb +9 -38
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf/wicked_pdf_helper/assets.rb +62 -7
- data/lib/wicked_pdf/wicked_pdf_helper.rb +2 -1
- data/lib/wicked_pdf.rb +48 -48
- data/test/fixtures/database.yml +4 -0
- data/test/fixtures/manifest.js +3 -0
- data/test/functional/pdf_helper_test.rb +71 -0
- data/test/test_helper.rb +11 -8
- data/test/unit/wicked_pdf_test.rb +22 -3
- data/test/unit/wkhtmltopdf_location_test.rb +50 -0
- data/wicked_pdf.gemspec +20 -12
- metadata +53 -28
- data/gemfiles/2.3.gemfile +0 -10
- data/gemfiles/3.0.gemfile +0 -12
- data/gemfiles/3.1.gemfile +0 -13
- data/gemfiles/3.2.gemfile +0 -12
data/README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1
|
-
# Wicked PDF [![Build Status](https://secure.travis-ci.org/mileszs/wicked_pdf.
|
1
|
+
# Wicked PDF [![Gem Version](https://badge.fury.io/rb/wicked_pdf.svg)](http://badge.fury.io/rb/wicked_pdf) [![Build Status](https://secure.travis-ci.org/mileszs/wicked_pdf.svg)](http://travis-ci.org/mileszs/wicked_pdf) [![Code Climate](https://codeclimate.com/github/mileszs/wicked_pdf/badges/gpa.svg)](https://codeclimate.com/github/mileszs/wicked_pdf) [![Open Source Helpers](https://www.codetriage.com/mileszs/wicked_pdf/badges/users.svg)](https://www.codetriage.com/mileszs/wicked_pdf)
|
2
2
|
|
3
3
|
## A PDF generation plugin for Ruby on Rails
|
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
|
7
|
+
_Wicked PDF has been verified to work on Ruby versions 2.2 through 2.6; Rails 4 through 6.1_
|
8
8
|
|
9
9
|
### Installation
|
10
10
|
|
@@ -26,8 +26,8 @@ to `config/initializers/mime_types.rb` in older versions of Rails.
|
|
26
26
|
|
27
27
|
Because `wicked_pdf` is a wrapper for [wkhtmltopdf](http://wkhtmltopdf.org/), you'll need to install that, too.
|
28
28
|
|
29
|
-
The simplest way to install all of the binaries
|
30
|
-
To install that, add
|
29
|
+
The simplest way to install all of the binaries on most Linux or OSX systems is through the gem [wkhtmltopdf-binary](https://github.com/zakird/wkhtmltopdf_binary_gem). Builds for other systems are available [here](https://wkhtmltopdf.org/downloads.html)
|
30
|
+
To install that gem, add this:
|
31
31
|
|
32
32
|
```ruby
|
33
33
|
gem 'wkhtmltopdf-binary'
|
@@ -35,7 +35,9 @@ gem 'wkhtmltopdf-binary'
|
|
35
35
|
|
36
36
|
To your Gemfile and run `bundle install`.
|
37
37
|
|
38
|
-
This
|
38
|
+
This gem currently installs version 0.12.x of `wkhtmltopdf`. Some of the options listed below are specific 0.9 or below, and others are for 0.12 and up.
|
39
|
+
|
40
|
+
You can see what flags are supported for the current version in [wkhtmltopdf's auto-generated manual](https://wkhtmltopdf.org/usage/wkhtmltopdf.txt)
|
39
41
|
|
40
42
|
If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer:
|
41
43
|
|
@@ -83,7 +85,6 @@ The wkhtmltopdf binary is run outside of your Rails application; therefore, your
|
|
83
85
|
</body>
|
84
86
|
</html>
|
85
87
|
```
|
86
|
-
|
87
88
|
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
|
|
89
90
|
```html
|
@@ -106,6 +107,19 @@ Using wicked_pdf_helpers with asset pipeline raises `Asset names passed to helpe
|
|
106
107
|
</html>
|
107
108
|
```
|
108
109
|
|
110
|
+
#### Webpacker usage
|
111
|
+
|
112
|
+
wicked_pdf supports webpack stylesheets and javascript assets.
|
113
|
+
|
114
|
+
Use `wicked_pdf_stylesheet_pack_tag` for stylesheets
|
115
|
+
Use `wicked_pdf_javascript_pack_tag` for javascripts
|
116
|
+
|
117
|
+
#### Asset pipeline usage
|
118
|
+
|
119
|
+
It is best to precompile assets used in PDF views. This will help avoid issues when it comes to deploying, as Rails serves asset files differently between development and production (`config.assets.compile = false`), which can make it look like your PDFs work in development, but fail to load assets in production.
|
120
|
+
|
121
|
+
config.assets.precompile += ['blueprint/screen.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js', ...etc...]
|
122
|
+
|
109
123
|
#### CDN reference
|
110
124
|
|
111
125
|
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.
|
@@ -116,13 +130,11 @@ In this case, you can use that standard Rails helpers and point to the current C
|
|
116
130
|
<%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
|
117
131
|
<%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
|
118
132
|
```
|
119
|
-
#### Asset pipeline usage
|
120
133
|
|
121
|
-
|
134
|
+
### Advanced Usage with all available options
|
122
135
|
|
123
|
-
|
136
|
+
_NOTE: Certain options are only supported in specific versions of wkhtmltopdf._
|
124
137
|
|
125
|
-
### Advanced Usage with all available options
|
126
138
|
```ruby
|
127
139
|
class ThingsController < ApplicationController
|
128
140
|
def show
|
@@ -132,8 +144,9 @@ class ThingsController < ApplicationController
|
|
132
144
|
render pdf: 'file_name',
|
133
145
|
disposition: 'attachment', # default 'inline'
|
134
146
|
template: 'things/show',
|
135
|
-
file: "#{Rails.root}/files/foo.erb"
|
136
|
-
|
147
|
+
file: "#{Rails.root}/files/foo.erb",
|
148
|
+
inline: '<!doctype html><html><head></head><body>INLINE HTML</body></html>',
|
149
|
+
layout: 'pdf', # for a pdf.pdf.erb file
|
137
150
|
wkhtmltopdf: '/usr/local/bin/wkhtmltopdf', # path to binary
|
138
151
|
show_as_html: params.key?('debug'), # allow debugging based on url param
|
139
152
|
orientation: 'Landscape', # default Portrait
|
@@ -142,6 +155,7 @@ class ThingsController < ApplicationController
|
|
142
155
|
page_width: NUMBER,
|
143
156
|
save_to_file: Rails.root.join('pdfs', "#{filename}.pdf"),
|
144
157
|
save_only: false, # depends on :save_to_file being set first
|
158
|
+
default_protocol: 'http',
|
145
159
|
proxy: 'TEXT',
|
146
160
|
basic_auth: false # when true username & password are automatically sent from session
|
147
161
|
username: 'TEXT',
|
@@ -171,10 +185,13 @@ class ThingsController < ApplicationController
|
|
171
185
|
print_media_type: true,
|
172
186
|
disable_smart_shrinking: true,
|
173
187
|
use_xserver: true,
|
174
|
-
background: false, #
|
188
|
+
background: false, # background needs to be true to enable background colors to render
|
175
189
|
no_background: true,
|
176
190
|
viewport_size: 'TEXT', # available only with use_xserver or patched QT
|
177
191
|
extra: '', # directly inserted into the command to wkhtmltopdf
|
192
|
+
raise_on_all_errors: nil, # raise error for any stderr output. Such as missing media, image assets
|
193
|
+
log_level: 'info', # Available values: none, error, warn, or info - only available with wkhtmltopdf 0.12.5+
|
194
|
+
quiet: false, # `false` is same as `log_level: 'info'`, `true` is same as `log_level: 'none'`
|
178
195
|
outline: { outline: true,
|
179
196
|
outline_depth: LEVEL },
|
180
197
|
margin: { top: SIZE, # default 10 (mm)
|
@@ -230,7 +247,8 @@ class ThingsController < ApplicationController
|
|
230
247
|
disable_links: true,
|
231
248
|
disable_toc_links: true,
|
232
249
|
disable_back_links:true,
|
233
|
-
xsl_style_sheet: 'file.xsl'} # optional XSLT stylesheet to use for styling table of contents
|
250
|
+
xsl_style_sheet: 'file.xsl'}, # optional XSLT stylesheet to use for styling table of contents
|
251
|
+
progress: proc { |output| puts output } # proc called when console output changes
|
234
252
|
end
|
235
253
|
end
|
236
254
|
end
|
@@ -267,9 +285,20 @@ pdf = WickedPdf.new.pdf_from_url('https://github.com/mileszs/wicked_pdf')
|
|
267
285
|
|
268
286
|
# create a pdf from string using templates, layouts and content option for header or footer
|
269
287
|
pdf = WickedPdf.new.pdf_from_string(
|
270
|
-
render_to_string('templates/pdf', layout: 'pdfs/layout_pdf'),
|
288
|
+
render_to_string('templates/pdf', layout: 'pdfs/layout_pdf.html'),
|
271
289
|
footer: {
|
272
|
-
content: render_to_string(
|
290
|
+
content: render_to_string(
|
291
|
+
'templates/footer',
|
292
|
+
layout: 'pdfs/layout_pdf.html'
|
293
|
+
)
|
294
|
+
}
|
295
|
+
)
|
296
|
+
|
297
|
+
# It is possible to use footer/header templates without a layout, in that case you need to provide a valid HTML document
|
298
|
+
pdf = WickedPdf.new.pdf_from_string(
|
299
|
+
render_to_string('templates/full_pdf_template'),
|
300
|
+
header: {
|
301
|
+
content: render_to_string('templates/full_header_template')
|
273
302
|
}
|
274
303
|
)
|
275
304
|
|
@@ -281,12 +310,33 @@ save_path = Rails.root.join('pdfs','filename.pdf')
|
|
281
310
|
File.open(save_path, 'wb') do |file|
|
282
311
|
file << pdf
|
283
312
|
end
|
313
|
+
|
314
|
+
# you can also track progress on your PDF generation, such as when using it from within a Resque job
|
315
|
+
class PdfJob
|
316
|
+
def perform
|
317
|
+
blk = proc { |output|
|
318
|
+
match = output.match(/\[.+\] Page (?<current_page>\d+) of (?<total_pages>\d+)/)
|
319
|
+
if match
|
320
|
+
current_page = match[:current_page].to_i
|
321
|
+
total_pages = match[:total_pages].to_i
|
322
|
+
message = "Generated #{current_page} of #{total_pages} pages"
|
323
|
+
at current_page, total_pages, message
|
324
|
+
end
|
325
|
+
}
|
326
|
+
WickedPdf.new.pdf_from_string(html, progress: blk)
|
327
|
+
end
|
328
|
+
end
|
284
329
|
```
|
285
330
|
If you need to display utf encoded characters, add this to your pdf views or layouts:
|
286
331
|
```html
|
287
332
|
<meta charset="utf-8" />
|
288
333
|
```
|
289
|
-
|
334
|
+
If you need to return a PDF in a controller with Rails in API mode:
|
335
|
+
```ruby
|
336
|
+
pdf_html = ActionController::Base.new.render_to_string(template: 'controller_name/action_name', layout: 'pdf')
|
337
|
+
pdf = WickedPdf.new.pdf_from_string(pdf_html)
|
338
|
+
send_data pdf, filename: 'file.pdf'
|
339
|
+
```
|
290
340
|
### Page Breaks
|
291
341
|
|
292
342
|
You can control page breaks with CSS.
|
@@ -340,7 +390,7 @@ If you would like to have WickedPdf automatically generate PDF views for all (or
|
|
340
390
|
require 'wicked_pdf'
|
341
391
|
config.middleware.use WickedPdf::Middleware
|
342
392
|
```
|
343
|
-
If you want to turn on or off the middleware for certain
|
393
|
+
If you want to turn on or off the middleware for certain URLs, use the `:only` or `:except` conditions like so:
|
344
394
|
```ruby
|
345
395
|
# conditions can be plain strings or regular expressions, and you can supply only one or an array
|
346
396
|
config.middleware.use WickedPdf::Middleware, {}, only: '/invoice'
|
@@ -348,6 +398,19 @@ config.middleware.use WickedPdf::Middleware, {}, except: [ %r[^/admin], '/secret
|
|
348
398
|
```
|
349
399
|
If you use the standard `render pdf: 'some_pdf'` in your app, you will want to exclude those actions from the middleware.
|
350
400
|
|
401
|
+
|
402
|
+
### Include in an email as an attachment
|
403
|
+
|
404
|
+
To include a rendered pdf file in an email you can do the following:
|
405
|
+
|
406
|
+
```ruby
|
407
|
+
attachments['attachment.pdf'] = WickedPdf.new.pdf_from_string(
|
408
|
+
render_to_string('link_to_view.pdf.erb', layout: 'pdf')
|
409
|
+
)
|
410
|
+
```
|
411
|
+
|
412
|
+
This will render the pdf to a string and include it in the email. This is very slow so make sure you schedule your email delivery in a job.
|
413
|
+
|
351
414
|
### Further Reading
|
352
415
|
|
353
416
|
Mike Ackerman's post [How To Create PDFs in Rails](https://www.viget.com/articles/how-to-create-pdfs-in-rails)
|
@@ -375,7 +438,9 @@ However, the wicked_pdf_* helpers will use file:/// paths for assets when using
|
|
375
438
|
|
376
439
|
#### Gotchas
|
377
440
|
|
378
|
-
If one image from your HTML cannot be found (relative or wrong path for
|
441
|
+
If one image from your HTML cannot be found (relative or wrong path for example), others images with right paths **may not** be displayed in the output PDF as well (it seems to be an issue with wkhtmltopdf).
|
442
|
+
|
443
|
+
wkhtmltopdf may render at different resolutions on different platforms. For example, Linux prints at 75 dpi (native for WebKit) while on Windows it's at the desktop's DPI (which is normally 96 dpi). [Use `:zoom => 0.78125`](https://github.com/wkhtmltopdf/wkhtmltopdf/issues/2184) (75/96) to match Linux rendering to Windows.
|
379
444
|
|
380
445
|
### Inspiration
|
381
446
|
|
data/Rakefile
CHANGED
@@ -34,18 +34,28 @@ 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
|
-
|
37
|
+
if Rails::VERSION::MAJOR > 2
|
38
|
+
system('rails new test/dummy --database=sqlite3')
|
39
|
+
else
|
40
|
+
system('rails test/dummy')
|
41
|
+
end
|
39
42
|
system('touch test/dummy/db/schema.rb')
|
43
|
+
FileUtils.cp 'test/fixtures/database.yml', 'test/dummy/config/'
|
40
44
|
FileUtils.rm_r Dir.glob('test/dummy/test/*')
|
45
|
+
|
46
|
+
# rails 6 needs this to be present before start:
|
47
|
+
FileUtils.mkdir_p('test/dummy/app/assets/config')
|
48
|
+
FileUtils.mkdir_p('test/dummy/app/assets/javascripts')
|
49
|
+
FileUtils.cp 'test/fixtures/manifest.js', 'test/dummy/app/assets/config/'
|
50
|
+
FileUtils.cp 'test/fixtures/wicked.js', 'test/dummy/app/assets/javascripts/'
|
41
51
|
end
|
42
52
|
|
43
53
|
desc 'Remove dummy application'
|
44
54
|
task :dummy_remove do
|
45
|
-
FileUtils.rm_r Dir.glob('test/dummy
|
55
|
+
FileUtils.rm_r Dir.glob('test/dummy/')
|
46
56
|
end
|
47
57
|
|
48
|
-
desc 'Generate documentation for the wicked_pdf
|
58
|
+
desc 'Generate documentation for the wicked_pdf gem.'
|
49
59
|
RDoc::Task.new(:rdoc) do |rdoc|
|
50
60
|
rdoc.rdoc_dir = 'rdoc'
|
51
61
|
rdoc.title = 'WickedPdf'
|
data/gemfiles/4.0.gemfile
CHANGED
data/gemfiles/4.1.gemfile
CHANGED
data/gemfiles/4.2.gemfile
CHANGED
data/gemfiles/5.0.gemfile
CHANGED
@@ -0,0 +1,10 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'rdoc'
|
4
|
+
gem 'rails', '~> 5.2'
|
5
|
+
gem 'sqlite3', '~> 1.3.6'
|
6
|
+
gem 'sprockets', '~>3.0' # v4 strips newlines from assets causing tests to fail
|
7
|
+
gem 'bootsnap' # required to run `rake test` in Rails 5.2
|
8
|
+
gem 'mocha', '= 1.3' # newer versions blow up
|
9
|
+
|
10
|
+
gemspec path: '../'
|
@@ -0,0 +1,11 @@
|
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
gem 'bundler', '~>2'
|
4
|
+
gem 'rdoc'
|
5
|
+
gem 'rails', '~>6.0.1'
|
6
|
+
gem 'sqlite3', '~> 1.4'
|
7
|
+
gem 'sprockets', '~>3.0'
|
8
|
+
gem 'bootsnap' # required to run `rake test` in Rails 6.0
|
9
|
+
gem 'mocha', '= 1.3' # newer versions blow up
|
10
|
+
|
11
|
+
gemspec path: '../'
|
data/gemfiles/rails_edge.gemfile
CHANGED
@@ -1,6 +1,9 @@
|
|
1
|
-
source
|
1
|
+
source 'https://rubygems.org'
|
2
2
|
|
3
3
|
gem 'rdoc'
|
4
|
-
gem 'rails', git:
|
4
|
+
gem 'rails', git: 'https://github.com/rails/rails.git'
|
5
|
+
gem 'sqlite3', '~> 1.4'
|
6
|
+
gem 'bootsnap' # required to run `rake test` in Rails 6.0
|
7
|
+
gem 'mocha', '= 1.3' # newer versions blow up
|
5
8
|
|
6
9
|
gemspec path: '../'
|
@@ -1,11 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
def copy_initializer
|
7
|
-
copy_file 'wicked_pdf.rb', 'config/initializers/wicked_pdf.rb'
|
8
|
-
end
|
1
|
+
# Rails generator invoked with 'rails generate wicked_pdf'
|
2
|
+
class WickedPdfGenerator < Rails::Generators::Base
|
3
|
+
source_root(File.expand_path(File.dirname(__FILE__) + '/../../generators/wicked_pdf/templates'))
|
4
|
+
def copy_initializer
|
5
|
+
copy_file 'wicked_pdf.rb', 'config/initializers/wicked_pdf.rb'
|
9
6
|
end
|
10
|
-
|
11
7
|
end
|
@@ -80,7 +80,7 @@ class WickedPdf
|
|
80
80
|
|
81
81
|
def set_request_to_render_as_pdf(env)
|
82
82
|
@render_pdf = true
|
83
|
-
%w
|
83
|
+
%w[PATH_INFO REQUEST_URI].each { |e| env[e] = env[e].sub(%r{\.pdf\b}, '') }
|
84
84
|
env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
|
85
85
|
env['Rack-Middleware-WickedPdf'] = 'true'
|
86
86
|
end
|
@@ -8,7 +8,11 @@ class WickedPdf
|
|
8
8
|
base.class_eval do
|
9
9
|
alias_method_chain :render, :wicked_pdf
|
10
10
|
alias_method_chain :render_to_string, :wicked_pdf
|
11
|
-
|
11
|
+
if respond_to?(:after_action)
|
12
|
+
after_action :clean_temp_files
|
13
|
+
else
|
14
|
+
after_filter :clean_temp_files
|
15
|
+
end
|
12
16
|
end
|
13
17
|
end
|
14
18
|
|
@@ -19,47 +23,46 @@ class WickedPdf
|
|
19
23
|
|
20
24
|
base.class_eval do
|
21
25
|
after_action :clean_temp_files
|
26
|
+
end
|
27
|
+
end
|
22
28
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
def render(options = nil, *args, &block)
|
27
|
-
render_with_wicked_pdf(options, *args, &block)
|
28
|
-
end
|
29
|
+
def render(options = nil, *args, &block)
|
30
|
+
render_with_wicked_pdf(options, *args, &block)
|
31
|
+
end
|
29
32
|
|
30
|
-
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
33
|
+
def render_to_string(options = nil, *args, &block)
|
34
|
+
render_to_string_with_wicked_pdf(options, *args, &block)
|
34
35
|
end
|
35
36
|
|
36
37
|
def render_with_wicked_pdf(options = nil, *args, &block)
|
37
38
|
if options.is_a?(Hash) && options.key?(:pdf)
|
38
|
-
log_pdf_creation
|
39
39
|
options[:basic_auth] = set_basic_auth(options)
|
40
40
|
make_and_send_pdf(options.delete(:pdf), (WickedPdf.config || {}).merge(options))
|
41
|
-
|
41
|
+
elsif respond_to?(:render_without_wicked_pdf)
|
42
|
+
# support alias_method_chain (module included)
|
42
43
|
render_without_wicked_pdf(options, *args, &block)
|
44
|
+
else
|
45
|
+
# support inheritance (module prepended)
|
46
|
+
method(:render).super_method.call(options, *args, &block)
|
43
47
|
end
|
44
48
|
end
|
45
49
|
|
46
50
|
def render_to_string_with_wicked_pdf(options = nil, *args, &block)
|
47
51
|
if options.is_a?(Hash) && options.key?(:pdf)
|
48
|
-
log_pdf_creation
|
49
52
|
options[:basic_auth] = set_basic_auth(options)
|
50
53
|
options.delete :pdf
|
51
54
|
make_pdf((WickedPdf.config || {}).merge(options))
|
52
|
-
|
55
|
+
elsif respond_to?(:render_to_string_without_wicked_pdf)
|
56
|
+
# support alias_method_chain (module included)
|
53
57
|
render_to_string_without_wicked_pdf(options, *args, &block)
|
58
|
+
else
|
59
|
+
# support inheritance (module prepended)
|
60
|
+
method(:render_to_string).super_method.call(options, *args, &block)
|
54
61
|
end
|
55
62
|
end
|
56
63
|
|
57
64
|
private
|
58
65
|
|
59
|
-
def log_pdf_creation
|
60
|
-
logger.info '*' * 15 + 'WICKED' + '*' * 15 if logger && logger.respond_to?(:info)
|
61
|
-
end
|
62
|
-
|
63
66
|
def set_basic_auth(options = {})
|
64
67
|
options[:basic_auth] ||= WickedPdf.config.fetch(:basic_auth) { false }
|
65
68
|
return unless options[:basic_auth] && request.env['HTTP_AUTHORIZATION']
|
@@ -76,8 +79,10 @@ class WickedPdf
|
|
76
79
|
:template => options[:template],
|
77
80
|
:layout => options[:layout],
|
78
81
|
:formats => options[:formats],
|
79
|
-
:handlers => options[:handlers]
|
82
|
+
:handlers => options[:handlers],
|
83
|
+
:assigns => options[:assigns]
|
80
84
|
}
|
85
|
+
render_opts[:inline] = options[:inline] if options[:inline]
|
81
86
|
render_opts[:locals] = options[:locals] if options[:locals]
|
82
87
|
render_opts[:file] = options[:file] if options[:file]
|
83
88
|
html_string = render_to_string(render_opts)
|
@@ -97,8 +102,10 @@ class WickedPdf
|
|
97
102
|
:layout => options[:layout],
|
98
103
|
:formats => options[:formats],
|
99
104
|
:handlers => options[:handlers],
|
105
|
+
:assigns => options[:assigns],
|
100
106
|
:content_type => 'text/html'
|
101
107
|
}
|
108
|
+
render_opts[:inline] = options[:inline] if options[:inline]
|
102
109
|
render_opts[:locals] = options[:locals] if options[:locals]
|
103
110
|
render_opts[:file] = options[:file] if options[:file]
|
104
111
|
render(render_opts)
|
@@ -121,7 +128,8 @@ class WickedPdf
|
|
121
128
|
:template => options[hf][:html][:template],
|
122
129
|
:layout => options[hf][:html][:layout],
|
123
130
|
:formats => options[hf][:html][:formats],
|
124
|
-
:handlers => options[hf][:html][:handlers]
|
131
|
+
:handlers => options[hf][:html][:handlers],
|
132
|
+
:assigns => options[hf][:html][:assigns]
|
125
133
|
}
|
126
134
|
render_opts[:locals] = options[hf][:html][:locals] if options[hf][:html][:locals]
|
127
135
|
render_opts[:file] = options[hf][:html][:file] if options[:file]
|
@@ -0,0 +1,33 @@
|
|
1
|
+
class WickedPdf
|
2
|
+
module Progress
|
3
|
+
require 'pty' if RbConfig::CONFIG['target_os'] !~ /mswin|mingw/ # no support for windows
|
4
|
+
require 'English'
|
5
|
+
|
6
|
+
def track_progress?(options)
|
7
|
+
options[:progress] && !on_windows?
|
8
|
+
end
|
9
|
+
|
10
|
+
def invoke_with_progress(command, options)
|
11
|
+
output = []
|
12
|
+
begin
|
13
|
+
PTY.spawn(command.join(' ')) do |stdout, _stdin, pid|
|
14
|
+
begin
|
15
|
+
stdout.sync
|
16
|
+
stdout.each_line("\r") do |line|
|
17
|
+
output << line.chomp
|
18
|
+
options[:progress].call(line) if options[:progress]
|
19
|
+
end
|
20
|
+
rescue Errno::EIO # rubocop:disable Lint/HandleExceptions
|
21
|
+
# child process is terminated, this is expected behaviour
|
22
|
+
ensure
|
23
|
+
::Process.wait pid
|
24
|
+
end
|
25
|
+
end
|
26
|
+
rescue PTY::ChildExited
|
27
|
+
puts 'The child process exited!'
|
28
|
+
end
|
29
|
+
err = output.join('\n')
|
30
|
+
raise "#{command} failed (exitstatus 0). Output was: #{err}" unless $CHILD_STATUS && $CHILD_STATUS.exitstatus.zero?
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/wicked_pdf/railtie.rb
CHANGED
@@ -3,48 +3,19 @@ require 'wicked_pdf/wicked_pdf_helper'
|
|
3
3
|
require 'wicked_pdf/wicked_pdf_helper/assets'
|
4
4
|
|
5
5
|
class WickedPdf
|
6
|
-
if defined?(Rails)
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
ActionView::Base.send :include, WickedPdfHelper::Assets
|
14
|
-
end
|
15
|
-
end
|
16
|
-
|
17
|
-
elsif Rails::VERSION::MAJOR == 4
|
18
|
-
|
19
|
-
class WickedRailtie < Rails::Railtie
|
20
|
-
initializer 'wicked_pdf.register' do |_app|
|
21
|
-
ActionController::Base.send :include, PdfHelper
|
22
|
-
ActionView::Base.send :include, WickedPdfHelper::Assets
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
elsif Rails::VERSION::MAJOR == 3
|
27
|
-
|
28
|
-
class WickedRailtie < Rails::Railtie
|
29
|
-
initializer 'wicked_pdf.register' do |_app|
|
30
|
-
ActionController::Base.send :include, PdfHelper
|
31
|
-
if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
|
32
|
-
ActionView::Base.send :include, WickedPdfHelper::Assets
|
6
|
+
if defined?(Rails.env)
|
7
|
+
class WickedRailtie < Rails::Railtie
|
8
|
+
initializer 'wicked_pdf.register', :after => 'remotipart.controller_helper' do |_app|
|
9
|
+
ActiveSupport.on_load(:action_controller) do
|
10
|
+
if ActionController::Base.respond_to?(:prepend) &&
|
11
|
+
Object.method(:new).respond_to?(:super_method)
|
12
|
+
ActionController::Base.send :prepend, PdfHelper
|
33
13
|
else
|
34
|
-
|
14
|
+
ActionController::Base.send :include, PdfHelper
|
35
15
|
end
|
16
|
+
ActionView::Base.send :include, WickedPdfHelper::Assets
|
36
17
|
end
|
37
18
|
end
|
38
|
-
|
39
|
-
elsif Rails::VERSION::MAJOR == 2
|
40
|
-
|
41
|
-
unless ActionController::Base.instance_methods.include? 'render_with_wicked_pdf'
|
42
|
-
ActionController::Base.send :include, PdfHelper
|
43
|
-
end
|
44
|
-
unless ActionView::Base.instance_methods.include? 'wicked_pdf_stylesheet_link_tag'
|
45
|
-
ActionView::Base.send :include, WickedPdfHelper
|
46
|
-
end
|
47
|
-
|
48
19
|
end
|
49
20
|
|
50
21
|
if Mime::Type.lookup_by_extension(:pdf).nil?
|
data/lib/wicked_pdf/version.rb
CHANGED