html2pdf-rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.rubocop.yml +5 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +142 -0
- data/LICENSE.txt +21 -0
- data/README.md +127 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/html2pdf-rails.gemspec +30 -0
- data/lib/html2pdf/rails.rb +20 -0
- data/lib/html2pdf/rails/client.rb +28 -0
- data/lib/html2pdf/rails/helper.rb +14 -0
- data/lib/html2pdf/rails/railtie.rb +24 -0
- data/lib/html2pdf/rails/rendering.rb +36 -0
- data/lib/html2pdf/rails/version.rb +7 -0
- metadata +119 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 2206892dfeb806c182e8549bfa6cf84ae83b34493ef607e070ee17e059542b20
|
4
|
+
data.tar.gz: dad4f0a90421b5c82948343a08ad23664f4af7a02001cd93a2b7e826dbce42ee
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 26700591edf5eb4826d2ed867383e173c5074c0e1a74f3b3ca91227f53be49eac99fbcc77e9ce9f2150788d59792d2c08fd8685724f8f0f94c1345bc8efeed8e
|
7
|
+
data.tar.gz: eaeddfd1ceacb84cb04175988fa01019be0877d766beceddafaff7ca60c9e2f327422f37e02f685c92f4d261958989a9f6b2cda1bc309ca61bad3f0adebe70a4
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
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 lala.akira@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,142 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
html2pdf-rails (0.1.0)
|
5
|
+
rails (>= 5.2.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (5.2.1)
|
11
|
+
actionpack (= 5.2.1)
|
12
|
+
nio4r (~> 2.0)
|
13
|
+
websocket-driver (>= 0.6.1)
|
14
|
+
actionmailer (5.2.1)
|
15
|
+
actionpack (= 5.2.1)
|
16
|
+
actionview (= 5.2.1)
|
17
|
+
activejob (= 5.2.1)
|
18
|
+
mail (~> 2.5, >= 2.5.4)
|
19
|
+
rails-dom-testing (~> 2.0)
|
20
|
+
actionpack (5.2.1)
|
21
|
+
actionview (= 5.2.1)
|
22
|
+
activesupport (= 5.2.1)
|
23
|
+
rack (~> 2.0)
|
24
|
+
rack-test (>= 0.6.3)
|
25
|
+
rails-dom-testing (~> 2.0)
|
26
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
27
|
+
actionview (5.2.1)
|
28
|
+
activesupport (= 5.2.1)
|
29
|
+
builder (~> 3.1)
|
30
|
+
erubi (~> 1.4)
|
31
|
+
rails-dom-testing (~> 2.0)
|
32
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
33
|
+
activejob (5.2.1)
|
34
|
+
activesupport (= 5.2.1)
|
35
|
+
globalid (>= 0.3.6)
|
36
|
+
activemodel (5.2.1)
|
37
|
+
activesupport (= 5.2.1)
|
38
|
+
activerecord (5.2.1)
|
39
|
+
activemodel (= 5.2.1)
|
40
|
+
activesupport (= 5.2.1)
|
41
|
+
arel (>= 9.0)
|
42
|
+
activestorage (5.2.1)
|
43
|
+
actionpack (= 5.2.1)
|
44
|
+
activerecord (= 5.2.1)
|
45
|
+
marcel (~> 0.3.1)
|
46
|
+
activesupport (5.2.1)
|
47
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
48
|
+
i18n (>= 0.7, < 2)
|
49
|
+
minitest (~> 5.1)
|
50
|
+
tzinfo (~> 1.1)
|
51
|
+
arel (9.0.0)
|
52
|
+
builder (3.2.3)
|
53
|
+
concurrent-ruby (1.0.5)
|
54
|
+
crass (1.0.4)
|
55
|
+
diff-lcs (1.3)
|
56
|
+
erubi (1.7.1)
|
57
|
+
globalid (0.4.1)
|
58
|
+
activesupport (>= 4.2.0)
|
59
|
+
i18n (1.1.1)
|
60
|
+
concurrent-ruby (~> 1.0)
|
61
|
+
loofah (2.2.2)
|
62
|
+
crass (~> 1.0.2)
|
63
|
+
nokogiri (>= 1.5.9)
|
64
|
+
mail (2.7.1)
|
65
|
+
mini_mime (>= 0.1.1)
|
66
|
+
marcel (0.3.3)
|
67
|
+
mimemagic (~> 0.3.2)
|
68
|
+
method_source (0.9.0)
|
69
|
+
mimemagic (0.3.2)
|
70
|
+
mini_mime (1.0.1)
|
71
|
+
mini_portile2 (2.3.0)
|
72
|
+
minitest (5.11.3)
|
73
|
+
nio4r (2.3.1)
|
74
|
+
nokogiri (1.8.5)
|
75
|
+
mini_portile2 (~> 2.3.0)
|
76
|
+
rack (2.0.5)
|
77
|
+
rack-test (1.1.0)
|
78
|
+
rack (>= 1.0, < 3)
|
79
|
+
rails (5.2.1)
|
80
|
+
actioncable (= 5.2.1)
|
81
|
+
actionmailer (= 5.2.1)
|
82
|
+
actionpack (= 5.2.1)
|
83
|
+
actionview (= 5.2.1)
|
84
|
+
activejob (= 5.2.1)
|
85
|
+
activemodel (= 5.2.1)
|
86
|
+
activerecord (= 5.2.1)
|
87
|
+
activestorage (= 5.2.1)
|
88
|
+
activesupport (= 5.2.1)
|
89
|
+
bundler (>= 1.3.0)
|
90
|
+
railties (= 5.2.1)
|
91
|
+
sprockets-rails (>= 2.0.0)
|
92
|
+
rails-dom-testing (2.0.3)
|
93
|
+
activesupport (>= 4.2.0)
|
94
|
+
nokogiri (>= 1.6)
|
95
|
+
rails-html-sanitizer (1.0.4)
|
96
|
+
loofah (~> 2.2, >= 2.2.2)
|
97
|
+
railties (5.2.1)
|
98
|
+
actionpack (= 5.2.1)
|
99
|
+
activesupport (= 5.2.1)
|
100
|
+
method_source
|
101
|
+
rake (>= 0.8.7)
|
102
|
+
thor (>= 0.19.0, < 2.0)
|
103
|
+
rake (10.5.0)
|
104
|
+
rspec (3.8.0)
|
105
|
+
rspec-core (~> 3.8.0)
|
106
|
+
rspec-expectations (~> 3.8.0)
|
107
|
+
rspec-mocks (~> 3.8.0)
|
108
|
+
rspec-core (3.8.0)
|
109
|
+
rspec-support (~> 3.8.0)
|
110
|
+
rspec-expectations (3.8.2)
|
111
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
112
|
+
rspec-support (~> 3.8.0)
|
113
|
+
rspec-mocks (3.8.0)
|
114
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
115
|
+
rspec-support (~> 3.8.0)
|
116
|
+
rspec-support (3.8.0)
|
117
|
+
sprockets (3.7.2)
|
118
|
+
concurrent-ruby (~> 1.0)
|
119
|
+
rack (> 1, < 3)
|
120
|
+
sprockets-rails (3.2.1)
|
121
|
+
actionpack (>= 4.0)
|
122
|
+
activesupport (>= 4.0)
|
123
|
+
sprockets (>= 3.0.0)
|
124
|
+
thor (0.20.0)
|
125
|
+
thread_safe (0.3.6)
|
126
|
+
tzinfo (1.2.5)
|
127
|
+
thread_safe (~> 0.1)
|
128
|
+
websocket-driver (0.7.0)
|
129
|
+
websocket-extensions (>= 0.1.0)
|
130
|
+
websocket-extensions (0.1.3)
|
131
|
+
|
132
|
+
PLATFORMS
|
133
|
+
ruby
|
134
|
+
|
135
|
+
DEPENDENCIES
|
136
|
+
bundler (~> 1.16)
|
137
|
+
html2pdf-rails!
|
138
|
+
rake (~> 10.0)
|
139
|
+
rspec (~> 3.0)
|
140
|
+
|
141
|
+
BUNDLED WITH
|
142
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 aki
|
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,127 @@
|
|
1
|
+
# html2pdf-rails
|
2
|
+
|
3
|
+
PDF generator (from HTML) gem for Ruby on Rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'html2pdf-rails'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install html2pdf-rails
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
### Basic Usage
|
24
|
+
|
25
|
+
Controller
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
class ThingsController < ApplicationController
|
29
|
+
def show
|
30
|
+
respond_to do |format|
|
31
|
+
format.html
|
32
|
+
format.pdf do
|
33
|
+
render_to_pdf pdf: 'file_name' # Excluding ".pdf" extension.
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
```
|
39
|
+
|
40
|
+
Layout
|
41
|
+
|
42
|
+
```haml
|
43
|
+
!!!
|
44
|
+
%html
|
45
|
+
%head
|
46
|
+
%meta{ charset: 'utf-8' }
|
47
|
+
= html2pdf_base_tag
|
48
|
+
= stylesheet_link_tag 'pdf', media: 'all'
|
49
|
+
%body
|
50
|
+
#header= image_tag 'logo.jpg'
|
51
|
+
#content= yield
|
52
|
+
```
|
53
|
+
|
54
|
+
### Advanced Usage with all available options
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
class ThingsController < ApplicationController
|
58
|
+
def show
|
59
|
+
respond_to do |format|
|
60
|
+
format.html
|
61
|
+
format.pdf do
|
62
|
+
render_to_pdf(
|
63
|
+
pdf: 'file_name', # Excluding ".pdf" extension.
|
64
|
+
disposition: 'attachment', # default 'inline'
|
65
|
+
template: 'things/show',
|
66
|
+
layout: 'pdf', # for a pdf.pdf.erb file
|
67
|
+
show_as_html: params.key?('debug'), # allow debugging based on url param
|
68
|
+
pdf_options: { # SEE: https://github.com/GoogleChrome/puppeteer/blob/master/docs/api.md#pagepdfoptions
|
69
|
+
margin: {
|
70
|
+
top: '30px'
|
71
|
+
bottom: '30px',
|
72
|
+
}
|
73
|
+
}
|
74
|
+
)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
### Cloud Functions for Firebase Sample
|
82
|
+
|
83
|
+
```javascript
|
84
|
+
const functions = require("firebase-functions");
|
85
|
+
const puppeteer = require("puppeteer");
|
86
|
+
|
87
|
+
const runOptions = {
|
88
|
+
timeoutSeconds: 20,
|
89
|
+
memory: "1GB"
|
90
|
+
};
|
91
|
+
exports.html2pdf = functions
|
92
|
+
.runWith(runOptions)
|
93
|
+
.https.onRequest(
|
94
|
+
async ({ method, body: { html = "", pdfOptions = {} } }, res) => {
|
95
|
+
const browser = await puppeteer.launch({
|
96
|
+
headless: true,
|
97
|
+
args: ["--no-sandbox"]
|
98
|
+
});
|
99
|
+
const page = await browser.newPage();
|
100
|
+
await page.emulateMedia("print");
|
101
|
+
await page.goto("data:text/html;charset=UTF-8," + html, {
|
102
|
+
waitUntil: "networkidle0"
|
103
|
+
});
|
104
|
+
const pdf = await page.pdf(pdfOptions);
|
105
|
+
res.header({ "Content-Type": "application/pdf" });
|
106
|
+
res.send(pdf);
|
107
|
+
}
|
108
|
+
);
|
109
|
+
```
|
110
|
+
|
111
|
+
## Configuration
|
112
|
+
|
113
|
+
In `config/initializers/html2pdf_rails.rb`, you can configure the following values.
|
114
|
+
|
115
|
+
```ruby
|
116
|
+
Html2Pdf.configure do |config|
|
117
|
+
config.endpoint = 'YOUR_HTTP_TRIGGER_ENDPOINT'
|
118
|
+
end
|
119
|
+
```
|
120
|
+
|
121
|
+
## Contributing
|
122
|
+
|
123
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/SonicGarden/html2pdf-rails. 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.
|
124
|
+
|
125
|
+
## License
|
126
|
+
|
127
|
+
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
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "html2pdf/rails"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "html2pdf/rails/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "html2pdf-rails"
|
8
|
+
spec.version = Html2Pdf::Rails::VERSION
|
9
|
+
spec.authors = ["aki77"]
|
10
|
+
spec.email = ["aki77@users.noreply.github.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{PDF generator (from HTML) gem for Ruby on Rails}
|
13
|
+
spec.description = %q{PDF generator (from HTML) gem for Ruby on Rails}
|
14
|
+
spec.homepage = "https://github.com/SonicGarden/html2pdf-rails"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_dependency "rails", ">= 5.2.0"
|
27
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
28
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
30
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
require 'html2pdf/rails/version'
|
3
|
+
require 'html2pdf/rails/railtie'
|
4
|
+
|
5
|
+
module Html2Pdf
|
6
|
+
class Config
|
7
|
+
include ActiveSupport::Configurable
|
8
|
+
config_accessor :endpoint
|
9
|
+
end
|
10
|
+
|
11
|
+
class << self
|
12
|
+
def configure(&block)
|
13
|
+
yield config
|
14
|
+
end
|
15
|
+
|
16
|
+
def config
|
17
|
+
@config ||= Config.new
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Html2Pdf
|
4
|
+
module Rails
|
5
|
+
class Client
|
6
|
+
def self.post(*args)
|
7
|
+
self.new(Html2Pdf.config.endpoint).post(*args)
|
8
|
+
end
|
9
|
+
|
10
|
+
def initialize(endpoint)
|
11
|
+
@uri = URI.parse(endpoint)
|
12
|
+
end
|
13
|
+
|
14
|
+
def post(html, pdf_options = {})
|
15
|
+
http = Net::HTTP.new(@uri.host, @uri.port).tap { |h| h.use_ssl = @uri.scheme == 'https' }
|
16
|
+
request = Net::HTTP::Post.new(@uri.request_uri, headers)
|
17
|
+
request.body = { html: html, pdfOptions: pdf_options }.to_json
|
18
|
+
http.request(request)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def headers
|
24
|
+
{ 'Content-Type' => 'application/json' }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Html2Pdf
|
4
|
+
module Rails
|
5
|
+
module Helper
|
6
|
+
def html2pdf_base_tag
|
7
|
+
# NOTE: for Ngrok
|
8
|
+
host = request.headers['HTTP_X_ORIGINAL_HOST'] || request.host
|
9
|
+
base_url = "#{request.protocol}#{host}"
|
10
|
+
tag.base href: base_url
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'html2pdf/rails/rendering'
|
4
|
+
require 'html2pdf/rails/helper'
|
5
|
+
|
6
|
+
module Html2Pdf
|
7
|
+
module Rails
|
8
|
+
class Railtie < ::Rails::Railtie
|
9
|
+
ActiveSupport.on_load(:action_controller) do
|
10
|
+
ActionController::Base.prepend Rendering
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport.on_load(:action_view) do
|
14
|
+
ActionView::Base.include Helper
|
15
|
+
end
|
16
|
+
|
17
|
+
config.after_initialize do
|
18
|
+
if Html2Pdf.config.endpoint.blank?
|
19
|
+
raise 'Html2Pdf.config.endpoint is required'
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'html2pdf/rails/client'
|
4
|
+
|
5
|
+
module Html2Pdf
|
6
|
+
module Rails
|
7
|
+
module Rendering
|
8
|
+
def render_to_pdf(options)
|
9
|
+
make_and_send_pdf(options.delete(:pdf), options)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def make_and_send_pdf(pdf_name, options = {})
|
15
|
+
options[:layout] ||= false
|
16
|
+
options[:template] ||= File.join(controller_path, action_name)
|
17
|
+
options[:disposition] ||= 'inline'
|
18
|
+
options[:pdf_options] ||= {}
|
19
|
+
|
20
|
+
if options[:show_as_html]
|
21
|
+
render_opts = options.slice(:template, :layout, :formats, :handlers)
|
22
|
+
render(render_opts.merge({ content_type: 'text/html' }))
|
23
|
+
else
|
24
|
+
pdf_content = make_pdf(options)
|
25
|
+
send_data(pdf_content, filename: pdf_name + '.pdf', type: 'application/pdf', disposition: options[:disposition])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def make_pdf(options = {})
|
30
|
+
render_opts = options.slice(:template, :layout, :formats, :handlers)
|
31
|
+
html = render_to_string(render_opts)
|
32
|
+
Client.post(html, options[:pdf_options]).body
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,119 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: html2pdf-rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- aki77
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 5.2.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 5.2.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.16'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.16'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: PDF generator (from HTML) gem for Ruby on Rails
|
70
|
+
email:
|
71
|
+
- aki77@users.noreply.github.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".rubocop.yml"
|
79
|
+
- ".travis.yml"
|
80
|
+
- CODE_OF_CONDUCT.md
|
81
|
+
- Gemfile
|
82
|
+
- Gemfile.lock
|
83
|
+
- LICENSE.txt
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- bin/console
|
87
|
+
- bin/setup
|
88
|
+
- html2pdf-rails.gemspec
|
89
|
+
- lib/html2pdf/rails.rb
|
90
|
+
- lib/html2pdf/rails/client.rb
|
91
|
+
- lib/html2pdf/rails/helper.rb
|
92
|
+
- lib/html2pdf/rails/railtie.rb
|
93
|
+
- lib/html2pdf/rails/rendering.rb
|
94
|
+
- lib/html2pdf/rails/version.rb
|
95
|
+
homepage: https://github.com/SonicGarden/html2pdf-rails
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.7.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: PDF generator (from HTML) gem for Ruby on Rails
|
119
|
+
test_files: []
|