Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 72 additions & 37 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,63 +1,98 @@
PATH
remote: .
specs:
browserless (1.0.1)
browserless (1.0.2)
httparty (>= 0.18)

GEM
remote: https://rubygems.org/
specs:
ansi (1.5.0)
ast (2.4.2)
builder (3.2.4)
debug (1.7.2)
irb (>= 1.5.0)
reline (>= 0.3.1)
httparty (0.21.0)
ast (2.4.3)
bigdecimal (3.2.1)
builder (3.3.0)
csv (3.3.5)
date (3.4.1)
debug (1.10.0)
irb (~> 1.10)
reline (>= 0.3.8)
erb (5.0.1)
httparty (0.23.1)
csv
mini_mime (>= 1.0.0)
multi_xml (>= 0.5.2)
io-console (0.6.0)
irb (1.6.3)
reline (>= 0.3.0)
json (2.6.3)
language_server-protocol (3.17.0.3)
mini_mime (1.1.2)
minitest (5.18.0)
minitest-reporters (1.6.0)
io-console (0.8.0)
irb (1.15.2)
pp (>= 0.6.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
json (2.12.2)
language_server-protocol (3.17.0.5)
lint_roller (1.1.0)
mini_mime (1.1.5)
minitest (5.25.5)
minitest-reporters (1.7.1)
ansi
builder
minitest (>= 5.0)
ruby-progressbar
multi_xml (0.6.0)
parallel (1.22.1)
parser (3.2.2.0)
multi_xml (0.7.2)
bigdecimal (~> 3.1)
parallel (1.27.0)
parser (3.3.8.0)
ast (~> 2.4.1)
racc
pp (0.6.2)
prettyprint
prettyprint (0.2.0)
prism (1.4.0)
psych (5.2.6)
date
stringio
racc (1.8.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.7.0)
reline (0.3.3)
rake (13.3.0)
rdoc (6.14.0)
erb
psych (>= 4.0.0)
regexp_parser (2.10.0)
reline (0.6.1)
io-console (~> 0.5)
rexml (3.2.5)
rubocop (1.48.1)
rubocop (1.75.8)
json (~> 2.3)
language_server-protocol (~> 3.17.0.2)
lint_roller (~> 1.1.0)
parallel (~> 1.10)
parser (>= 3.2.0.0)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.26.0, < 2.0)
regexp_parser (>= 2.9.3, < 3.0)
rubocop-ast (>= 1.44.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.28.0)
parser (>= 3.2.1.0)
rubocop-performance (1.16.0)
rubocop (>= 1.7.0, < 2.0)
rubocop-ast (>= 0.4.0)
unicode-display_width (>= 2.4.0, < 4.0)
rubocop-ast (1.45.0)
parser (>= 3.3.7.2)
prism (~> 1.4)
rubocop-performance (1.25.0)
lint_roller (~> 1.1)
rubocop (>= 1.75.0, < 2.0)
rubocop-ast (>= 1.38.0, < 2.0)
ruby-progressbar (1.13.0)
standard (1.26.0)
standard (1.50.0)
language_server-protocol (~> 3.17.0.2)
rubocop (~> 1.48.1)
rubocop-performance (~> 1.16.0)
unicode-display_width (2.4.2)
lint_roller (~> 1.0)
rubocop (~> 1.75.5)
standard-custom (~> 1.0.0)
standard-performance (~> 1.8)
standard-custom (1.0.2)
lint_roller (~> 1.0)
rubocop (~> 1.50)
standard-performance (1.8.0)
lint_roller (~> 1.1)
rubocop-performance (~> 1.25.0)
stringio (3.1.7)
unicode-display_width (3.1.4)
unicode-emoji (~> 4.0, >= 4.0.4)
unicode-emoji (4.0.4)

PLATFORMS
arm64-darwin-22
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Add your Browserless.io API key in an initializer file, like `config/browserless
```rb
Browserless.configure do |config|
config.api_key = "your_api_key_here"
config.emulate_media = "print" # choose between print or screen (default)
config.emulate_media_type = "print" # choose between print or screen (default)
config.style_tag = File.read(Rails.root.join("app/assets/builds/application.css")) # Pass public asset URL or CSS string content
config.options = {
landscape: false # default
Expand All @@ -57,8 +57,8 @@ end
__api_key__
Make sure to replace `"your_api_key_here"` with your actual Browserless.io API key.

__emulate_media__
You can specify the media type by passing in the optional `emulate_media` keyword argument. Choose between `screen` (default) or `print`.
__emulate_media_type__
You can specify the media type by passing in the optional `emulate_media_type` keyword argument. Choose between `screen` (default) or `print`.

>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.

Expand Down Expand Up @@ -91,6 +91,8 @@ pdf_data = client.to_pdf

You can customize the PDF generation by passing options. Passed in options will overwrite options set in the intializer.

See: https://docs.browserless.io/open-api#tag/Browser-REST-APIs/paths/~1chrome~1pdf/post for the full list.

```rb
client = Browserless::Client.new(
html: "<html>...</html>",
Expand Down
2 changes: 1 addition & 1 deletion browserless.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "debug", "~> 1.7.2"
spec.add_development_dependency "standard", "~> 1.26.0"

# spec.add_dependency "httpary", "~> 0.18"
spec.add_dependency "httparty", ">= 0.18"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
15 changes: 5 additions & 10 deletions lib/browserless/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ module Browserless
class ApikeyError < StandardError; end

class Client
attr_reader :html, :url, :style_tag, :emulate_media, :options
attr_reader :html, :url, :style_tag, :emulate_media_type, :goto_options, :options

def initialize(html:, options: {}, **kwargs)
@html = html
@options = Options.new(**options).to_h
@style_tag = StyleTag.new(kwargs[:style_tag]).to_h
@emulate_media = config_value(:emulate_media, kwargs[:emulate_media]) || "screen"
@emulate_media_type = config_value(:emulate_media_type, kwargs[:emulate_media_type]) || "screen"
@goto_options = kwargs[:goto_options] || {waitUntil: "networkidle2"}
@url = Browserless.configuration.url
end

Expand Down Expand Up @@ -58,19 +59,13 @@ def handle_fragment(fragment, file)
def browserless_options
{
html: html,
safeMode: safe_mode,
emulateMedia: emulate_media,
emulateMediaType: emulate_media_type,
addStyleTag: [style_tag],
gotoOptions: @goto_options,
options: options
}
end

def safe_mode
# Longer pages can crash trigger a "Page Crashed!" error. Safemode on by default.
# https://www.browserless.io/docs/pdf
true
end

def config_value(key, value)
value || Browserless.configuration.send(key)
end
Expand Down
7 changes: 3 additions & 4 deletions lib/browserless/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ class ConfigurationError < StandardError; end

class Configuration
attr_writer :api_key
attr_accessor :options, :emulate_media, :style_tag

BASE_URL = "https://chrome.browserless.io/pdf?token="
attr_accessor :host, :options, :emulate_media_type, :style_tag

def initialize
@api_key = nil
@host = "production-sfo.browserless.io"
end

def api_key
Expand All @@ -21,7 +20,7 @@ def api_key
end

def url
"#{BASE_URL}#{api_key}"
"https://#{host}/pdf?token=#{api_key}"
end
end
end
6 changes: 3 additions & 3 deletions test/models/client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_initialize
client = Browserless::Client.new(html: "<html></html>")

assert_equal "<html></html>", client.html
assert_equal "screen", client.emulate_media
assert_equal "screen", client.emulate_media_type
assert_equal({content: nil}, client.style_tag)
assert_equal Browserless.configuration.url, client.url
end
Expand All @@ -50,12 +50,12 @@ def test_initialize_with_custom_options

client = Browserless::Client.new(
html: "<html></html>",
emulate_media: "print",
emulate_media_type: "print",
options: {display_header_footer: true}
)

assert_equal "<html></html>", client.html
assert_equal "print", client.emulate_media
assert_equal "print", client.emulate_media_type
assert_equal({
landscape: false,
displayHeaderFooter: true,
Expand Down
2 changes: 1 addition & 1 deletion test/models/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ def test_options_accessor
def test_url_with_api_key
@configuration.api_key = "test_key"

assert_equal "https://chrome.browserless.io/pdf?token=test_key", @configuration.url
assert_equal "https://production-sfo.browserless.io/pdf?token=test_key", @configuration.url
end
end