adzap-wicked_pdf 2.0.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (57) hide show
  1. checksums.yaml +7 -0
  2. data/.github/issue_template.md +15 -0
  3. data/.gitignore +21 -0
  4. data/.rubocop.yml +22 -0
  5. data/.rubocop_todo.yml +63 -0
  6. data/.travis.yml +30 -0
  7. data/CHANGELOG.md +135 -0
  8. data/Gemfile +3 -0
  9. data/LICENSE.txt +22 -0
  10. data/README.md +446 -0
  11. data/Rakefile +31 -0
  12. data/gemfiles/4.2.gemfile +6 -0
  13. data/gemfiles/5.0.gemfile +6 -0
  14. data/gemfiles/5.1.gemfile +6 -0
  15. data/gemfiles/5.2.gemfile +9 -0
  16. data/gemfiles/rails_edge.gemfile +9 -0
  17. data/generators/wicked_pdf/templates/wicked_pdf.rb +21 -0
  18. data/generators/wicked_pdf/wicked_pdf_generator.rb +7 -0
  19. data/init.rb +2 -0
  20. data/lib/generators/wicked_pdf_generator.rb +6 -0
  21. data/lib/wicked_pdf.rb +29 -0
  22. data/lib/wicked_pdf/asset_helper.rb +141 -0
  23. data/lib/wicked_pdf/binary.rb +56 -0
  24. data/lib/wicked_pdf/command.rb +52 -0
  25. data/lib/wicked_pdf/document.rb +47 -0
  26. data/lib/wicked_pdf/middleware.rb +101 -0
  27. data/lib/wicked_pdf/option_parser.rb +220 -0
  28. data/lib/wicked_pdf/pdf_helper.rb +17 -0
  29. data/lib/wicked_pdf/progress.rb +33 -0
  30. data/lib/wicked_pdf/railtie.rb +19 -0
  31. data/lib/wicked_pdf/renderer.rb +121 -0
  32. data/lib/wicked_pdf/tempfile.rb +13 -0
  33. data/lib/wicked_pdf/version.rb +3 -0
  34. data/test/dummy/app/assets/javascripts/application.js +16 -0
  35. data/test/dummy/app/assets/javascripts/wicked.js +1 -0
  36. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  37. data/test/dummy/app/assets/stylesheets/wicked.css +1 -0
  38. data/test/dummy/config/database.yml +3 -0
  39. data/test/dummy/config/routes.rb +5 -0
  40. data/test/dummy/log/.gitignore +1 -0
  41. data/test/dummy/public/favicon.ico +0 -0
  42. data/test/fixtures/database.yml +4 -0
  43. data/test/fixtures/document_with_long_line.html +16 -0
  44. data/test/fixtures/wicked.css +1 -0
  45. data/test/fixtures/wicked.js +1 -0
  46. data/test/functional/pdf_helper_test.rb +61 -0
  47. data/test/functional/wicked_pdf_asset_helper_test.rb +118 -0
  48. data/test/test_helper.rb +33 -0
  49. data/test/unit/wicked_pdf_binary_test.rb +52 -0
  50. data/test/unit/wicked_pdf_command_test.rb +4 -0
  51. data/test/unit/wicked_pdf_document_test.rb +60 -0
  52. data/test/unit/wicked_pdf_option_parser_test.rb +128 -0
  53. data/test/unit/wicked_pdf_renderer_test.rb +43 -0
  54. data/test/unit/wicked_pdf_test.rb +8 -0
  55. data/test/unit/wkhtmltopdf_location_test.rb +50 -0
  56. data/wicked_pdf.gemspec +38 -0
  57. metadata +249 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 56cc9f21233c4aa590afe1bf5577c967a56abeec26c3fccbd302effe839c3421
4
+ data.tar.gz: 9b0d47a24d5c9fe990efc2290fc64606b9177862217107186bea786955f93ac0
5
+ SHA512:
6
+ metadata.gz: ee172642130eedcb6fbfed05656e9f420625e5fcd9be1763dec387b76d4b21051581bf7ba7dd9a4499b6941462a347e08c7bf8b279d100e56378610762d5dc5d
7
+ data.tar.gz: 687d4369110d2c14e8d08fe4bad877fca2fc38182ab5632efb3dd903c341619e66bdc864e3a10f27f96a87f9c1444aa6022e59ead5bec20e176251778a9739fb
@@ -0,0 +1,15 @@
1
+ ## Issue description
2
+
3
+ ## Expected or desired behavior
4
+
5
+ ## System specifications
6
+
7
+ wicked_pdf gem version (output of `cat Gemfile.lock | grep wicked_pdf`):
8
+
9
+ wkhtmltopdf version (output of `wkhtmltopdf --version`):
10
+
11
+ whtmltopdf [provider gem](https://rubygems.org/search?utf8=%E2%9C%93&query=wkhtmltopdf) and version if one is used:
12
+
13
+ platform/distribution and version (e.g. Windows 10 / Ubuntu 16.04 / Heroku cedar):
14
+
15
+
@@ -0,0 +1,21 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
19
+ gemfiles/*.lock
20
+ .DS_Store
21
+ test/dummy/db/
@@ -0,0 +1,22 @@
1
+ inherit_from: .rubocop_todo.yml
2
+
3
+ AllCops:
4
+ TargetRubyVersion: 2.3
5
+ Exclude:
6
+ - 'test/dummy/**/*'
7
+
8
+ Metrics/BlockLength:
9
+ Exclude:
10
+ - 'wicked_pdf.gemspec'
11
+
12
+ Style/HashSyntax:
13
+ EnforcedStyle: no_mixed_keys
14
+
15
+ Style/FrozenStringLiteralComment:
16
+ EnforcedStyle: never
17
+
18
+ Style/SymbolArray:
19
+ EnforcedStyle: brackets
20
+
21
+ Style/SafeNavigation:
22
+ Enabled: false
@@ -0,0 +1,63 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`
2
+ # on 2015-11-08 18:41:57 -0500 using RuboCop version 0.32.1.
3
+ # The point is for the user to remove these configuration records
4
+ # one by one as the offenses are removed from the code base.
5
+ # Note that changes in the inspected code, or installation of new
6
+ # versions of RuboCop, may require this file to be generated again.
7
+
8
+
9
+ # Offense count: 9
10
+ Metrics/AbcSize:
11
+ Max: 45
12
+
13
+ # Offense count: 2
14
+ Metrics/BlockNesting:
15
+ Max: 4
16
+
17
+ # Offense count: 2
18
+ # Configuration parameters: CountComments.
19
+ Metrics/ClassLength:
20
+ Max: 300
21
+
22
+ Metrics/ModuleLength:
23
+ Max: 120
24
+
25
+ # Offense count: 5
26
+ Metrics/CyclomaticComplexity:
27
+ Max: 11
28
+
29
+ # Offense count: 92
30
+ # Configuration parameters: AllowURI, URISchemes.
31
+ Metrics/LineLength:
32
+ Max: 563
33
+
34
+ # Offense count: 14
35
+ # Configuration parameters: CountComments.
36
+ Metrics/MethodLength:
37
+ Max: 30
38
+
39
+ # Offense count: 4
40
+ Metrics/PerceivedComplexity:
41
+ Max: 12
42
+
43
+ # Offense count: 2
44
+ Naming/AccessorMethodName:
45
+ Enabled: false
46
+
47
+ # Offense count: 1
48
+ Style/CaseEquality:
49
+ Enabled: false
50
+
51
+ # Offense count: 1
52
+ Style/ClassVars:
53
+ Enabled: false
54
+
55
+ # Offense count: 15
56
+ Style/Documentation:
57
+ Enabled: false
58
+
59
+ # Offense count: 5
60
+ # Cop supports --auto-correct.
61
+ # Configuration parameters: EnforcedStyle, SupportedStyles, AllowInnerSlashes.
62
+ Style/RegexpLiteral:
63
+ Enabled: false
@@ -0,0 +1,30 @@
1
+ language: ruby
2
+ git:
3
+ depth: 1
4
+ quiet: true
5
+ sudo: required
6
+ before_install:
7
+ - gem uninstall -v '>= 2' -i $(rvm gemdir)@global -ax bundler || true
8
+ - gem install bundler -v '< 2'
9
+ - sudo apt-get update -qq
10
+ bundler_args: --verbose
11
+ before_script:
12
+ - bundle list
13
+ - sudo apt-get install -y wkhtmltopdf
14
+ - "export DISPLAY=:99.0"
15
+ - "sh -e /etc/init.d/xvfb start"
16
+ env: WKHTMLTOPDF_BIN=/usr/bin/wkhtmltopdf
17
+ script:
18
+ - bundle exec rake
19
+
20
+ gemfile:
21
+ - gemfiles/4.2.gemfile
22
+ - gemfiles/5.0.gemfile
23
+ - gemfiles/5.1.gemfile
24
+ - gemfiles/5.2.gemfile
25
+
26
+ rvm:
27
+ - "2.3"
28
+ - "2.4"
29
+ - "2.5"
30
+ - "2.6"
@@ -0,0 +1,135 @@
1
+ # Change Log
2
+ All notable changes to this project will be documented in this file.
3
+ This project adheres to [Semantic Versioning](http://semver.org/).
4
+
5
+ ## [1.2.1] - 2019-03-16
6
+ ### Changed
7
+ - [Fix `SystemStackError` in some setups](https://github.com/mileszs/wicked_pdf/pull/813)
8
+
9
+ ## [1.2.0] - 2019-03-16
10
+ ### Added
11
+ - [Add `raise_on_all_errors: true` option to raise on any error that prints to STDOUT during PDF generation](https://github.com/mileszs/wicked_pdf/pull/751)
12
+ - [Add ability to use the `assigns` option to `render` to assign instance variables to a PDF template](https://github.com/mileszs/wicked_pdf/pull/801)
13
+ - [Add ability to track console progress](https://github.com/mileszs/wicked_pdf/pull/804) with `progress: -> (output) { puts output }`. This is useful to add reporting hooks to show your frontend what page number is being generated.
14
+
15
+ ### Changed
16
+ - [Fix conflict with other gems that hook into `render`](https://github.com/mileszs/wicked_pdf/pull/574) and avoid using `alias_method_chain` where possible
17
+ - [Fix issue using the shell to locate `wkhtmltopdf` in a Bundler environment](https://github.com/mileszs/wicked_pdf/pull/728)
18
+ - [Fix `wkhtmltopdf` path detection when HOME environment variable is unset](https://github.com/mileszs/wicked_pdf/pull/568)
19
+ - [Fix error when the `Rails` constant is defined but not actually using Rails](https://github.com/mileszs/wicked_pdf/pull/613)
20
+ - [Fix compatibility issue with Sprockets 4](https://github.com/mileszs/wicked_pdf/pull/615)
21
+ - [Fix compatibility issue with `Mime::JS` in Rails 5.1+](https://github.com/mileszs/wicked_pdf/pull/627)
22
+ - [Fix deprecation warning by using `after_action` instead of `after_filter` when available](https://github.com/mileszs/wicked_pdf/pull/663)
23
+ - [Provide Rails `base_path` to `find_asset` calls for Sprockets file lookup](https://github.com/mileszs/wicked_pdf/pull/688)
24
+ - Logger changes:
25
+ - [Use `Rails.logger.debug` instead of `p`](https://github.com/mileszs/wicked_pdf/pull/575)
26
+ - [Change logger message to prepend `[wicked_pdf]` instead of nonstandard `****************WICKED****************`](https://github.com/mileszs/wicked_pdf/pull/589)
27
+ - Documentation changes:
28
+ - [Update link to wkhtmltopdf homepage](https://github.com/mileszs/wicked_pdf/pull/582)
29
+ - [Update link to `wkhtmltopdf_binary_gem`](https://github.com/mileszs/wicked_pdf/commit/59e6c5fca3985f2fa2f345089596250df5da2682)
30
+ - [Update documentation for usage with the Asset Pipeline](https://github.com/mileszs/wicked_pdf/commit/690d00157706699a71b7dcd71834759f4d84702f)
31
+ - [Document `default_protocol` option](https://github.com/mileszs/wicked_pdf/pull/585)
32
+ - [Document `image` and `no_image` options](https://github.com/mileszs/wicked_pdf/pull/689)
33
+ - [Document issue with DPI/scaling on various platforms](https://github.com/mileszs/wicked_pdf/pull/715)
34
+ - [Document creating and attaching a PDF in a mailer](https://github.com/mileszs/wicked_pdf/pull/746)
35
+ - [Document dependency on `wkhtmltopdf` with RubyGems](https://github.com/mileszs/wicked_pdf/pull/656)
36
+ - [Add example using WickedPDF with Rails in an API-only configuration](https://github.com/mileszs/wicked_pdf/pull/796)
37
+ - [Add example for rending a template as a header/footer](https://github.com/mileszs/wicked_pdf/pull/603)
38
+ - [Add GitHub issue template](https://github.com/mileszs/wicked_pdf/pull/805)
39
+ - [Add CodeClimate Badge](https://github.com/mileszs/wicked_pdf/pull/646)
40
+ - RuboCop cleanup
41
+ - Updates to Travis CI pipeline to support newer versions of Ruby & Rails
42
+
43
+ ## [1.1.0] - 2016-08-30
44
+ ### Added
45
+ - Support Rails 5.x and Sprockets 3.x
46
+ - 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/)
47
+ - Support `no_stop_slow_scripts: true` to let slow running scripts delay rendering
48
+
49
+ ### Changed
50
+ - [Improved error handling](https://github.com/mileszs/wicked_pdf/pull/543)
51
+ - [Namespace helper classes under WickedPdf namespace](https://github.com/mileszs/wicked_pdf/pull/538)
52
+ - [Changes to asset finding to support Rails 5](https://github.com/mileszs/wicked_pdf/pull/561)
53
+
54
+ ## [1.0.6] - 2016-04-04
55
+ ### Changed
56
+ - 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.
57
+
58
+ ## [1.0.5] - 2016-03-28
59
+ ### Changed
60
+ - Numerous RuboCop style violation fixes, spelling errors, and test-setup issues from [indyrb.org](http://indyrb.org/) hack night. Thank you all for your contributions!
61
+
62
+ ### Fixed
63
+ - Shellescape options. A stray quote in `header` or `footer` would cause PDF to fail to generate, and this should close down many potential attack vectors if you allow user-supplied values to be passed into `wicked_pdf` render options.
64
+
65
+ ## [1.0.4] - 2016-01-26
66
+ ### Changed
67
+ - Check that logger responds to info before calling it. It was possible to have a `logger` method defined as a controller helper that would override `Rails.logger`.
68
+
69
+ ### Fixed
70
+ - [Issue with Sprockets 3.0](https://github.com/mileszs/wicked_pdf/issues/476) where an asset referenced in a stylesheet not existing would raise an exception `read_asset` on nil.
71
+
72
+ ## [1.0.3] - 2015-12-02
73
+ ### Changed
74
+ - Revert default DPI. Some installs of `wkhtmltopdf` would experience major slowdowns or crashes with it set to 72. It is suggested that a DPI of 75 may be better, but I'm holding off on making it a default without more information.
75
+
76
+ ## [1.0.2] - 2015-11-30
77
+ ### Changed
78
+ - The default dpi is now 72. Previously the default would be whatever your `wkhtmltopdf` version specified as the default. This change [speeds up generation of documents that contain `border-radius` dramatically](https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1510)
79
+
80
+ ## [1.0.1] - 2015-11-19
81
+ ### Changed
82
+ - Made minor RuboCop style tweaks.
83
+
84
+ ### Added
85
+ - Added default [RuboCop](https://github.com/bbatsov/rubocop) config and run after test suite.
86
+
87
+ ### Fixed
88
+ - Issue with `nil.basename` from asset helpers.
89
+
90
+ ## [1.0.0] - 2015-11-03
91
+ ### Changed
92
+ - Accepted that `WickedPDF` cannot guarantee backwards compatibility with older versions of `wkthmltopdf`, and decided to publish a new version with the MAJOR number incremented, signaling that this may have breaking changes for some people, but providing a path forward for progress. This release number also signals that this is a mature (and relatively stable) project, and should be deemed ready for production (since it has been used in production since ~2009, and downloaded over a *million* times on [RubyGems.org](https://rubygems.org/gems/wicked_pdf)).
93
+ - Stopped attempting to track with version number of `wkhtmltopdf` binary releases (`wkhtmltopdf` v9.x == `WickedPDF` v9.x)
94
+ - Adopted [Semantic Versioning](http://semver.org/) for release numbering
95
+ - Added a CHANGELOG (based on [keepachangelog.com](http://keepachangelog.com/))
96
+ - Misc code tweaks as suggested by [RuboCop](https://github.com/bbatsov/rubocop)
97
+
98
+ ### Added
99
+ - Check version of `wkhtmltopdf` before deciding to pass arguments with or without dashes
100
+ - New arguments and options for the table of contents supported in newer versions of wkhtmltopf: `text_size_shrink`, `level_indentation`, `disable_dotted_lines`, `disable_toc_links`, `xsl_style_sheet`
101
+ - Merge in global options to `pdf_from_html_file` and `pdf_from_string`
102
+ - Add ability to generate pdf from a web resource: `pdf_from_url(url)`
103
+
104
+ ### Removed
105
+ - Explicit dependency on [Rails](https://github.com/rails/rails), since parts of this library may be used without it.
106
+ - Comment out the `:exe_path` option in the generated initalizer by default (since many systems won't have `wkthmltopdf` installed in that specific location)
107
+
108
+ ### Fixed
109
+ - Issues with `file://` paths on Windows-based systems
110
+ - Issues with parsed options/argument ordering on versions of `wkthmltopdf` > 0.9
111
+ - Issues with middleware headers when running Rails app mounted in a subdirectory
112
+ - Issues with options that have a `key: 'value'` syntax when passed to `wkthmltopdf`
113
+ - Issue with `:temp_path` option being deleted from original options hash
114
+ - Issue with header/footer `:content` being deleted after the first page
115
+ - Issues with options being modified during processing (including global config options)
116
+ - Issues with asset helpers recognizing assets specified without a protocol
117
+ - Issues with `url()` references and embedded `data:base64` assests in stylesheets rendered with `wicked_pdf_stylesheet_link_tag`
118
+ - Asset helpers no longer add a file extension if it already is specified with one
119
+
120
+ # Compare Releases
121
+ - [1.2.1...HEAD](https://github.com/mileszs/wicked_pdf/compare/1.2.1...HEAD)
122
+ - [1.2.0...1.2.1](https://github.com/mileszs/wicked_pdf/compare/1.1.0...1.2.0)
123
+ - [1.1.0...1.2.0](https://github.com/mileszs/wicked_pdf/compare/1.1.0...1.2.0)
124
+ - [1.0.6...1.1.0](https://github.com/mileszs/wicked_pdf/compare/1.0.6...1.1.0)
125
+ - [1.0.5...1.0.6](https://github.com/mileszs/wicked_pdf/compare/1.0.5...1.0.6)
126
+ - [1.0.4...1.0.5](https://github.com/mileszs/wicked_pdf/compare/1.0.4...1.0.5)
127
+ - [1.0.3...1.0.4](https://github.com/mileszs/wicked_pdf/compare/1.0.3...1.0.4)
128
+ - [1.0.2...1.0.3](https://github.com/mileszs/wicked_pdf/compare/1.0.2...1.0.3)
129
+ - [1.0.1...1.0.2](https://github.com/mileszs/wicked_pdf/compare/1.0.1...1.0.2)
130
+ - [1.0.0...1.0.1](https://github.com/mileszs/wicked_pdf/compare/1.0.0...1.0.1)
131
+ - [0.11.0...1.0.0](https://github.com/mileszs/wicked_pdf/compare/0.11.0...1.0.0)
132
+ - [0.10.2...0.11.0](https://github.com/mileszs/wicked_pdf/compare/0.10.2...0.11.0)
133
+ - [0.10.1...0.10.2](https://github.com/mileszs/wicked_pdf/compare/0.10.1...0.10.2)
134
+ - [0.10.0...0.10.1](https://github.com/mileszs/wicked_pdf/compare/0.10.0...0.10.1)
135
+ - [0.9.10...0.10.0](https://github.com/mileszs/wicked_pdf/compare/0.9.10...0.10.0)
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2008 Miles Z. Sterrett
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,446 @@
1
+ # Wicked PDF [![Gem Version](https://badge.fury.io/rb/adzap-wicked_pdf.svg)](http://badge.fury.io/rb/adzap-wicked_pdf) [![Build Status](https://secure.travis-ci.org/adzap/wicked_pdf.svg)](http://travis-ci.org/adzap/wicked_pdf)
2
+
3
+ _This is a fork of the [original](https://github.com/milesz/wicked_pdf) to redesign the internals for a much lighter footprint on your app and offer greater extensibility_
4
+
5
+ ## A PDF generation plugin for Ruby on Rails
6
+
7
+ 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.
8
+
9
+ _Wicked PDF has been verified to work on Ruby versions 2.3 through 2.6; Rails 4.2 through 5.2_
10
+
11
+ ### Installation
12
+
13
+ Add this to your Gemfile and run `bundle install`:
14
+
15
+ ```ruby
16
+ gem 'adzap-wicked_pdf'
17
+ ```
18
+
19
+ Then create the initializer with
20
+
21
+ rails generate wicked_pdf
22
+
23
+ Because `wicked_pdf` is a wrapper for [wkhtmltopdf](http://wkhtmltopdf.org/), you'll need to install that, too.
24
+
25
+ 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)
26
+ To install that gem, add this:
27
+
28
+ ```ruby
29
+ gem 'wkhtmltopdf-binary'
30
+ ```
31
+
32
+ To your Gemfile and run `bundle install`.
33
+
34
+ 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.
35
+
36
+ You can see what flags are supported for the current version in [wkhtmltopdf's auto-generated manual](https://wkhtmltopdf.org/usage/wkhtmltopdf.txt)
37
+
38
+ If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer:
39
+
40
+ ```ruby
41
+ WickedPdf.config = {
42
+ exe_path: '/usr/local/bin/wkhtmltopdf'
43
+ }
44
+ ```
45
+
46
+ For more information about `wkhtmltopdf`, see the project's [homepage](http://wkhtmltopdf.org/).
47
+
48
+ ### Basic Usage
49
+ ```ruby
50
+ class ThingsController < ApplicationController
51
+ def show
52
+ respond_to do |format|
53
+ format.html
54
+ format.pdf do
55
+ render pdf: "file_name" # Excluding ".pdf" extension.
56
+ end
57
+ end
58
+ end
59
+ end
60
+ ```
61
+ ### Usage Conditions - Important!
62
+
63
+ 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.
64
+
65
+ #### wicked_pdf helpers
66
+ ```html
67
+ <!doctype html>
68
+ <html>
69
+ <head>
70
+ <meta charset='utf-8' />
71
+ <%= wicked_pdf_stylesheet_link_tag "pdf" -%>
72
+ <%= wicked_pdf_javascript_include_tag "number_pages" %>
73
+ </head>
74
+ <body onload='number_pages'>
75
+ <div id="header">
76
+ <%= wicked_pdf_image_tag 'mysite.jpg' %>
77
+ </div>
78
+ <div id="content">
79
+ <%= yield %>
80
+ </div>
81
+ </body>
82
+ </html>
83
+ ```
84
+
85
+ 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.
86
+
87
+ ```html
88
+ <!doctype html>
89
+ <html>
90
+ <head>
91
+ <meta charset='utf-8' />
92
+ <%= stylesheet_link_tag wicked_pdf_asset_base64("pdf") %>
93
+ <%= javascript_include_tag wicked_pdf_asset_base64("number_pages") %>
94
+
95
+ </head>
96
+ <body onload='number_pages'>
97
+ <div id="header">
98
+ <%= image_tag wicked_pdf_asset_base64('mysite.jpg') %>
99
+ </div>
100
+ <div id="content">
101
+ <%= yield %>
102
+ </div>
103
+ </body>
104
+ </html>
105
+ ```
106
+
107
+ #### Asset pipeline usage
108
+
109
+ 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.
110
+
111
+ config.assets.precompile += ['blueprint/screen.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js', ...etc...]
112
+
113
+ #### CDN reference
114
+
115
+ 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
+ ```html
117
+ <!doctype html>
118
+ <html>
119
+ <head>
120
+ <%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
121
+ <%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
122
+ ```
123
+
124
+ ### Advanced Usage with all available options
125
+ ```ruby
126
+ class ThingsController < ApplicationController
127
+ def show
128
+ respond_to do |format|
129
+ format.html
130
+ format.pdf do
131
+ render pdf: 'file_name',
132
+ disposition: 'attachment', # default 'inline'
133
+ template: 'things/show',
134
+ file: "#{Rails.root}/files/foo.erb",
135
+ inline: '<!doctype html><html><head></head><body>INLINE HTML</body></html>',
136
+ layout: 'pdf', # for a pdf.pdf.erb file
137
+ wkhtmltopdf: '/usr/local/bin/wkhtmltopdf', # path to binary
138
+ show_as_html: params.key?('debug'), # allow debugging based on url param
139
+ orientation: 'Landscape', # default Portrait
140
+ page_size: 'A4, Letter, ...', # default A4
141
+ page_height: NUMBER,
142
+ page_width: NUMBER,
143
+ save_to_file: Rails.root.join('pdfs', "#{filename}.pdf"),
144
+ save_only: false, # depends on :save_to_file being set first
145
+ default_protocol: 'http',
146
+ proxy: 'TEXT',
147
+ basic_auth: false # when true username & password are automatically sent from session
148
+ username: 'TEXT',
149
+ password: 'TEXT',
150
+ title: 'Alternate Title', # otherwise first page title is used
151
+ cover: 'URL, Pathname, or raw HTML string',
152
+ dpi: 'dpi',
153
+ encoding: 'TEXT',
154
+ user_style_sheet: 'URL',
155
+ cookie: ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
156
+ post: ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
157
+ redirect_delay: NUMBER,
158
+ javascript_delay: NUMBER,
159
+ window_status: 'TEXT', # wait to render until some JS sets window.status to the given string
160
+ image_quality: NUMBER,
161
+ no_pdf_compression: true,
162
+ zoom: FLOAT,
163
+ page_offset: NUMBER,
164
+ book: true,
165
+ default_header: true,
166
+ disable_javascript: false,
167
+ grayscale: true,
168
+ lowquality: true,
169
+ enable_plugins: true,
170
+ disable_internal_links: true,
171
+ disable_external_links: true,
172
+ print_media_type: true,
173
+ disable_smart_shrinking: true,
174
+ use_xserver: true,
175
+ background: false, # background needs to be true to enable background colors to render
176
+ no_background: true,
177
+ viewport_size: 'TEXT', # available only with use_xserver or patched QT
178
+ extra: '', # directly inserted into the command to wkhtmltopdf
179
+ raise_on_all_errors: nil, # raise error for any stderr output. Such as missing media, image assets
180
+ outline: { outline: true,
181
+ outline_depth: LEVEL },
182
+ margin: { top: SIZE, # default 10 (mm)
183
+ bottom: SIZE,
184
+ left: SIZE,
185
+ right: SIZE },
186
+ header: { html: { template: 'users/header', # use :template OR :url
187
+ layout: 'pdf_plain', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout
188
+ url: 'www.example.com',
189
+ locals: { foo: @bar }},
190
+ center: 'TEXT',
191
+ font_name: 'NAME',
192
+ font_size: SIZE,
193
+ left: 'TEXT',
194
+ right: 'TEXT',
195
+ spacing: REAL,
196
+ line: true,
197
+ content: 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
198
+ footer: { html: { template:'shared/footer', # use :template OR :url
199
+ layout: 'pdf_plain.html', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout
200
+ url: 'www.example.com',
201
+ locals: { foo: @bar }},
202
+ center: 'TEXT',
203
+ font_name: 'NAME',
204
+ font_size: SIZE,
205
+ left: 'TEXT',
206
+ right: 'TEXT',
207
+ spacing: REAL,
208
+ line: true,
209
+ content: 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
210
+ toc: { font_name: "NAME",
211
+ depth: LEVEL,
212
+ header_text: "TEXT",
213
+ header_fs: SIZE,
214
+ text_size_shrink: 0.8,
215
+ l1_font_size: SIZE,
216
+ l2_font_size: SIZE,
217
+ l3_font_size: SIZE,
218
+ l4_font_size: SIZE,
219
+ l5_font_size: SIZE,
220
+ l6_font_size: SIZE,
221
+ l7_font_size: SIZE,
222
+ level_indentation: NUM,
223
+ l1_indentation: NUM,
224
+ l2_indentation: NUM,
225
+ l3_indentation: NUM,
226
+ l4_indentation: NUM,
227
+ l5_indentation: NUM,
228
+ l6_indentation: NUM,
229
+ l7_indentation: NUM,
230
+ no_dots: true,
231
+ disable_dotted_lines: true,
232
+ disable_links: true,
233
+ disable_toc_links: true,
234
+ disable_back_links:true,
235
+ xsl_style_sheet: 'file.xsl'}, # optional XSLT stylesheet to use for styling table of contents
236
+ progress: proc { |output| puts output } # proc called when console output changes
237
+ end
238
+ end
239
+ end
240
+ end
241
+ ```
242
+ By default, it will render without a layout (layout: false) and the template for the current controller and action.
243
+
244
+ #### wkhtmltopdf Binary Options
245
+
246
+ 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.
247
+
248
+ Examples of those options are:
249
+
250
+ ```ruby
251
+ print_media_type: true # Passes `--print-media-type`
252
+ no_background: true # Passes `--no-background`
253
+ ```
254
+
255
+ You can see the complete list of options under "Global Options" in wkhtmltopdf usage [docs](http://wkhtmltopdf.org/usage/wkhtmltopdf.txt).
256
+
257
+ ### Super Advanced Usage ###
258
+
259
+ If you need to just create a pdf and not display it:
260
+ ```ruby
261
+ # create a pdf from a string
262
+ pdf = WickedPdf.new.pdf_from_string('<h1>Hello There!</h1>')
263
+
264
+ # create a pdf file from a html file without converting it to string
265
+ # Path must be absolute path
266
+ pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')
267
+
268
+ # create a pdf from a URL
269
+ pdf = WickedPdf.new.pdf_from_url('https://github.com/adzap/wicked_pdf')
270
+
271
+ # create a pdf from string using templates, layouts and content option for header or footer
272
+ pdf = WickedPdf.new.pdf_from_string(
273
+ render_to_string('templates/pdf', layout: 'pdfs/layout_pdf.html'),
274
+ footer: {
275
+ content: render_to_string(
276
+ 'templates/footer',
277
+ layout: 'pdfs/layout_pdf.html'
278
+ )
279
+ }
280
+ )
281
+
282
+ # It is possible to use footer/header templates without a layout, in that case you need to provide a valid HTML document
283
+ pdf = WickedPdf.new.pdf_from_string(
284
+ render_to_string('templates/full_pdf_template'),
285
+ header: {
286
+ content: render_to_string('templates/full_header_template')
287
+ }
288
+ )
289
+
290
+ # or from your controller, using views & templates and all wicked_pdf options as normal
291
+ pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8"
292
+
293
+ # then save to a file
294
+ save_path = Rails.root.join('pdfs','filename.pdf')
295
+ File.open(save_path, 'wb') do |file|
296
+ file << pdf
297
+ end
298
+
299
+ # you can also track progress on your PDF generation, such as when using it from within a Resque job
300
+ class PdfJob
301
+ def perform
302
+ blk = proc { |output|
303
+ match = output.match(/\[.+\] Page (?<current_page>\d+) of (?<total_pages>\d+)/)
304
+ if match
305
+ current_page = match[:current_page].to_i
306
+ total_pages = match[:total_pages].to_i
307
+ message = "Generated #{current_page} of #{total_pages} pages"
308
+ at current_page, total_pages, message
309
+ end
310
+ }
311
+ WickedPdf.new.pdf_from_string(html, progress: blk)
312
+ end
313
+ end
314
+ ```
315
+ If you need to display utf encoded characters, add this to your pdf views or layouts:
316
+ ```html
317
+ <meta charset="utf-8" />
318
+ ```
319
+ If you need to return a PDF in a controller with Rails in API mode:
320
+ ```ruby
321
+ pdf_html = ActionController::Base.new.render_to_string(template: 'controller_name/action_name', layout: 'pdf')
322
+ pdf = WickedPdf.new.pdf_from_string(pdf_html)
323
+ send_data pdf, filename: 'file.pdf'
324
+ ```
325
+ ### Page Breaks
326
+
327
+ You can control page breaks with CSS.
328
+
329
+ Add a few styles like this to your stylesheet or page:
330
+ ```css
331
+ div.alwaysbreak { page-break-before: always; }
332
+ div.nobreak:before { clear:both; }
333
+ div.nobreak { page-break-inside: avoid; }
334
+ ```
335
+
336
+ ### Page Numbering
337
+
338
+ A bit of javascript can help you number your pages. Create a template or header/footer file with this:
339
+ ```html
340
+ <html>
341
+ <head>
342
+ <script>
343
+ function number_pages() {
344
+ var vars={};
345
+ var x=document.location.search.substring(1).split('&');
346
+ for(var i in x) {var z=x[i].split('=',2);vars[z[0]] = decodeURIComponent(z[1]);}
347
+ var x=['frompage','topage','page','webpage','section','subsection','subsubsection'];
348
+ for(var i in x) {
349
+ var y = document.getElementsByClassName(x[i]);
350
+ for(var j=0; j<y.length; ++j) y[j].textContent = vars[x[i]];
351
+ }
352
+ }
353
+ </script>
354
+ </head>
355
+ <body onload="number_pages()">
356
+ Page <span class="page"></span> of <span class="topage"></span>
357
+ </body>
358
+ </html>
359
+ ```
360
+ Anything with a class listed in "var x" above will be auto-filled at render time.
361
+
362
+ If you do not have explicit page breaks (and therefore do not have any "page" class), you can also use wkhtmltopdf's built in page number generation by setting one of the headers to "[page]":
363
+ ```ruby
364
+ render pdf: 'filename', header: { right: '[page] of [topage]' }
365
+ ```
366
+ ### Configuration
367
+
368
+ You can put your default configuration, applied to all pdf's at "wicked_pdf.rb" initializer.
369
+
370
+ ### Rack Middleware
371
+
372
+ If you would like to have WickedPdf automatically generate PDF views for all (or nearly all) pages by appending .pdf to the URL, add the following to your Rails app:
373
+ ```ruby
374
+ # in application.rb (Rails 4+)
375
+ require 'wicked_pdf'
376
+ config.middleware.use WickedPdf::Middleware
377
+ ```
378
+ If you want to turn on or off the middleware for certain URLs, use the `:only` or `:except` conditions like so:
379
+ ```ruby
380
+ # conditions can be plain strings or regular expressions, and you can supply only one or an array
381
+ config.middleware.use WickedPdf::Middleware, {}, only: '/invoice'
382
+ config.middleware.use WickedPdf::Middleware, {}, except: [ %r[^/admin], '/secret', %r[^/people/\d] ]
383
+ ```
384
+ If you use the standard `render pdf: 'some_pdf'` in your app, you will want to exclude those actions from the middleware.
385
+
386
+
387
+ ### Include in an email as an attachment
388
+
389
+ To include a rendered pdf file in an email you can do the following:
390
+
391
+ ```ruby
392
+ attachments['attachment.pdf'] = WickedPdf.new.pdf_from_string(
393
+ render_to_string('link_to_view.pdf.erb', layout: 'pdf')
394
+ )
395
+ ```
396
+
397
+ 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.
398
+
399
+ ### Further Reading
400
+
401
+ Mike Ackerman's post [How To Create PDFs in Rails](https://www.viget.com/articles/how-to-create-pdfs-in-rails)
402
+
403
+ Andreas Happe's post [Generating PDFs from Ruby on Rails](http://www.snikt.net/blog/2012/04/26/wicked-pdf/)
404
+
405
+ JESii's post [WickedPDF, wkhtmltopdf, and Heroku...a tricky combination](http://www.nubyrubyrailstales.com/2013/06/wickedpdf-wkhtmltopdf-and-herokua.html)
406
+
407
+ 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/)
408
+
409
+ StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf)
410
+
411
+ ### Debugging
412
+
413
+ Now you can use a debug param on the URL that shows you the content of the pdf in plain html to design it faster.
414
+
415
+ First of all you must configure the render parameter `show_as_html: params.key?('debug')` and then just use it like you normally would but add "debug" as a GET param in the URL:
416
+
417
+ http://localhost:3001/CONTROLLER/X.pdf?debug
418
+
419
+ However, the wicked_pdf_* helpers will use file:/// paths for assets when using :show_as_html, and your browser's cross-domain safety feature will kick in, and not render them. To get around this, you can load your assets like so in your templates:
420
+ ```html
421
+ <%= params.key?('debug') ? image_tag('foo') : wicked_pdf_image_tag('foo') %>
422
+ ```
423
+
424
+ #### Gotchas
425
+
426
+ 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).
427
+
428
+ 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.
429
+
430
+ ### Inspiration
431
+
432
+ You may have noticed: this plugin is heavily inspired by the PrinceXML plugin [princely](http://github.com/mbleigh/princely/tree/master). PrinceXML's cost was prohibitive for me. So, with a little help from some friends (thanks [jqr](http://github.com/jqr)), I tracked down wkhtmltopdf, and here we are.
433
+
434
+ ### Contributing
435
+
436
+ 1. Fork it
437
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
438
+ 3. Run the test suite and check the output (`rake`)
439
+ 4. Add tests for your feature or fix (please)
440
+ 5. Commit your changes (`git commit -am 'Add some feature'`)
441
+ 6. Push to the branch (`git push origin my-new-feature`)
442
+ 7. Create new Pull Request
443
+
444
+ ### Awesome Peoples
445
+
446
+ Also, thanks to [unixmonkey](https://github.com/Unixmonkey), [galdomedia](http://github.com/galdomedia), [jcrisp](http://github.com/jcrisp), [lleirborras](http://github.com/lleirborras), [tiennou](http://github.com/tiennou), and everyone else for all their hard work and patience with my delays in merging in their enhancements.