browserless 1.0.0 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.standard.yml +3 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +75 -0
- data/LICENSE.txt +21 -0
- data/README.md +169 -0
- data/Rakefile +15 -0
- data/lib/browserless/client.rb +87 -0
- data/lib/browserless/configuration.rb +27 -0
- data/lib/browserless/options.rb +41 -0
- data/lib/browserless/style_tag.rb +23 -0
- data/lib/browserless/version.rb +5 -0
- data/lib/browserless.rb +22 -0
- metadata +16 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 14f47b6657d20f63991dbacbae3c9ec7c7cc8408896a2e94f37680741156cbc9
|
4
|
+
data.tar.gz: 459c77a3d92d28ea270cec0b2bfa3c230e752ecd4820eebc6f14a152c90c9205
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 12204ed5fe0e0fd23d904836aa780a412ac318e8e54009f1cb28af61e6dac809651ca94e7aadd24ea9dc07a02122e0e5afc05a14f38f20ead4441987e5049ac8
|
7
|
+
data.tar.gz: 04e2eaac1111ccd04e2ab294b88cc6e957a52abb86c81ae9feb467757035898ebee6a3e1a1fb3e99f7ea5997df1724a096423271f5da23bed4c0a6cf73fdb6cc
|
data/.standard.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at thomasvanholder@hey.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
source "https://rubygems.org"
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in browserless.gemspec
|
6
|
+
gemspec
|
7
|
+
|
8
|
+
gem "httparty" # , "~> 0.18"
|
9
|
+
|
10
|
+
group :test do
|
11
|
+
gem "rake", "~> 13.0"
|
12
|
+
gem "standard", "~> 1.3"
|
13
|
+
gem "minitest"
|
14
|
+
gem "debug"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
browserless (1.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
ast (2.4.2)
|
11
|
+
builder (3.2.4)
|
12
|
+
debug (1.7.2)
|
13
|
+
irb (>= 1.5.0)
|
14
|
+
reline (>= 0.3.1)
|
15
|
+
httparty (0.21.0)
|
16
|
+
mini_mime (>= 1.0.0)
|
17
|
+
multi_xml (>= 0.5.2)
|
18
|
+
io-console (0.6.0)
|
19
|
+
irb (1.6.3)
|
20
|
+
reline (>= 0.3.0)
|
21
|
+
json (2.6.3)
|
22
|
+
language_server-protocol (3.17.0.3)
|
23
|
+
mini_mime (1.1.2)
|
24
|
+
minitest (5.18.0)
|
25
|
+
minitest-reporters (1.6.0)
|
26
|
+
ansi
|
27
|
+
builder
|
28
|
+
minitest (>= 5.0)
|
29
|
+
ruby-progressbar
|
30
|
+
multi_xml (0.6.0)
|
31
|
+
parallel (1.22.1)
|
32
|
+
parser (3.2.2.0)
|
33
|
+
ast (~> 2.4.1)
|
34
|
+
rainbow (3.1.1)
|
35
|
+
rake (13.0.6)
|
36
|
+
regexp_parser (2.7.0)
|
37
|
+
reline (0.3.3)
|
38
|
+
io-console (~> 0.5)
|
39
|
+
rexml (3.2.5)
|
40
|
+
rubocop (1.48.1)
|
41
|
+
json (~> 2.3)
|
42
|
+
parallel (~> 1.10)
|
43
|
+
parser (>= 3.2.0.0)
|
44
|
+
rainbow (>= 2.2.2, < 4.0)
|
45
|
+
regexp_parser (>= 1.8, < 3.0)
|
46
|
+
rexml (>= 3.2.5, < 4.0)
|
47
|
+
rubocop-ast (>= 1.26.0, < 2.0)
|
48
|
+
ruby-progressbar (~> 1.7)
|
49
|
+
unicode-display_width (>= 2.4.0, < 3.0)
|
50
|
+
rubocop-ast (1.28.0)
|
51
|
+
parser (>= 3.2.1.0)
|
52
|
+
rubocop-performance (1.16.0)
|
53
|
+
rubocop (>= 1.7.0, < 2.0)
|
54
|
+
rubocop-ast (>= 0.4.0)
|
55
|
+
ruby-progressbar (1.13.0)
|
56
|
+
standard (1.26.0)
|
57
|
+
language_server-protocol (~> 3.17.0.2)
|
58
|
+
rubocop (~> 1.48.1)
|
59
|
+
rubocop-performance (~> 1.16.0)
|
60
|
+
unicode-display_width (2.4.2)
|
61
|
+
|
62
|
+
PLATFORMS
|
63
|
+
arm64-darwin-22
|
64
|
+
|
65
|
+
DEPENDENCIES
|
66
|
+
browserless!
|
67
|
+
debug
|
68
|
+
httparty
|
69
|
+
minitest
|
70
|
+
minitest-reporters (~> 1.1)
|
71
|
+
rake (~> 13.0)
|
72
|
+
standard (~> 1.3)
|
73
|
+
|
74
|
+
BUNDLED WITH
|
75
|
+
2.3.7
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2023 Thomas Van Holder
|
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,169 @@
|
|
1
|
+
# Browserless Gem
|
2
|
+
|
3
|
+
Introducing the "Browserless" gem - a Ruby gem that provides a convenient wrapper around the [Browserless PDF API](https://www.browserless.io/docs/pdf). Browserless relies on puppeteer to convert modern CSS into a PDF. The goal of this gem is to enables developers to easily generate PDF documents from the HTML you already have.
|
4
|
+
|
5
|
+
By utilizing a managed service such as [Browserless.io](https://www.browserless.io/), you can concentrate on your core application functionality while relying on a simple API call to address your PDF generation requirements.
|
6
|
+
|
7
|
+
The free trial of browerless includes upto 1,000 PDFs a month, 10 concurrent browsers.
|
8
|
+
|
9
|
+
__Benefits__
|
10
|
+
- turn your existing HTML and CSS into a PDF
|
11
|
+
- supports modern CSS frameworks such as TailwindCSS
|
12
|
+
- no need to install, run and maintain puppeteer
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'browserless'
|
20
|
+
```
|
21
|
+
|
22
|
+
And then execute:
|
23
|
+
**
|
24
|
+
$ bundle install**
|
25
|
+
|
26
|
+
Or install it yourself as:
|
27
|
+
|
28
|
+
$ gem install browserless
|
29
|
+
|
30
|
+
## Configuration
|
31
|
+
Add your Browserless.io API key in an initializer file, like `config/browserless.rb`. You can set default configuration options in the initializer.
|
32
|
+
|
33
|
+
```rb
|
34
|
+
Browserless.configure do |config|
|
35
|
+
config.api_key = "your_api_key_here"
|
36
|
+
config.emulate_media = "print" # choose between print or screen (default)
|
37
|
+
config.style_tag = File.read(Rails.root.join("app/assets/builds/application.css")) # Pass public asset URL or CSS string content
|
38
|
+
config.options = {
|
39
|
+
landscape: false # default
|
40
|
+
print_background: false,
|
41
|
+
format: "A4" # default https://pptr.dev/api/puppeteer.paperformat#remarks
|
42
|
+
display_header_footer: false, # default
|
43
|
+
header_template: "<div>...</div>", # ensure display_header_footer is true
|
44
|
+
footer_template: "<div>...</div>", # ensure display_header_footer is true
|
45
|
+
margin: {
|
46
|
+
top: "2cm",
|
47
|
+
left: "0.5cm",
|
48
|
+
right: "0.5cm",
|
49
|
+
bottom: "2cm"
|
50
|
+
},
|
51
|
+
}
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
__api_key__
|
56
|
+
Make sure to replace `"your_api_key_here"` with your actual Browserless.io API key.
|
57
|
+
|
58
|
+
__emulate_media__
|
59
|
+
You can specify the media type by passing in the optional `emulate_media` keyword argument. Choose between `screen` (default) or `print`.
|
60
|
+
|
61
|
+
>TailwindCSS supports the [print modifier](https://tailwindcss.com/docs/hover-focus-and-other-states#print-styles), so you can conditional add styles to only be displayed with the PDF is being generated.
|
62
|
+
|
63
|
+
__style_tag__
|
64
|
+
You can use both a public URL or pass the CSS as a string to add CSS to your PDF. Note that browserless can't asses any url refering to your local environment. Therefore, in your local environment pass the CSS as a string.
|
65
|
+
|
66
|
+
```rb
|
67
|
+
def css_asset
|
68
|
+
if Rails.env.production?
|
69
|
+
ActionController::Base.helpers.asset_path("application.css")
|
70
|
+
else
|
71
|
+
File.read(Rails.root.join("app/assets/builds/application.css"))
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
Browserless.configure do |config|
|
76
|
+
# ...
|
77
|
+
config.style_tag = css_asset
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
## Usage
|
82
|
+
|
83
|
+
Here's an example of how to use the `Browserless::Client` to generate a PDF from HTML content:
|
84
|
+
|
85
|
+
```rb
|
86
|
+
client = Browserless::Client.new(html: "<html></html>")
|
87
|
+
pdf_data = client.to_pdf
|
88
|
+
```
|
89
|
+
|
90
|
+
You can customize the PDF generation by passing options. Passed in options will overwrite options set in the intializer.
|
91
|
+
|
92
|
+
```rb
|
93
|
+
client = Browserless::Client.new(
|
94
|
+
html: "<html>...</html>",
|
95
|
+
emulate_media: "print",
|
96
|
+
options: {
|
97
|
+
landscape: true
|
98
|
+
display_header_footer: true
|
99
|
+
}
|
100
|
+
)
|
101
|
+
pdf_data = client.to_pdf
|
102
|
+
```
|
103
|
+
|
104
|
+
You can pass the following options:
|
105
|
+
|
106
|
+
- `landscape` default false
|
107
|
+
- `print_background` default false
|
108
|
+
- `margin` default none
|
109
|
+
- `format` default A4
|
110
|
+
- `header_template` default empty
|
111
|
+
- `footer_template`default X of X page
|
112
|
+
- `display_header_footer` default true
|
113
|
+
|
114
|
+
Note that the above options are nested in an options hash.
|
115
|
+
|
116
|
+
## Rails example
|
117
|
+
|
118
|
+
```rb
|
119
|
+
class Policy
|
120
|
+
# ...
|
121
|
+
def to_pdf
|
122
|
+
html = ApplicationController.render(
|
123
|
+
partial: "policies/document",
|
124
|
+
locals: { policy: policy}
|
125
|
+
)
|
126
|
+
|
127
|
+
Browserless::Client.new(
|
128
|
+
html: html,
|
129
|
+
options: {
|
130
|
+
display_header_footer: true
|
131
|
+
}
|
132
|
+
).to_pdf
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
class PoliciesController
|
137
|
+
# ...
|
138
|
+
def show
|
139
|
+
# ...
|
140
|
+
respond_to do |format|
|
141
|
+
format.pdf {
|
142
|
+
send_data @policy.to_pdf, type: "application/pdf", disposition: "attachment", filename: "privacy_policy.pdf"
|
143
|
+
}
|
144
|
+
end
|
145
|
+
end
|
146
|
+
```
|
147
|
+
|
148
|
+
In your view::
|
149
|
+
```rb
|
150
|
+
link_to "Download", policy_path(@policy, format: :pdf)
|
151
|
+
```
|
152
|
+
|
153
|
+
## Development
|
154
|
+
|
155
|
+
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
156
|
+
|
157
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
158
|
+
|
159
|
+
## Contributing
|
160
|
+
|
161
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/thomasvanholder/browserless. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/thomasvanholder/browserless/blob/master/CODE_OF_CONDUCT.md).
|
162
|
+
|
163
|
+
## License
|
164
|
+
|
165
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
166
|
+
|
167
|
+
## Code of Conduct
|
168
|
+
|
169
|
+
Everyone interacting in the Browserless project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/thomasvanholder/browserless/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "bundler/gem_tasks"
|
4
|
+
require "standard/rake"
|
5
|
+
require "rake/testtask"
|
6
|
+
|
7
|
+
task default: :standard
|
8
|
+
|
9
|
+
Rake::TestTask.new do |test|
|
10
|
+
test.libs << "test"
|
11
|
+
test.test_files = FileList["test/**/*_test.rb"]
|
12
|
+
test.warning = false
|
13
|
+
end
|
14
|
+
desc "Run tests"
|
15
|
+
task default: :test
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "options"
|
4
|
+
require_relative "style_tag"
|
5
|
+
|
6
|
+
module Browserless
|
7
|
+
class ApikeyError < StandardError; end
|
8
|
+
|
9
|
+
class Client
|
10
|
+
attr_reader :html, :url, :style_tag, :emulate_media, :options
|
11
|
+
|
12
|
+
def initialize(html:, options: {}, **kwargs)
|
13
|
+
@html = html
|
14
|
+
@options = Options.new(**options).to_h
|
15
|
+
@style_tag = StyleTag.new(kwargs[:style_tag]).to_h
|
16
|
+
@emulate_media = config_value(:emulate_media, kwargs[:emulate_media]) || "screen"
|
17
|
+
@url = Browserless.configuration.url
|
18
|
+
end
|
19
|
+
|
20
|
+
def to_pdf
|
21
|
+
temp_file = Tempfile.new
|
22
|
+
save_pdf_to_temp_file(temp_file)
|
23
|
+
|
24
|
+
temp_file.read
|
25
|
+
ensure
|
26
|
+
temp_file.close
|
27
|
+
temp_file.unlink
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def save_pdf_to_temp_file(temp_file)
|
33
|
+
File.open(temp_file, "wb") do |file|
|
34
|
+
file.binmode
|
35
|
+
|
36
|
+
HTTParty.post(Browserless.configuration.url, headers: Browserless::Client.headers, body: browserless_options.to_json, stream_body: true) do |fragment|
|
37
|
+
handle_fragment(fragment, file)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def handle_fragment(fragment, file)
|
43
|
+
case fragment.code
|
44
|
+
when 401
|
45
|
+
error = ApikeyError.new("Unauthorized. Please check if you have a valid Browserless API key")
|
46
|
+
raise error
|
47
|
+
when 301, 302
|
48
|
+
print "skip writing for redirect"
|
49
|
+
when 200
|
50
|
+
print "."
|
51
|
+
file.write(fragment)
|
52
|
+
else
|
53
|
+
puts fragment
|
54
|
+
raise StandardError, "Non-success status code while streaming #{fragment.code}"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def browserless_options
|
59
|
+
{
|
60
|
+
html: html,
|
61
|
+
safeMode: safe_mode,
|
62
|
+
emulateMedia: emulate_media,
|
63
|
+
addStyleTag: [style_tag],
|
64
|
+
options: options
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
def safe_mode
|
69
|
+
# Longer pages can crash trigger a "Page Crashed!" error. Safemode on by default.
|
70
|
+
# https://www.browserless.io/docs/pdf
|
71
|
+
true
|
72
|
+
end
|
73
|
+
|
74
|
+
def config_value(key, value)
|
75
|
+
value || Browserless.configuration.send(key)
|
76
|
+
end
|
77
|
+
|
78
|
+
class << self
|
79
|
+
def headers
|
80
|
+
{
|
81
|
+
"Cache-Control": "no-cache",
|
82
|
+
"Content-Type": "application/json"
|
83
|
+
}
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Browserless
|
4
|
+
class ConfigurationError < StandardError; end
|
5
|
+
|
6
|
+
class Configuration
|
7
|
+
attr_writer :api_key
|
8
|
+
attr_accessor :options, :emulate_media, :style_tag
|
9
|
+
|
10
|
+
BASE_URL = "https://chrome.browserless.io/pdf?token="
|
11
|
+
|
12
|
+
def initialize
|
13
|
+
@api_key = nil
|
14
|
+
end
|
15
|
+
|
16
|
+
def api_key
|
17
|
+
return @api_key if @api_key
|
18
|
+
|
19
|
+
error = "Api key missing. Check if you have defined an api key in the config/browserless.rb file."
|
20
|
+
raise ConfigurationError, error
|
21
|
+
end
|
22
|
+
|
23
|
+
def url
|
24
|
+
"#{BASE_URL}#{api_key}"
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Browserless
|
4
|
+
class Options
|
5
|
+
HEADER_TEMPLATE = "<div></div>" # empty header
|
6
|
+
FOOTER_TEMPLATE = "<div style='font-size: 11px; margin-left: 40px; font: Helvetica'><span class='pageNumber'></span> of <span class='totalPages'></span></div>"
|
7
|
+
|
8
|
+
attr_reader :display_header_footer, :margin, :pdf_format, :print_background, :header_template, :footer_template, :options, :landscape
|
9
|
+
|
10
|
+
def initialize(**options)
|
11
|
+
@options = options
|
12
|
+
@landscape = config_value(:landscape) || false
|
13
|
+
@margin = config_value(:margin) || {}
|
14
|
+
@pdf_format = config_value(:format) || "A4"
|
15
|
+
@print_background = config_value(:print_background) || false
|
16
|
+
@header_template = config_value(:header_template) || HEADER_TEMPLATE
|
17
|
+
@footer_template = config_value(:footer_template) || FOOTER_TEMPLATE
|
18
|
+
@display_header_footer = config_value(:display_header_footer) || false
|
19
|
+
end
|
20
|
+
|
21
|
+
def to_h
|
22
|
+
{
|
23
|
+
landscape: landscape,
|
24
|
+
displayHeaderFooter: display_header_footer,
|
25
|
+
printBackground: print_background,
|
26
|
+
margin: margin,
|
27
|
+
format: pdf_format,
|
28
|
+
headerTemplate: header_template,
|
29
|
+
footerTemplate: footer_template
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def config_value(key)
|
36
|
+
return options[key] if Browserless.configuration.options.nil?
|
37
|
+
|
38
|
+
options[key] || Browserless.configuration.options[key]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Browserless
|
4
|
+
class StyleTag
|
5
|
+
attr_reader :style_tag
|
6
|
+
|
7
|
+
def initialize(style_tag = nil)
|
8
|
+
@style_tag = style_tag
|
9
|
+
end
|
10
|
+
|
11
|
+
def to_h
|
12
|
+
return {content: nil} if config_value.nil?
|
13
|
+
|
14
|
+
config_value.start_with?("http") ? {url: config_value} : {content: config_value}
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def config_value
|
20
|
+
style_tag || Browserless.configuration.style_tag
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/browserless.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "json"
|
4
|
+
require "httparty"
|
5
|
+
|
6
|
+
require_relative "browserless/configuration"
|
7
|
+
require_relative "browserless/client"
|
8
|
+
require_relative "browserless/version"
|
9
|
+
|
10
|
+
module Browserless
|
11
|
+
class << self
|
12
|
+
attr_writer :configuration
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configuration
|
16
|
+
@configuration ||= Configuration.new
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.configure
|
20
|
+
yield(configuration)
|
21
|
+
end
|
22
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: browserless
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Thomas Van Holder
|
@@ -90,7 +90,21 @@ email:
|
|
90
90
|
executables: []
|
91
91
|
extensions: []
|
92
92
|
extra_rdoc_files: []
|
93
|
-
files:
|
93
|
+
files:
|
94
|
+
- ".standard.yml"
|
95
|
+
- CHANGELOG.md
|
96
|
+
- CODE_OF_CONDUCT.md
|
97
|
+
- Gemfile
|
98
|
+
- Gemfile.lock
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- lib/browserless.rb
|
103
|
+
- lib/browserless/client.rb
|
104
|
+
- lib/browserless/configuration.rb
|
105
|
+
- lib/browserless/options.rb
|
106
|
+
- lib/browserless/style_tag.rb
|
107
|
+
- lib/browserless/version.rb
|
94
108
|
homepage: https://github.com/thomasvanholder/browserless
|
95
109
|
licenses:
|
96
110
|
- MIT
|