wkhtmltopdf_runner 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +36 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +11 -0
- data/Gemfile.lock +61 -0
- data/LICENSE +21 -0
- data/README.md +88 -0
- data/Rakefile +8 -0
- data/bin/console +14 -0
- data/bin/rubocop +29 -0
- data/bin/setup +8 -0
- data/lib/wkhtmltopdf_runner/cmd.rb +80 -0
- data/lib/wkhtmltopdf_runner/configuration.rb +22 -0
- data/lib/wkhtmltopdf_runner/error.rb +6 -0
- data/lib/wkhtmltopdf_runner/path.rb +45 -0
- data/lib/wkhtmltopdf_runner/path_validator.rb +38 -0
- data/lib/wkhtmltopdf_runner/runner.rb +50 -0
- data/lib/wkhtmltopdf_runner/utils.rb +32 -0
- data/lib/wkhtmltopdf_runner/version.rb +5 -0
- data/lib/wkhtmltopdf_runner.rb +29 -0
- data/wkhtmltopdf_runner.gemspec +42 -0
- metadata +128 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 25395e8c97bcf92fbfb3c1900aeebc29fde134bd284ea20ac8d0a6001392a7b3
|
|
4
|
+
data.tar.gz: b2854859d2dcb315f891ab90d9e890d2233c44ac55c2eb36450b334dd0df5508
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b0bc3de109daf0557bb5d52fb075d024bfcca427b29f4f85077b82dab1b55d3cc710c7eb118bb9a7189be192277d9ea826a0f5fb637d6e20d29b85427416647b
|
|
7
|
+
data.tar.gz: c62ce9a592e75d480fc466a25a777582b728835e91c5a32478539136ef15312c85609d30919416abeb4245dfb9294653764e6cead326e73abfd27e62caf8e600
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
AllCops:
|
|
2
|
+
TargetRubyVersion: 2.3
|
|
3
|
+
Exclude:
|
|
4
|
+
- 'bin/rubocop'
|
|
5
|
+
|
|
6
|
+
Layout/AlignParameters:
|
|
7
|
+
Enabled: true
|
|
8
|
+
EnforcedStyle: with_fixed_indentation
|
|
9
|
+
|
|
10
|
+
Layout/MultilineOperationIndentation:
|
|
11
|
+
Enabled: true
|
|
12
|
+
EnforcedStyle: indented
|
|
13
|
+
|
|
14
|
+
Layout/MultilineMethodCallIndentation:
|
|
15
|
+
Enabled: true
|
|
16
|
+
EnforcedStyle: indented
|
|
17
|
+
|
|
18
|
+
Layout/CaseIndentation:
|
|
19
|
+
Enabled: true
|
|
20
|
+
EnforcedStyle: end
|
|
21
|
+
|
|
22
|
+
Layout/EndAlignment:
|
|
23
|
+
Enabled: true
|
|
24
|
+
EnforcedStyleAlignWith: variable
|
|
25
|
+
|
|
26
|
+
Style/Documentation:
|
|
27
|
+
Enabled: false
|
|
28
|
+
|
|
29
|
+
Style/DoubleNegation:
|
|
30
|
+
Exclude:
|
|
31
|
+
- 'lib/wkhtmltopdf_runner/utils.rb'
|
|
32
|
+
|
|
33
|
+
Metrics/BlockLength:
|
|
34
|
+
Exclude:
|
|
35
|
+
- '*.gemspec'
|
|
36
|
+
- 'spec/**/*_spec.rb'
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at jpalumickas@gmail.com. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
wkhtmltopdf_runner (0.1.0)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
ast (2.4.0)
|
|
10
|
+
coderay (1.1.2)
|
|
11
|
+
diff-lcs (1.3)
|
|
12
|
+
jaro_winkler (1.5.2)
|
|
13
|
+
method_source (0.9.2)
|
|
14
|
+
parallel (1.13.0)
|
|
15
|
+
parser (2.6.0.0)
|
|
16
|
+
ast (~> 2.4.0)
|
|
17
|
+
powerpack (0.1.2)
|
|
18
|
+
pry (0.12.2)
|
|
19
|
+
coderay (~> 1.1.0)
|
|
20
|
+
method_source (~> 0.9.0)
|
|
21
|
+
rainbow (3.0.0)
|
|
22
|
+
rake (10.5.0)
|
|
23
|
+
rspec (3.8.0)
|
|
24
|
+
rspec-core (~> 3.8.0)
|
|
25
|
+
rspec-expectations (~> 3.8.0)
|
|
26
|
+
rspec-mocks (~> 3.8.0)
|
|
27
|
+
rspec-core (3.8.0)
|
|
28
|
+
rspec-support (~> 3.8.0)
|
|
29
|
+
rspec-expectations (3.8.2)
|
|
30
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
31
|
+
rspec-support (~> 3.8.0)
|
|
32
|
+
rspec-mocks (3.8.0)
|
|
33
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
|
34
|
+
rspec-support (~> 3.8.0)
|
|
35
|
+
rspec-support (3.8.0)
|
|
36
|
+
rubocop (0.64.0)
|
|
37
|
+
jaro_winkler (~> 1.5.1)
|
|
38
|
+
parallel (~> 1.10)
|
|
39
|
+
parser (>= 2.5, != 2.5.1.1)
|
|
40
|
+
powerpack (~> 0.1)
|
|
41
|
+
rainbow (>= 2.2.2, < 4.0)
|
|
42
|
+
ruby-progressbar (~> 1.7)
|
|
43
|
+
unicode-display_width (~> 1.4.0)
|
|
44
|
+
ruby-progressbar (1.10.0)
|
|
45
|
+
unicode-display_width (1.4.1)
|
|
46
|
+
wkhtmltopdf-binary-edge (0.12.5.0)
|
|
47
|
+
|
|
48
|
+
PLATFORMS
|
|
49
|
+
ruby
|
|
50
|
+
|
|
51
|
+
DEPENDENCIES
|
|
52
|
+
bundler (~> 2.0)
|
|
53
|
+
pry
|
|
54
|
+
rake (~> 10.0)
|
|
55
|
+
rspec (~> 3.0)
|
|
56
|
+
rubocop
|
|
57
|
+
wkhtmltopdf-binary-edge (~> 0.12.5)
|
|
58
|
+
wkhtmltopdf_runner!
|
|
59
|
+
|
|
60
|
+
BUNDLED WITH
|
|
61
|
+
2.0.1
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Justas Palumickas
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
# Wkhtmltopdf Runner
|
|
2
|
+
|
|
3
|
+
This gem is a wrapper for a popular `wkhtmltopdf` library to generate PDF files from HTML.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile and then run `bundle install`:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'wkhtmltopdf_runner'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
You also need to have installed `wkhtmltopdf`. Easy way to do that is to add binary files to your Gemfile.
|
|
14
|
+
```ruby
|
|
15
|
+
gem 'wkhtmltopdf-binary' # or 'wkhtmltopdf-binary-edge'
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
If you have custom path for `wkhtmltopdf` please see [Configuration](#configuration)
|
|
19
|
+
|
|
20
|
+
## Usage
|
|
21
|
+
|
|
22
|
+
### Write to File
|
|
23
|
+
|
|
24
|
+
Use block which has File argument that will be returned after PDF generation.
|
|
25
|
+
|
|
26
|
+
You can render PDF from HTML string:
|
|
27
|
+
|
|
28
|
+
```rb
|
|
29
|
+
string = '<h1>Hello user</h1>'
|
|
30
|
+
|
|
31
|
+
WkhtmltopdfRunner.pdf_from_string(string) do |file|
|
|
32
|
+
user.document.attach(io: file, file_name: 'document.pdf')
|
|
33
|
+
end
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
You can render PDF from HTML file:
|
|
37
|
+
|
|
38
|
+
```rb
|
|
39
|
+
File.open('index.html') do |html_file|
|
|
40
|
+
WkhtmltopdfRunner.pdf_from_file(html_file) do |file|
|
|
41
|
+
user.document.attach(io: file, file_name: 'document.pdf')
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
You can render PDF from URL:
|
|
47
|
+
|
|
48
|
+
```rb
|
|
49
|
+
WkhtmltopdfRunner.pdf_from_url('https://github.com') do |file|
|
|
50
|
+
user.document.attach(io: file, file_name: 'document.pdf')
|
|
51
|
+
end
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Render to String
|
|
55
|
+
|
|
56
|
+
If you will not provide block, PDF string will be returned.
|
|
57
|
+
|
|
58
|
+
> **Warning:** Be careful when using this method because all PDF content will
|
|
59
|
+
> be stored in Memory. Better to use block which will return file. See examples
|
|
60
|
+
> above.
|
|
61
|
+
|
|
62
|
+
```rb
|
|
63
|
+
pdf_string = WkhtmltopdfRunner.pdf_from_url('https://github.com')
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Configuration
|
|
67
|
+
|
|
68
|
+
If you're using Rails, create file in `config/initializers/wkhtmltopdf_runner.rb`
|
|
69
|
+
|
|
70
|
+
```rb
|
|
71
|
+
WkhtmltopdfRunner.configure do |config|
|
|
72
|
+
config.debug = true # Default: false
|
|
73
|
+
config.logger = Logger.new('runner.log') # Default: STDOUT or Rails.logger in Rails
|
|
74
|
+
config.binary_path = '/path/to/wkhtmltopdf' # Default: will search automatically in PATH or Gemfile
|
|
75
|
+
end
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Contributing
|
|
79
|
+
|
|
80
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/jpalumickas/wkhtmltopdf_runner. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
81
|
+
|
|
82
|
+
## Code of Conduct
|
|
83
|
+
|
|
84
|
+
Everyone interacting in the WkhtmltopdfRunner project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/jpalumickas/wkhtmltopdf_runner/blob/master/CODE_OF_CONDUCT.md).
|
|
85
|
+
|
|
86
|
+
## License
|
|
87
|
+
|
|
88
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
require 'bundler/setup'
|
|
5
|
+
require 'wkhtmltopdf_runner'
|
|
6
|
+
|
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
9
|
+
|
|
10
|
+
# require 'irb'
|
|
11
|
+
# IRB.start(__FILE__)
|
|
12
|
+
|
|
13
|
+
require 'pry'
|
|
14
|
+
Pry.start
|
data/bin/rubocop
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
#
|
|
5
|
+
# This file was generated by Bundler.
|
|
6
|
+
#
|
|
7
|
+
# The application 'rubocop' is installed as part of a gem, and
|
|
8
|
+
# this file is here to facilitate running it.
|
|
9
|
+
#
|
|
10
|
+
|
|
11
|
+
require 'pathname'
|
|
12
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
|
13
|
+
Pathname.new(__FILE__).realpath)
|
|
14
|
+
|
|
15
|
+
bundle_binstub = File.expand_path('bundle', __dir__)
|
|
16
|
+
|
|
17
|
+
if File.file?(bundle_binstub)
|
|
18
|
+
if File.read(bundle_binstub, 300) =~ /This file was generated by Bundler/
|
|
19
|
+
load(bundle_binstub)
|
|
20
|
+
else
|
|
21
|
+
abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
|
|
22
|
+
Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
require 'rubygems'
|
|
27
|
+
require 'bundler/setup'
|
|
28
|
+
|
|
29
|
+
load Gem.bin_path('rubocop', 'rubocop')
|
data/bin/setup
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'open3'
|
|
4
|
+
|
|
5
|
+
module WkhtmltopdfRunner
|
|
6
|
+
class Cmd
|
|
7
|
+
attr_reader :url, :file, :options, :config
|
|
8
|
+
|
|
9
|
+
def initialize(url:, file:, config:, options: {})
|
|
10
|
+
@url = url
|
|
11
|
+
@file = file
|
|
12
|
+
@config = config
|
|
13
|
+
@options = options.reverse_merge(config.options)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def run
|
|
17
|
+
validate!
|
|
18
|
+
debug_command!
|
|
19
|
+
|
|
20
|
+
err = Open3.popen3(*command) do |_stdin, _stdout, stderr|
|
|
21
|
+
stderr.read
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
unless err&.strip&.empty?
|
|
25
|
+
raise WkhtmltopdfRunner::Error,
|
|
26
|
+
"Error generating PDF. Command Error:\n#{err}"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
private
|
|
33
|
+
|
|
34
|
+
def validate!
|
|
35
|
+
WkhtmltopdfRunner::PathValidator.validate!(wkhtmltopdf_path)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def debug_command!
|
|
39
|
+
return unless config.debug
|
|
40
|
+
|
|
41
|
+
config.logger.debug("[WkhtmltopdfRunner] Running #{command.join(' ')}")
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def command
|
|
45
|
+
command = [wkhtmltopdf_path]
|
|
46
|
+
command << '-q' # Output is in stderr so we need to run in quiet mode
|
|
47
|
+
command += formatted_options
|
|
48
|
+
command << url
|
|
49
|
+
command << file_path
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def formatted_options
|
|
53
|
+
opts = options.each_with_object([]) do |(key, value), list|
|
|
54
|
+
next if value == false
|
|
55
|
+
|
|
56
|
+
dashed_key = WkhtmltopdfRunner::Utils.dasherize(key.to_s)
|
|
57
|
+
|
|
58
|
+
list << if value == true
|
|
59
|
+
"--#{dashed_key}"
|
|
60
|
+
else
|
|
61
|
+
["--#{dashed_key}"].concat(Array(value))
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
opts.flatten
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def file_path
|
|
69
|
+
if file.respond_to?(:path)
|
|
70
|
+
file.path.to_s
|
|
71
|
+
else
|
|
72
|
+
file.to_s
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def wkhtmltopdf_path
|
|
77
|
+
@wkhtmltopdf_path ||= WkhtmltopdfRunner::Path.new(config.binary_path).call
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'logger'
|
|
4
|
+
|
|
5
|
+
module WkhtmltopdfRunner
|
|
6
|
+
class Configuration
|
|
7
|
+
attr_writer :logger, :options
|
|
8
|
+
attr_accessor :debug, :binary_path
|
|
9
|
+
|
|
10
|
+
def logger
|
|
11
|
+
@logger ||= begin
|
|
12
|
+
return Rails.logger if defined?(Rails)
|
|
13
|
+
|
|
14
|
+
Logger.new(STDOUT)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def options
|
|
19
|
+
@options || {}
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module WkhtmltopdfRunner
|
|
4
|
+
class Path
|
|
5
|
+
EXE_NAME = 'wkhtmltopdf'
|
|
6
|
+
|
|
7
|
+
def initialize(path = nil)
|
|
8
|
+
@path = path
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call
|
|
12
|
+
@path || find_wkhtmltopdf_binary_path
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
|
|
17
|
+
def find_wkhtmltopdf_binary_path
|
|
18
|
+
path_from_which || path_from_env
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def path_from_which
|
|
22
|
+
detected_path = if defined?(Bundler)
|
|
23
|
+
Bundler.which(EXE_NAME)
|
|
24
|
+
else
|
|
25
|
+
`which #{EXE_NAME}`.chomp
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
WkhtmltopdfRunner::Utils.present?(detected_path) && detected_path
|
|
29
|
+
rescue StandardError
|
|
30
|
+
nil
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def path_from_env
|
|
34
|
+
possible_locations = [
|
|
35
|
+
ENV['PATH'].split(':'),
|
|
36
|
+
%w[/usr/bin /usr/local/bin]
|
|
37
|
+
]
|
|
38
|
+
|
|
39
|
+
possible_locations << %w[~/bin] if ENV.key?('HOME')
|
|
40
|
+
possible_locations
|
|
41
|
+
.flatten.map { |l| File.expand_path(File.join(l, EXE_NAME)) }
|
|
42
|
+
.detect { |location| File.exist?(location) }
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module WkhtmltopdfRunner
|
|
4
|
+
class PathValidator
|
|
5
|
+
def self.validate!(path)
|
|
6
|
+
new(path).validate!
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
attr_reader :path
|
|
10
|
+
|
|
11
|
+
def initialize(path)
|
|
12
|
+
@path = path
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def validate!
|
|
16
|
+
validate_if_path_exists!
|
|
17
|
+
validate_if_path_executable!
|
|
18
|
+
|
|
19
|
+
true
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def validate_if_path_exists!
|
|
25
|
+
return if path && !path.empty? && File.exist?(path)
|
|
26
|
+
|
|
27
|
+
raise WkhtmltopdfRunner::InvalidPathError,
|
|
28
|
+
"Cannot find wkhtmltopdf location #{path}".strip
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def validate_if_path_executable!
|
|
32
|
+
return if File.executable?(path)
|
|
33
|
+
|
|
34
|
+
raise WkhtmltopdfRunner::InvalidPathError,
|
|
35
|
+
"#{path} is not executable."
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'wkhtmltopdf_runner/configuration'
|
|
4
|
+
|
|
5
|
+
module WkhtmltopdfRunner
|
|
6
|
+
class Runner
|
|
7
|
+
def pdf_from_url(url, options = {})
|
|
8
|
+
Tempfile.open(['file-', '.pdf']) do |pdf_file|
|
|
9
|
+
pdf_file.binmode
|
|
10
|
+
|
|
11
|
+
run(url, pdf_file, options)
|
|
12
|
+
pdf_file.rewind
|
|
13
|
+
|
|
14
|
+
return yield(pdf_file) if block_given?
|
|
15
|
+
|
|
16
|
+
pdf_file.read
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def pdf_from_file(file, options = {}, &block)
|
|
21
|
+
pdf_from_url(file.path, options, &block)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def pdf_from_string(string, options = {}, &block)
|
|
25
|
+
Tempfile.open(['file-', '.html']) do |html_file|
|
|
26
|
+
html_file.binmode
|
|
27
|
+
html_file.write(string)
|
|
28
|
+
html_file.rewind
|
|
29
|
+
pdf_from_file(html_file, options, &block)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def run(url, file, options = {})
|
|
34
|
+
WkhtmltopdfRunner::Cmd
|
|
35
|
+
.new(url: url, file: file, config: config, options: options)
|
|
36
|
+
.run
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def config
|
|
40
|
+
@config ||= Configuration.new
|
|
41
|
+
end
|
|
42
|
+
alias configuration config
|
|
43
|
+
|
|
44
|
+
# Configure client with a block of settings.
|
|
45
|
+
def configure
|
|
46
|
+
yield(config) if block_given?
|
|
47
|
+
true
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module WkhtmltopdfRunner
|
|
4
|
+
module Utils
|
|
5
|
+
BLANK_RE = /\A[[:space:]]*\z/.freeze
|
|
6
|
+
|
|
7
|
+
def self.dasherize(word)
|
|
8
|
+
underscore(word).tr('_', '-')
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def self.underscore(camel_cased_word)
|
|
12
|
+
return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)
|
|
13
|
+
|
|
14
|
+
word = camel_cased_word.to_s.gsub('::', '/')
|
|
15
|
+
word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
|
|
16
|
+
word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
|
|
17
|
+
word.tr!('-', '_')
|
|
18
|
+
word.downcase!
|
|
19
|
+
word
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def self.blank?(obj)
|
|
23
|
+
return !!BLANK_RE.match(obj) if obj.is_a?(String)
|
|
24
|
+
|
|
25
|
+
obj.respond_to?(:empty?) ? !!obj.empty? : !obj
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def self.present?(obj)
|
|
29
|
+
!blank?(obj)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'wkhtmltopdf_runner/version'
|
|
4
|
+
require 'wkhtmltopdf_runner/error'
|
|
5
|
+
require 'wkhtmltopdf_runner/utils'
|
|
6
|
+
require 'wkhtmltopdf_runner/path_validator'
|
|
7
|
+
require 'wkhtmltopdf_runner/path'
|
|
8
|
+
require 'wkhtmltopdf_runner/cmd'
|
|
9
|
+
require 'wkhtmltopdf_runner/runner'
|
|
10
|
+
|
|
11
|
+
module WkhtmltopdfRunner
|
|
12
|
+
class << self
|
|
13
|
+
def runner
|
|
14
|
+
@runner ||= WkhtmltopdfRunner::Runner.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def method_missing(method_name, *args, &block)
|
|
20
|
+
return super unless runner.respond_to?(method_name)
|
|
21
|
+
|
|
22
|
+
runner.send(method_name, *args, &block)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def respond_to_missing?(method_name, include_private = false)
|
|
26
|
+
runner.respond_to?(method_name, include_private)
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
5
|
+
require 'wkhtmltopdf_runner/version'
|
|
6
|
+
|
|
7
|
+
Gem::Specification.new do |spec|
|
|
8
|
+
spec.name = 'wkhtmltopdf_runner'
|
|
9
|
+
spec.version = WkhtmltopdfRunner::VERSION
|
|
10
|
+
spec.authors = ['Justas Palumickas']
|
|
11
|
+
spec.email = ['jpalumickas@gmail.com']
|
|
12
|
+
|
|
13
|
+
spec.summary = 'Wrapper for wkhtmltopdf to generate PDF from HTML.'
|
|
14
|
+
spec.description = 'This gem is a wrapper for a popular wkhtmltopdf ' \
|
|
15
|
+
'library to generate PDF files from HTML.'
|
|
16
|
+
spec.homepage = 'https://github.com/jpalumickas/wkhtmltopdf_runner'
|
|
17
|
+
spec.license = 'MIT'
|
|
18
|
+
|
|
19
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
|
20
|
+
spec.metadata['source_code_uri'] = 'https://github.com/jpalumickas/wkhtmltopdf_runner'
|
|
21
|
+
spec.metadata['changelog_uri'] = 'https://github.com/jpalumickas/wkhtmltopdf_runner/releases'
|
|
22
|
+
spec.metadata['bug_tracker_uri'] = 'https://github.com/jpalumickas/wkhtmltopdf_runner/issues'
|
|
23
|
+
|
|
24
|
+
# Specify which files should be added to the gem when it is released.
|
|
25
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added
|
|
26
|
+
# into git.
|
|
27
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
28
|
+
`git ls-files -z`.split("\x0")
|
|
29
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
30
|
+
end
|
|
31
|
+
spec.bindir = 'exe'
|
|
32
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
33
|
+
spec.require_paths = ['lib']
|
|
34
|
+
|
|
35
|
+
spec.requirements << 'wkhtmltopdf'
|
|
36
|
+
spec.required_ruby_version = '>= 2.3.0'
|
|
37
|
+
|
|
38
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
|
39
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
40
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
41
|
+
spec.add_development_dependency 'wkhtmltopdf-binary-edge', '~> 0.12.5'
|
|
42
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: wkhtmltopdf_runner
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Justas Palumickas
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-02-19 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: wkhtmltopdf-binary-edge
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 0.12.5
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 0.12.5
|
|
69
|
+
description: This gem is a wrapper for a popular wkhtmltopdf library to generate PDF
|
|
70
|
+
files from HTML.
|
|
71
|
+
email:
|
|
72
|
+
- jpalumickas@gmail.com
|
|
73
|
+
executables: []
|
|
74
|
+
extensions: []
|
|
75
|
+
extra_rdoc_files: []
|
|
76
|
+
files:
|
|
77
|
+
- ".gitignore"
|
|
78
|
+
- ".rspec"
|
|
79
|
+
- ".rubocop.yml"
|
|
80
|
+
- ".travis.yml"
|
|
81
|
+
- CODE_OF_CONDUCT.md
|
|
82
|
+
- Gemfile
|
|
83
|
+
- Gemfile.lock
|
|
84
|
+
- LICENSE
|
|
85
|
+
- README.md
|
|
86
|
+
- Rakefile
|
|
87
|
+
- bin/console
|
|
88
|
+
- bin/rubocop
|
|
89
|
+
- bin/setup
|
|
90
|
+
- lib/wkhtmltopdf_runner.rb
|
|
91
|
+
- lib/wkhtmltopdf_runner/cmd.rb
|
|
92
|
+
- lib/wkhtmltopdf_runner/configuration.rb
|
|
93
|
+
- lib/wkhtmltopdf_runner/error.rb
|
|
94
|
+
- lib/wkhtmltopdf_runner/path.rb
|
|
95
|
+
- lib/wkhtmltopdf_runner/path_validator.rb
|
|
96
|
+
- lib/wkhtmltopdf_runner/runner.rb
|
|
97
|
+
- lib/wkhtmltopdf_runner/utils.rb
|
|
98
|
+
- lib/wkhtmltopdf_runner/version.rb
|
|
99
|
+
- wkhtmltopdf_runner.gemspec
|
|
100
|
+
homepage: https://github.com/jpalumickas/wkhtmltopdf_runner
|
|
101
|
+
licenses:
|
|
102
|
+
- MIT
|
|
103
|
+
metadata:
|
|
104
|
+
homepage_uri: https://github.com/jpalumickas/wkhtmltopdf_runner
|
|
105
|
+
source_code_uri: https://github.com/jpalumickas/wkhtmltopdf_runner
|
|
106
|
+
changelog_uri: https://github.com/jpalumickas/wkhtmltopdf_runner/releases
|
|
107
|
+
bug_tracker_uri: https://github.com/jpalumickas/wkhtmltopdf_runner/issues
|
|
108
|
+
post_install_message:
|
|
109
|
+
rdoc_options: []
|
|
110
|
+
require_paths:
|
|
111
|
+
- lib
|
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
113
|
+
requirements:
|
|
114
|
+
- - ">="
|
|
115
|
+
- !ruby/object:Gem::Version
|
|
116
|
+
version: 2.3.0
|
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
|
+
requirements:
|
|
119
|
+
- - ">="
|
|
120
|
+
- !ruby/object:Gem::Version
|
|
121
|
+
version: '0'
|
|
122
|
+
requirements:
|
|
123
|
+
- wkhtmltopdf
|
|
124
|
+
rubygems_version: 3.0.2
|
|
125
|
+
signing_key:
|
|
126
|
+
specification_version: 4
|
|
127
|
+
summary: Wrapper for wkhtmltopdf to generate PDF from HTML.
|
|
128
|
+
test_files: []
|