├── .github
├── issue_template.md
└── workflows
│ └── ci.yml
├── .gitignore
├── .rubocop.yml
├── .rubocop_todo.yml
├── CHANGELOG.md
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── gemfiles
├── 5.0.gemfile
├── 5.1.gemfile
├── 5.2.gemfile
├── 6.0.gemfile
├── 6.1.gemfile
└── 7.0.gemfile
├── generators
└── wicked_pdf
│ ├── templates
│ └── wicked_pdf.rb
│ └── wicked_pdf_generator.rb
├── init.rb
├── lib
├── generators
│ └── wicked_pdf_generator.rb
├── wicked_pdf.rb
└── wicked_pdf
│ ├── binary.rb
│ ├── middleware.rb
│ ├── option_parser.rb
│ ├── pdf_helper.rb
│ ├── progress.rb
│ ├── railtie.rb
│ ├── tempfile.rb
│ ├── version.rb
│ ├── wicked_pdf_helper.rb
│ └── wicked_pdf_helper
│ └── assets.rb
├── test
├── fixtures
│ ├── database.yml
│ ├── document_with_long_line.html
│ ├── manifest.js
│ ├── subdirectory
│ │ └── nested.js
│ ├── wicked.css
│ └── wicked.js
├── functional
│ ├── pdf_helper_test.rb
│ ├── wicked_pdf_helper_assets_test.rb
│ └── wicked_pdf_helper_test.rb
├── test_helper.rb
└── unit
│ ├── wicked_pdf_binary_test.rb
│ ├── wicked_pdf_option_parser_test.rb
│ ├── wicked_pdf_test.rb
│ └── wkhtmltopdf_location_test.rb
└── wicked_pdf.gemspec
/.github/issue_template.md:
--------------------------------------------------------------------------------
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 |
16 |
--------------------------------------------------------------------------------
/.github/workflows/ci.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 |
3 | on: [push, pull_request, workflow_dispatch]
4 |
5 | jobs:
6 | tests:
7 | name: Tests with Ruby ${{ matrix.ruby-version }} and Rails ${{ matrix.gemfile }}
8 | runs-on: ubuntu-latest
9 | strategy:
10 | matrix:
11 | gemfile: ["5.0"]
12 | ruby-version: [2.6]
13 | include:
14 | - gemfile: "5.0"
15 | ruby-version: 2.7
16 | - gemfile: "5.1"
17 | ruby-version: 2.6
18 | - gemfile: "5.1"
19 | ruby-version: 2.7
20 | - gemfile: "5.2"
21 | ruby-version: 2.6
22 | - gemfile: "5.2"
23 | ruby-version: 2.7
24 | - gemfile: "6.0"
25 | ruby-version: 2.6
26 | - gemfile: "6.0"
27 | ruby-version: 2.7
28 | - gemfile: "6.1"
29 | ruby-version: 2.7
30 | - gemfile: "6.1"
31 | ruby-version: '3.0'
32 | - gemfile: "7.0"
33 | ruby-version: 3.1
34 | - gemfile: "7.0"
35 | ruby-version: 3.2
36 |
37 | env:
38 | BUNDLE_GEMFILE: ${{ github.workspace }}/gemfiles/${{ matrix.gemfile }}.gemfile
39 | WKHTMLTOPDF_BIN: /usr/bin/wkhtmltopdf
40 |
41 | steps:
42 | - uses: actions/checkout@v3
43 |
44 | - name: Install OS dependencies
45 | run: |
46 | sudo apt-get update -y -qq
47 | sudo apt-get install -y wkhtmltopdf
48 |
49 | - name: Install Ruby ${{ matrix.ruby-version }}
50 | uses: ruby/setup-ruby@v1
51 | with:
52 | ruby-version: ${{ matrix.ruby-version }}
53 | bundler-cache: true
54 |
55 | - name: Run tests with Ruby ${{ matrix.ruby-version }} and Rails ${{ matrix.gemfile }}
56 | run: bundle exec rake
57 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
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 | test/dummy/**/*
20 | gemfiles/*.lock
21 | .DS_Store
--------------------------------------------------------------------------------
/.rubocop.yml:
--------------------------------------------------------------------------------
1 | inherit_from: .rubocop_todo.yml
2 |
3 | AllCops:
4 | NewCops: disable
5 | SuggestExtensions: false
6 | TargetRubyVersion: 2.6
7 | Exclude:
8 | - 'gemfiles/bin/*'
9 | - 'test/dummy/**/*'
10 | - 'vendor/**/*'
11 |
12 | Metrics/PerceivedComplexity:
13 | Enabled: false
14 |
15 | Gemspec/RequiredRubyVersion:
16 | Enabled: false
17 |
18 | Bundler/OrderedGems:
19 | Enabled: false
20 |
21 | Style/FrozenStringLiteralComment:
22 | Enabled: false
23 |
24 | Style/RedundantBegin:
25 | Enabled: false
26 |
27 | Style/NumericPredicate:
28 | Enabled: false
29 |
30 | Style/RedundantRegexpEscape:
31 | Enabled: false
32 |
33 | Style/SafeNavigation:
34 | Enabled: false
35 |
36 | Lint/SendWithMixinArgument:
37 | Enabled: false
38 |
39 | Lint/RedundantCopDisableDirective:
40 | Enabled: false
41 |
42 | Metrics/AbcSize:
43 | Enabled: false
44 |
45 | Style/StringConcatenation:
46 | Enabled: false
47 |
48 | Style/RedundantFetchBlock:
49 | Enabled: false
50 |
51 | Style/CaseLikeIf:
52 | Enabled: false
53 |
54 | Style/SoleNestedConditional:
55 | Enabled: false
56 |
57 | Style/RedundantReturn:
58 | Enabled: false
59 |
60 | Metrics/BlockLength:
61 | Exclude:
62 | - 'wicked_pdf.gemspec'
63 |
64 | Metrics/ModuleLength:
65 | Exclude:
66 | # Excluding to keep the logic in one module for the time being.
67 | - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
68 |
69 | # I'd like wicked_pdf to keep Ruby 1.8 compatibility for now
70 | Style/HashSyntax:
71 | EnforcedStyle: hash_rockets
72 |
--------------------------------------------------------------------------------
/.rubocop_todo.yml:
--------------------------------------------------------------------------------
1 | # This configuration was generated by
2 | # `rubocop --auto-gen-config`
3 | # on 2023-01-24 11:24:49 UTC using RuboCop version 1.44.0.
4 | # The point is for the user to remove these configuration records
5 | # one by one as the offenses are removed from the code base.
6 | # Note that changes in the inspected code, or installation of new
7 | # versions of RuboCop, may require this file to be generated again.
8 |
9 | # Offense count: 2
10 | # Configuration parameters: CountComments, CountAsOne.
11 | Metrics/ClassLength:
12 | Max: 203
13 |
14 | # Offense count: 7
15 | # Configuration parameters: AllowedMethods, AllowedPatterns, IgnoredMethods.
16 | Metrics/CyclomaticComplexity:
17 | Max: 13
18 |
19 | # Offense count: 17
20 | # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, AllowedMethods, AllowedPatterns, IgnoredMethods.
21 | Metrics/MethodLength:
22 | Max: 34
23 |
24 | # Offense count: 1
25 | # Configuration parameters: CountComments, CountAsOne.
26 | Metrics/ModuleLength:
27 | Max: 104
28 |
29 | # Offense count: 1
30 | Naming/AccessorMethodName:
31 | Exclude:
32 | - 'lib/wicked_pdf/middleware.rb'
33 |
34 | # Offense count: 1
35 | # This cop supports safe autocorrection (--autocorrect).
36 | # Configuration parameters: AllowOnConstant, AllowOnSelfClass.
37 | Style/CaseEquality:
38 | Exclude:
39 | - 'lib/wicked_pdf/wicked_pdf_helper.rb'
40 |
41 | # Offense count: 1
42 | Style/ClassVars:
43 | Exclude:
44 | - 'lib/wicked_pdf.rb'
45 |
46 | # Offense count: 13
47 | # Configuration parameters: AllowedConstants.
48 | Style/Documentation:
49 | Exclude:
50 | - 'spec/**/*'
51 | - 'test/**/*'
52 | - 'generators/wicked_pdf/wicked_pdf_generator.rb'
53 | - 'lib/wicked_pdf.rb'
54 | - 'lib/wicked_pdf/binary.rb'
55 | - 'lib/wicked_pdf/middleware.rb'
56 | - 'lib/wicked_pdf/option_parser.rb'
57 | - 'lib/wicked_pdf/pdf_helper.rb'
58 | - 'lib/wicked_pdf/progress.rb'
59 | - 'lib/wicked_pdf/railtie.rb'
60 | - 'lib/wicked_pdf/tempfile.rb'
61 | - 'lib/wicked_pdf/wicked_pdf_helper.rb'
62 | - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
63 |
64 | # Offense count: 2
65 | # This cop supports safe autocorrection (--autocorrect).
66 | Style/ExpandPathArguments:
67 | Exclude:
68 | - 'test/test_helper.rb'
69 | - 'wicked_pdf.gemspec'
70 |
71 | # Offense count: 4
72 | # This cop supports safe autocorrection (--autocorrect).
73 | # Configuration parameters: EnforcedStyle, EnforcedShorthandSyntax, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols.
74 | # SupportedStyles: ruby19, hash_rockets, no_mixed_keys, ruby19_no_mixed_keys
75 | # SupportedShorthandSyntax: always, never, either, consistent
76 | Style/HashSyntax:
77 | Exclude:
78 | - 'gemfiles/5.0.gemfile'
79 | - 'gemfiles/5.1.gemfile'
80 | - 'gemfiles/5.2.gemfile'
81 | - 'gemfiles/6.0.gemfile'
82 |
83 | # Offense count: 2
84 | # This cop supports unsafe autocorrection (--autocorrect-all).
85 | # Configuration parameters: EnforcedStyle.
86 | # SupportedStyles: literals, strict
87 | Style/MutableConstant:
88 | Exclude:
89 | - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
90 |
91 | # Offense count: 5
92 | # This cop supports safe autocorrection (--autocorrect).
93 | # Configuration parameters: EnforcedStyle, AllowInnerSlashes.
94 | # SupportedStyles: slashes, percent_r, mixed
95 | Style/RegexpLiteral:
96 | Exclude:
97 | - 'lib/wicked_pdf/middleware.rb'
98 | - 'lib/wicked_pdf/wicked_pdf_helper/assets.rb'
99 |
100 | # Offense count: 18
101 | # This cop supports safe autocorrection (--autocorrect).
102 | # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, AllowedPatterns, IgnoredPatterns.
103 | # URISchemes: http, https
104 | Layout/LineLength:
105 | Max: 563
106 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Change Log
2 | All notable changes to this project should be documented in this file.
3 | This project attempts to adhere to [Semantic Versioning](http://semver.org/).
4 |
5 | ## [[master branch] - Unreleased changes](https://github.com/mileszs/wicked_pdf/compare/2.8.2...HEAD)
6 | ### Breaking Changes
7 | ### New Features
8 | ### Fixes
9 |
10 | ## [2.8.2]
11 | ### Fixes
12 | - [Fix for frozen_string_literal in Ruby 3.4](https://github.com/mileszs/wicked_pdf/pull/1118)
13 | - [Add OpenStruct dependency explicitly for Ruby 3.5](https://github.com/mileszs/wicked_pdf/pull/1131)
14 |
15 | ## [2.8.1]
16 | ### Fixes
17 | - [Explicitly require OpenStruct, which isn't loaded by default anymore in new versions of Rake](https://github.com/mileszs/wicked_pdf/pull/1110)
18 | - [Ensure assets without extensions are handled correctly](https://github.com/mileszs/wicked_pdf/pull/1115)
19 |
20 | ## [2.8.0]
21 | ### New Features
22 | - [Add New config option `raise_on_missing_assets`](https://github.com/mileszs/wicked_pdf/pull/1094)
23 | - [Add support for truffleruby (22.1.0) on Linux](https://github.com/mileszs/wicked_pdf/pull/1028)
24 |
25 | ### Fixes
26 | - [Fix Propshaft encoding issue](https://github.com/mileszs/wicked_pdf/pull/1096)
27 | - [Fix Webpacker & Shakapacker compatibility issue](https://github.com/mileszs/wicked_pdf/pull/1099)
28 |
29 | ## [2.7.0]
30 | ### New Features
31 | - [Support Shakapacker 7](https://github.com/mileszs/wicked_pdf/pull/1067)
32 | - [Add option `delete_temporary_files` to keep the temporary files generated by `pdf_from_string` method](https://github.com/mileszs/wicked_pdf/pull/1068)
33 | - [Add support for --allow flag](https://github.com/mileszs/wicked_pdf/pull/1030)
34 |
35 | ## Fixes
36 | - [Add require for `stringio`, which is no longer loaded by default in Ruby 3.1+](https://github.com/mileszs/wicked_pdf/pull/1062)
37 | - [Fix CI build.](https://github.com/mileszs/wicked_pdf/pull/1055)
38 | - [Fix Header/footer temporary file is removed before `wkhtmltopdf` is called](https://github.com/mileszs/wicked_pdf/pull/1039)
39 | - [Bump rubocop to 1.46](https://github.com/mileszs/wicked_pdf/pull/1051)
40 | - [Add Ruby 3.2 to the test matrix](https://github.com/mileszs/wicked_pdf/pull/1046)
41 |
42 | ## [2.6.3]
43 | ### Fixes
44 | - [Fix typo of #possible_binary_locations](https://github.com/mileszs/wicked_pdf/pull/1025)
45 | - [Drop unused executables gemspec directive](https://github.com/mileszs/wicked_pdf/pull/1024)
46 |
47 | ## [2.6.2]
48 | ### Fixes
49 | - [Fix undefined local variable or method 'block' for render_to_string](https://github.com/mileszs/wicked_pdf/pull/962)
50 | - [Add require for `delegate`, which is no longer loaded by default in Ruby 2.7+](https://github.com/mileszs/wicked_pdf/pull/1019)
51 | ## [2.6.0]
52 | ### New Features
53 | - [Support Propshaft in find_asset helper](https://github.com/mileszs/wicked_pdf/pull/1010)
54 | ### Fixes
55 | - [Update Changelog with changes from 2.1.0](https://github.com/mileszs/wicked_pdf/pull/1013)
56 | - [Fix CI build for Rails 7.](https://github.com/mileszs/wicked_pdf/pull/1014)
57 |
58 | ## [2.5.4] December 20th 2021 769f9df487f3c1e31dc91431666baa78d2aa24fb
59 | ### New Features
60 | - [Test with Rails 7](https://github.com/mileszs/wicked_pdf/pull/998)
61 | ### Fixes
62 | - [Include view helper on view load.](https://github.com/mileszs/wicked_pdf/pull/992)
63 |
64 | ## [2.5.3] December 15th 2021 7991877de634067b4245fb47fdad65da43761887
65 | - [Fix check for webpacker version](https://github.com/mileszs/wicked_pdf/pull/964)
66 | - [Complete transition to Github actions](https://github.com/mileszs/wicked_pdf/pull/987)
67 |
68 | ## [2.5.2] November 2021 - fix webpacker_source_url bdd0ca3eca759e277ce5461141b1506f56fefcd1
69 | - [fix: `webpacker_source_url`](https://github.com/mileszs/wicked_pdf/pull/993)
70 | - [update README](https://github.com/mileszs/wicked_pdf/pull/968)
71 |
72 | ## [2.5.1] September 2021 - fix webpacker helper, github actions and Readme updates ae725e8055dc8f51a392c27767b4dcdcfffe155d
73 | - [Add comment about enable_local_file_access to README](https://github.com/mileszs/wicked_pdf/commit/2dc96dde2e0fd7362395064f2480cac1edcc1f48)
74 | - [README updates](https://github.com/mileszs/wicked_pdf/pull/974) &&
75 | - [Github actions](https://github.com/mileszs/wicked_pdf/pull/986)
76 | - [Screencast links](https://github.com/mileszs/wicked_pdf/pull/976)
77 | - [fix url generating in webpacker helper](https://github.com/mileszs/wicked_pdf/pull/973)
78 |
79 | ## [2.5.0] November 2020 Release - 2b1d47a84fce3600e7cbe2f50843af1a7b84d4a6
80 | - [Remove code for unsupported rails and ruby versions](https://github.com/mileszs/wicked_pdf/pull/925)
81 |
82 | ## [2.4.1] b56c46a05895def395ebc75ed8e822551c2c478f
83 | - [Extract reading in chunk](https://github.com/mileszs/wicked_pdf/pull/951)
84 | - [add ruby 2.7 to the test matrix](https://github.com/mileszs/wicked_pdf/pull/952)
85 |
86 | ## [2.4.0] 8c007a77057e1a6680469d1ef53aa19a108fe209
87 | ### New Features
88 | - [Do not unlink HTML temp files immediately (to enable HTML tempfile inspection)](https://github.com/mileszs/wicked_pdf/pull/950)
89 | - [Read HTML string and generated PDF file in chunks (to reduce memory overhead of generating large PDFs)](https://github.com/mileszs/wicked_pdf/pull/949)
90 | - [Add `wicked_pdf_url_base64` helper](https://github.com/mileszs/wicked_pdf/pull/947)
91 |
92 | ## [2.3.1] - Allow bundler 2.x ee6a5e1f807c872af37c1382f629dd4cac3040a8
93 | - [Adjust gemspec development dependencies](https://github.com/mileszs/wicked_pdf/pull/814)
94 |
95 | ## [2.3.0] - Remove support for Ruby 1.x and Rails 2.x 66149c67e54cd3a63dd27528f5b78255fdd5ac43
96 | - [Remove support for Ruby 1.x and Rails 2.x](https://github.com/mileszs/wicked_pdf/pull/859)
97 |
98 | ## [2.2.0] - October 2020 release f8abe706f5eb6dba2fcded473c81f2176e9d717e
99 | ### Fixes
100 | - [Make CI green again](https://github.com/mileszs/wicked_pdf/pull/939)
101 | - [rubocop fixes](https://github.com/mileszs/wicked_pdf/pull/945)
102 | ### New Features
103 | - [Add support for --keep-relative-links flag](https://github.com/mileszs/wicked_pdf/pull/930)
104 | - [Encapsulate binary path and version handling](https://github.com/mileszs/wicked_pdf/pull/816) && [#815](https://github.com/mileszs/wicked_pdf/pull/815)
105 |
106 |
107 | ## [2.1.0] - 2020-06-14
108 | ### Fixes
109 | - [Document no_stop_slow_scripts in README](https://github.com/mileszs/wicked_pdf/pull/905)
110 | - [Document how to use locals in README](https://github.com/mileszs/wicked_pdf/pull/915)
111 |
112 | ### New Features
113 | - [Improved support for Webpacker assets with `wicked_pdf_asset_pack_path`](https://github.com/mileszs/wicked_pdf/pull/896)
114 | - [Support enabling/disabling local file access compatible with wkhtmltopdf 0.12.6](https://github.com/mileszs/wicked_pdf/pull/920)
115 | - [Add option `use_xvfb` to emulate an X server](https://github.com/mileszs/wicked_pdf/pull/909)
116 |
117 | ## [2.0.2] - 2020-03-17
118 | ### Fixes
119 | - [Force UTF-8 encoding in assets helper](https://github.com/mileszs/wicked_pdf/pull/894)
120 |
121 | ## [2.0.1] - 2020-02-22
122 | ### Fixes
123 | - [Replace open-uri with more secure Net:HTTP.get](https://github.com/mileszs/wicked_pdf/pull/864)
124 |
125 | ## [2.0.0] - 2020-02-22
126 | ### Breaking changes
127 | - [Remove support for older Ruby and Rails versions](https://github.com/mileszs/wicked_pdf/pull/854) - This project no longer supports Ruby < 2.2 and Rails < 4. It may work for you, but we are no longer worrying about breaking backwards compatibility for versions older than these. If you are on an affected version, you can continue to use the 1.x releases. Patches to fix broken behavior on old versions may not be accepted unless they are highly decoupled from the rest of the code base.
128 |
129 | ### New Features
130 | - [Add Rubygems metadata hash to gemspec](https://github.com/mileszs/wicked_pdf/pull/856)
131 | - [Add support for Rails 6](https://github.com/mileszs/wicked_pdf/pull/869)
132 |
133 | ### Fixes
134 | - [Fix Webpacker helpers in production environment](https://github.com/mileszs/wicked_pdf/pull/837)
135 | - [Fix unit tests](https://github.com/mileszs/wicked_pdf/pull/852)
136 |
137 | ## [1.4.0] - 2019-05-23
138 | ### New Features
139 | - [Add support for `log_level` and `quiet` options](https://github.com/mileszs/wicked_pdf/pull/834)
140 |
141 | ## [1.3.0] - 2019-05-20
142 | ### New Features
143 | - [Add support for Webpacker provided bundles](https://github.com/mileszs/wicked_pdf/pull/739)
144 |
145 | ## [1.2.2] - 2019-04-13
146 | ### Fixes
147 | - [Fix issue loading Pty on Windows](https://github.com/mileszs/wicked_pdf/pull/820)
148 | - [Fix conflict with remotipart causing SystemStackError](https://github.com/mileszs/wicked_pdf/pull/821)
149 |
150 | ## [1.2.1] - 2019-03-16
151 | ### Fixes
152 | - [Fix `SystemStackError` in some setups](https://github.com/mileszs/wicked_pdf/pull/813)
153 |
154 | ## [1.2.0] - 2019-03-16
155 | ### New Features
156 | - [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)
157 | - [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)
158 | - [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.
159 |
160 | ### Fixes
161 | - [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
162 | - [Fix issue using the shell to locate `wkhtmltopdf` in a Bundler environment](https://github.com/mileszs/wicked_pdf/pull/728)
163 | - [Fix `wkhtmltopdf` path detection when HOME environment variable is unset](https://github.com/mileszs/wicked_pdf/pull/568)
164 | - [Fix error when the `Rails` constant is defined but not actually using Rails](https://github.com/mileszs/wicked_pdf/pull/613)
165 | - [Fix compatibility issue with Sprockets 4](https://github.com/mileszs/wicked_pdf/pull/615)
166 | - [Fix compatibility issue with `Mime::JS` in Rails 5.1+](https://github.com/mileszs/wicked_pdf/pull/627)
167 | - [Fix deprecation warning by using `after_action` instead of `after_filter` when available](https://github.com/mileszs/wicked_pdf/pull/663)
168 | - [Provide Rails `base_path` to `find_asset` calls for Sprockets file lookup](https://github.com/mileszs/wicked_pdf/pull/688)
169 | - Logger changes:
170 | - [Use `Rails.logger.debug` instead of `p`](https://github.com/mileszs/wicked_pdf/pull/575)
171 | - [Change logger message to prepend `[wicked_pdf]` instead of nonstandard `****************WICKED****************`](https://github.com/mileszs/wicked_pdf/pull/589)
172 | - Documentation changes:
173 | - [Update link to wkhtmltopdf homepage](https://github.com/mileszs/wicked_pdf/pull/582)
174 | - [Update link to `wkhtmltopdf_binary_gem`](https://github.com/mileszs/wicked_pdf/commit/59e6c5fca3985f2fa2f345089596250df5da2682)
175 | - [Update documentation for usage with the Asset Pipeline](https://github.com/mileszs/wicked_pdf/commit/690d00157706699a71b7dcd71834759f4d84702f)
176 | - [Document `default_protocol` option](https://github.com/mileszs/wicked_pdf/pull/585)
177 | - [Document `image` and `no_image` options](https://github.com/mileszs/wicked_pdf/pull/689)
178 | - [Document issue with DPI/scaling on various platforms](https://github.com/mileszs/wicked_pdf/pull/715)
179 | - [Document creating and attaching a PDF in a mailer](https://github.com/mileszs/wicked_pdf/pull/746)
180 | - [Document dependency on `wkhtmltopdf` with RubyGems](https://github.com/mileszs/wicked_pdf/pull/656)
181 | - [Add example using WickedPDF with Rails in an API-only configuration](https://github.com/mileszs/wicked_pdf/pull/796)
182 | - [Add example for rending a template as a header/footer](https://github.com/mileszs/wicked_pdf/pull/603)
183 | - [Add GitHub issue template](https://github.com/mileszs/wicked_pdf/pull/805)
184 | - [Add CodeClimate Badge](https://github.com/mileszs/wicked_pdf/pull/646)
185 | - RuboCop cleanup
186 | - Updates to Travis CI pipeline to support newer versions of Ruby & Rails
187 |
188 | ## [1.1.0] - 2016-08-30
189 | ### New Features
190 | - Support Rails 5.x and Sprockets 3.x
191 | - 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/)
192 | - Support `no_stop_slow_scripts: true` to let slow running scripts delay rendering
193 | - [Changes to asset finding to support Rails 5](https://github.com/mileszs/wicked_pdf/pull/561)
194 |
195 | ### Fixes
196 | - [Improved error handling](https://github.com/mileszs/wicked_pdf/pull/543)
197 | - [Namespace helper classes under WickedPdf namespace](https://github.com/mileszs/wicked_pdf/pull/538)
198 |
199 | ## [1.0.6] - 2016-04-04
200 | ### Fixes
201 | - Revert shell escaping 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.
202 |
203 | ## [1.0.5] - 2016-03-28
204 | ### Fixes
205 | - 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!
206 | - 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.
207 |
208 | ## [1.0.4] - 2016-01-26
209 | ### Fixes
210 | - 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`.
211 | - [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.
212 |
213 | ## [1.0.3] - 2015-12-02
214 | ### Fixes
215 | - 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.
216 |
217 | ## [1.0.2] - 2015-11-30
218 | ### Fixes
219 | - 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)
220 |
221 | ## [1.0.1] - 2015-11-19
222 | ### Fixes
223 | - Made minor RuboCop style tweaks.
224 | - Added default [RuboCop](https://github.com/bbatsov/rubocop) config and run after test suite.
225 | - Issue with `nil.basename` from asset helpers.
226 |
227 | ## [1.0.0] - 2015-11-03
228 | ### Breaking Changes
229 | - 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)).
230 | - Stopped attempting to track with version number of `wkhtmltopdf` binary releases (`wkhtmltopdf` v9.x == `WickedPDF` v9.x)
231 | - Adopted [Semantic Versioning](http://semver.org/) for release numbering
232 | - Added a CHANGELOG (based on [keepachangelog.com](http://keepachangelog.com/))
233 | - Misc code tweaks as suggested by [RuboCop](https://github.com/bbatsov/rubocop)
234 |
235 | ### New Features
236 | - Check version of `wkhtmltopdf` before deciding to pass arguments with or without dashes
237 | - New arguments and options for the table of contents supported in newer versions of wkhtmltopdf: `text_size_shrink`, `level_indentation`, `disable_dotted_lines`, `disable_toc_links`, `xsl_style_sheet`
238 | - Merge in global options to `pdf_from_html_file` and `pdf_from_string`
239 | - Add ability to generate pdf from a web resource: `pdf_from_url(url)`
240 | - Removed explicit dependency on [Rails](https://github.com/rails/rails), since parts of this library may be used without it.
241 |
242 | ### Fixes
243 | - Comment out the `:exe_path` option in the generated initializer by default (since many systems won't have `wkthmltopdf` installed in that specific location)
244 | - Issues with `file://` paths on Windows-based systems
245 | - Issues with parsed options/argument ordering on versions of `wkthmltopdf` > 0.9
246 | - Issues with middleware headers when running Rails app mounted in a subdirectory
247 | - Issues with options that have a `key: 'value'` syntax when passed to `wkthmltopdf`
248 | - Issue with `:temp_path` option being deleted from original options hash
249 | - Issue with header/footer `:content` being deleted after the first page
250 | - Issues with options being modified during processing (including global config options)
251 | - Issues with asset helpers recognizing assets specified without a protocol
252 | - Issues with `url()` references and embedded `data:base64` assets in stylesheets rendered with `wicked_pdf_stylesheet_link_tag`
253 | - Asset helpers no longer add a file extension if it already is specified with one
254 |
255 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source 'https://rubygems.org'
2 |
3 | gemspec
4 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
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.
23 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Wicked PDF [](http://badge.fury.io/rb/wicked_pdf) [](https://github.com/mileszs/wicked_pdf/actions/workflows/ci.yml) [](https://codeclimate.com/github/mileszs/wicked_pdf) [](https://www.codetriage.com/mileszs/wicked_pdf)
2 |
3 | ## A PDF generation plugin for Ruby on Rails
4 |
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 |
7 | _Wicked PDF has been verified to work on Ruby versions 2.2 through 3.2; Rails 4 through 7.0_
8 |
9 | ### Installation
10 |
11 | Add this to your Gemfile and run `bundle install`:
12 |
13 | ```ruby
14 | gem 'wicked_pdf'
15 | ```
16 |
17 | Then create the initializer with
18 |
19 | rails generate wicked_pdf
20 |
21 | You may also need to add
22 | ```ruby
23 | Mime::Type.register "application/pdf", :pdf
24 | ```
25 | to `config/initializers/mime_types.rb` in older versions of Rails.
26 |
27 | Because `wicked_pdf` is a wrapper for [wkhtmltopdf](http://wkhtmltopdf.org/), you'll need to install that, too.
28 |
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 |
32 | ```ruby
33 | gem 'wkhtmltopdf-binary'
34 | ```
35 |
36 | To your Gemfile and run `bundle install`.
37 |
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)
41 |
42 | If your wkhtmltopdf executable is not on your webserver's path, you can configure it in an initializer:
43 |
44 | ```ruby
45 | WickedPdf.configure do |c|
46 | c.exe_path = '/usr/local/bin/wkhtmltopdf'
47 | c.enable_local_file_access = true
48 | end
49 | ```
50 |
51 | For more information about `wkhtmltopdf`, see the project's [homepage](http://wkhtmltopdf.org/).
52 |
53 | ### Basic Usage
54 | ```ruby
55 | class ThingsController < ApplicationController
56 | def show
57 | respond_to do |format|
58 | format.html
59 | format.pdf do
60 | render pdf: "file_name" # Excluding ".pdf" extension.
61 | end
62 | end
63 | end
64 | end
65 | ```
66 | ### Usage Conditions - Important!
67 |
68 | 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.
69 |
70 | #### wicked_pdf helpers
71 | ```html
72 |
73 |
74 |
86 |
87 |
88 | ```
89 | 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.
90 |
91 | ```html
92 |
93 |
94 |
95 |
96 | <%= stylesheet_link_tag wicked_pdf_asset_base64("pdf") %>
97 | <%= javascript_include_tag wicked_pdf_asset_base64("number_pages") %>
98 |
99 |
100 |
101 |
107 |
108 |
109 | ```
110 |
111 | #### Webpacker usage
112 |
113 | wicked_pdf supports webpack assets.
114 |
115 | - Use `wicked_pdf_stylesheet_pack_tag` for stylesheets
116 | - Use `wicked_pdf_javascript_pack_tag` for javascripts
117 | - Use `wicked_pdf_asset_pack_path` to access an asset directly, for example: `image_tag wicked_pdf_asset_pack_path("media/images/foobar.png")`
118 |
119 | #### Asset pipeline usage
120 |
121 | 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.
122 |
123 | config.assets.precompile += ['blueprint/screen.css', 'pdf.css', 'jquery.ui.datepicker.js', 'pdf.js', ...etc...]
124 |
125 | #### CDN reference
126 |
127 | 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.
128 | ```html
129 |
130 |
131 |
132 | <%= javascript_include_tag "http://code.jquery.com/jquery-1.10.0.min.js" %>
133 | <%= javascript_include_tag "http://code.jquery.com/ui/1.10.3/jquery-ui.min.js" %>
134 | ```
135 |
136 | ### Advanced Usage with all available options
137 |
138 | _NOTE: Certain options are only supported in specific versions of wkhtmltopdf._
139 |
140 | ```ruby
141 | class ThingsController < ApplicationController
142 | def show
143 | respond_to do |format|
144 | format.html
145 | format.pdf do
146 | render pdf: 'file_name',
147 | disposition: 'attachment', # default 'inline'
148 | template: 'things/show',
149 | locals: {foo: @bar},
150 | file: "#{Rails.root}/files/foo.erb",
151 | inline: 'INLINE HTML',
152 | layout: 'pdf', # for a pdf.pdf.erb file
153 | wkhtmltopdf: '/usr/local/bin/wkhtmltopdf', # path to binary
154 | show_as_html: params.key?('debug'), # allow debugging based on url param
155 | orientation: 'Landscape', # default Portrait
156 | page_size: 'A4, Letter, ...', # default A4
157 | page_height: NUMBER,
158 | page_width: NUMBER,
159 | save_to_file: Rails.root.join('pdfs', "#{filename}.pdf"),
160 | save_only: false, # depends on :save_to_file being set first
161 | default_protocol: 'http',
162 | proxy: 'TEXT',
163 | basic_auth: false # when true username & password are automatically sent from session
164 | username: 'TEXT',
165 | password: 'TEXT',
166 | title: 'Alternate Title', # otherwise first page title is used
167 | cover: 'URL, Pathname, or raw HTML string',
168 | dpi: 'dpi',
169 | encoding: 'TEXT',
170 | user_style_sheet: 'URL',
171 | cookie: ['_session_id SESSION_ID'], # could be an array or a single string in a 'name value' format
172 | post: ['query QUERY_PARAM'], # could be an array or a single string in a 'name value' format
173 | redirect_delay: NUMBER,
174 | javascript_delay: NUMBER,
175 | window_status: 'TEXT', # wait to render until some JS sets window.status to the given string
176 | image_quality: NUMBER,
177 | no_pdf_compression: true,
178 | zoom: FLOAT,
179 | page_offset: NUMBER,
180 | book: true,
181 | default_header: true,
182 | disable_javascript: false,
183 | grayscale: true,
184 | lowquality: true,
185 | enable_plugins: true,
186 | disable_internal_links: true,
187 | disable_external_links: true,
188 | keep_relative_links: true,
189 | print_media_type: true,
190 |
191 | # define as true the key 'disable_local_file_access' or 'enable_local_file_access', not both
192 | disable_local_file_access: true,
193 | enable_local_file_access: false, # must be true when using wkhtmltopdf > 0.12.6
194 | allow: ["#{Rails.root}/public"], # could be an array or a single string
195 |
196 | disable_smart_shrinking: true,
197 | use_xserver: true,
198 | background: false, # background needs to be true to enable background colors to render
199 | no_background: true,
200 | no_stop_slow_scripts: false,
201 | viewport_size: 'TEXT', # available only with use_xserver or patched QT
202 | extra: '', # directly inserted into the command to wkhtmltopdf
203 | raise_on_all_errors: nil, # raise error for any stderr output. Such as missing media, image assets
204 | raise_on_missing_assets: nil, # raise when trying to access a missing asset
205 | log_level: 'info', # Available values: none, error, warn, or info - only available with wkhtmltopdf 0.12.5+
206 | quiet: false, # `false` is same as `log_level: 'info'`, `true` is same as `log_level: 'none'`
207 | outline: { outline: true,
208 | outline_depth: LEVEL },
209 | margin: { top: SIZE, # default 10 (mm)
210 | bottom: SIZE,
211 | left: SIZE,
212 | right: SIZE },
213 | header: { html: { template: 'users/header', # use :template OR :url
214 | layout: 'pdf_plain', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout
215 | url: 'www.example.com',
216 | locals: { foo: @bar }},
217 | center: 'TEXT',
218 | font_name: 'NAME',
219 | font_size: SIZE,
220 | left: 'TEXT',
221 | right: 'TEXT',
222 | spacing: REAL,
223 | line: true,
224 | content: 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
225 | footer: { html: { template:'shared/footer', # use :template OR :url
226 | layout: 'pdf_plain.html', # optional, use 'pdf_plain' for a pdf_plain.html.pdf.erb file, defaults to main layout
227 | url: 'www.example.com',
228 | locals: { foo: @bar }},
229 | center: 'TEXT',
230 | font_name: 'NAME',
231 | font_size: SIZE,
232 | left: 'TEXT',
233 | right: 'TEXT',
234 | spacing: REAL,
235 | line: true,
236 | content: 'HTML CONTENT ALREADY RENDERED'}, # optionally you can pass plain html already rendered (useful if using pdf_from_string)
237 | toc: { font_name: "NAME",
238 | depth: LEVEL,
239 | header_text: "TEXT",
240 | header_fs: SIZE,
241 | text_size_shrink: 0.8,
242 | l1_font_size: SIZE,
243 | l2_font_size: SIZE,
244 | l3_font_size: SIZE,
245 | l4_font_size: SIZE,
246 | l5_font_size: SIZE,
247 | l6_font_size: SIZE,
248 | l7_font_size: SIZE,
249 | level_indentation: NUM,
250 | l1_indentation: NUM,
251 | l2_indentation: NUM,
252 | l3_indentation: NUM,
253 | l4_indentation: NUM,
254 | l5_indentation: NUM,
255 | l6_indentation: NUM,
256 | l7_indentation: NUM,
257 | no_dots: true,
258 | disable_dotted_lines: true,
259 | disable_links: true,
260 | disable_toc_links: true,
261 | disable_back_links:true,
262 | xsl_style_sheet: 'file.xsl'}, # optional XSLT stylesheet to use for styling table of contents
263 | progress: proc { |output| puts output }, # proc called when console output changes
264 | delete_temporary_files: true # explicitly delete temporary files, default false
265 | end
266 | end
267 | end
268 | end
269 | ```
270 | By default, it will render without a layout (layout: false) and the template for the current controller and action.
271 |
272 | #### wkhtmltopdf Binary Options
273 |
274 | 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.
275 |
276 | Examples of those options are:
277 |
278 | ```ruby
279 | print_media_type: true # Passes `--print-media-type`
280 | no_background: true # Passes `--no-background`
281 | ```
282 |
283 | You can see the complete list of options under "Global Options" in wkhtmltopdf usage [docs](http://wkhtmltopdf.org/usage/wkhtmltopdf.txt).
284 |
285 | ### Super Advanced Usage ###
286 |
287 | If you need to just create a pdf and not display it:
288 | ```ruby
289 | # create a pdf from a string
290 | pdf = WickedPdf.new.pdf_from_string('
Hello There!
')
291 |
292 | # create a pdf file from a html file without converting it to string
293 | # Path must be absolute path
294 | pdf = WickedPdf.new.pdf_from_html_file('/your/absolute/path/here')
295 |
296 | # create a pdf from a URL
297 | pdf = WickedPdf.new.pdf_from_url('https://github.com/mileszs/wicked_pdf')
298 |
299 | # create a pdf from string using templates, layouts, and content option for header or footer
300 | pdf = WickedPdf.new.pdf_from_string(
301 | render_to_string('templates/pdf', layout: 'pdfs/layout_pdf.html'),
302 | footer: {
303 | content: render_to_string(
304 | 'templates/footer',
305 | layout: 'pdfs/layout_pdf.html'
306 | )
307 | }
308 | )
309 |
310 | # It is possible to use footer/header templates without a layout, in that case you need to provide a valid HTML document
311 | pdf = WickedPdf.new.pdf_from_string(
312 | render_to_string('templates/full_pdf_template'),
313 | header: {
314 | content: render_to_string('templates/full_header_template')
315 | }
316 | )
317 |
318 | # or from your controller, using views & templates and all wicked_pdf options as normal
319 | pdf = render_to_string pdf: "some_file_name", template: "templates/pdf", encoding: "UTF-8"
320 |
321 | # then save to a file
322 | save_path = Rails.root.join('pdfs','filename.pdf')
323 | File.open(save_path, 'wb') do |file|
324 | file << pdf
325 | end
326 |
327 | # you can also track progress on your PDF generation, such as when using it from within a Resque job
328 | class PdfJob
329 | def perform
330 | blk = proc { |output|
331 | match = output.match(/\[.+\] Page (?\d+) of (?\d+)/)
332 | if match
333 | current_page = match[:current_page].to_i
334 | total_pages = match[:total_pages].to_i
335 | message = "Generated #{current_page} of #{total_pages} pages"
336 | at current_page, total_pages, message
337 | end
338 | }
339 | WickedPdf.new.pdf_from_string(html, progress: blk)
340 | end
341 | end
342 | ```
343 | If you need to display utf encoded characters, add this to your pdf views or layouts:
344 | ```html
345 |
346 | ```
347 | If you need to return a PDF in a controller with Rails in API mode:
348 | ```ruby
349 | pdf_html = ActionController::Base.new.render_to_string(template: 'controller_name/action_name', layout: 'pdf')
350 | pdf = WickedPdf.new.pdf_from_string(pdf_html)
351 | send_data pdf, filename: 'file.pdf'
352 | ```
353 | ### Page Breaks
354 |
355 | You can control page breaks with CSS.
356 |
357 | Add a few styles like this to your stylesheet or page:
358 | ```css
359 | div.alwaysbreak { page-break-before: always; }
360 | div.nobreak:before { clear:both; }
361 | div.nobreak { page-break-inside: avoid; }
362 | ```
363 |
364 | ### Page Numbering
365 |
366 | A bit of javascript can help you number your pages. Create a template or header/footer file with this:
367 | ```html
368 |
369 |
370 |
382 |
383 |
384 | Page of
385 |
386 |
387 | ```
388 | Anything with a class listed in "var x" above will be auto-filled at render time.
389 |
390 | 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]":
391 | ```ruby
392 | render pdf: 'filename', header: { right: '[page] of [topage]' }
393 | ```
394 | ### Configuration
395 |
396 | You can put your default configuration, applied to all pdf's at "wicked_pdf.rb" initializer.
397 |
398 | ### Rack Middleware
399 |
400 | 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:
401 | ```ruby
402 | # in application.rb (Rails3) or environment.rb (Rails2)
403 | require 'wicked_pdf'
404 | config.middleware.use WickedPdf::Middleware
405 | ```
406 | If you want to turn on or off the middleware for certain URLs, use the `:only` or `:except` conditions like so:
407 | ```ruby
408 | # conditions can be plain strings or regular expressions, and you can supply only one or an array
409 | config.middleware.use WickedPdf::Middleware, {}, only: '/invoice'
410 | config.middleware.use WickedPdf::Middleware, {}, except: [ %r[^/admin], '/secret', %r[^/people/\d] ]
411 | ```
412 | If you use the standard `render pdf: 'some_pdf'` in your app, you will want to exclude those actions from the middleware.
413 |
414 |
415 | ### Include in an email as an attachment
416 |
417 | To include a rendered pdf file in an email you can do the following:
418 |
419 | ```ruby
420 | attachments['attachment.pdf'] = WickedPdf.new.pdf_from_string(
421 | render_to_string('link_to_view.pdf.erb', layout: 'pdf')
422 | )
423 | ```
424 |
425 | 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.
426 |
427 | ### Further Reading
428 |
429 | Mike Ackerman's post [How To Create PDFs in Rails](https://www.viget.com/articles/how-to-create-pdfs-in-rails)
430 |
431 | Andreas Happe's post [Generating PDFs from Ruby on Rails](http://www.snikt.net/blog/2012/04/26/wicked-pdf/)
432 |
433 | JESii's post [WickedPDF, wkhtmltopdf, and Heroku...a tricky combination](http://www.nubyrubyrailstales.com/2013/06/wickedpdf-wkhtmltopdf-and-herokua.html)
434 |
435 | 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/)
436 |
437 | Corsego's 2021 post [Complete guide to generating PDFs with gem wicked_pdf](https://blog.corsego.com/gem-wicked-pdf)
438 |
439 | PDFTron's post [How to Generate PDFs With Ruby on Rails](https://www.pdftron.com/blog/rails/how-to-generate-pdf-with-ruby-on-rails/)
440 |
441 | StackOverflow [questions with the tag "wicked-pdf"](http://stackoverflow.com/questions/tagged/wicked-pdf)
442 |
443 | ### Screencasts
444 |
445 | * SupeRails Screencast [EN]
446 |
447 | [](https://youtu.be/tFvtwEmW-GE)
448 |
449 | * codigofacilito Screencast [ES]
450 |
451 | [](https://youtu.be/jeWM_gusmJc)
452 |
453 | ### Debugging
454 |
455 | 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.
456 |
457 | 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:
458 |
459 | http://localhost:3001/CONTROLLER/X.pdf?debug
460 |
461 | 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:
462 | ```html
463 | <%= params.key?('debug') ? image_tag('foo') : wicked_pdf_image_tag('foo') %>
464 | ```
465 |
466 | #### Gotchas
467 |
468 | 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).
469 |
470 | 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.
471 |
472 | ### Security considerations
473 |
474 | WickedPdf renders page content on the server by saving HTML and assets to temporary files on disk, then executing `wkhtmltopdf` to convert that HTML to a PDF file.
475 |
476 | It is highly recommended if you allow user-generated HTML/CSS/JS to be converted to PDF, you sanitize it first, or at least disallow requesting content from internal IP addresses and hostnames.
477 |
478 | For example, these could potentially leak internal AWS metadata:
479 | ```html
480 |
481 |