From ec87cdec1a60a65fa8f85c18a50359f9082a2d46 Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Wed, 4 Jun 2025 10:17:58 -0400 Subject: [PATCH 1/4] Declare httparty dependency If we're gonna party hard, let's invite everyone. --- Gemfile.lock | 109 +++++++++++++++++++++++++++++--------------- browserless.gemspec | 2 +- 2 files changed, 73 insertions(+), 38 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 7fe56c4..8a80570 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/browserless.gemspec b/browserless.gemspec index 6f75061..7bef55f 100644 --- a/browserless.gemspec +++ b/browserless.gemspec @@ -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 From 2787e0b01699fe203b703f1ee22293d0f66f59fe Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Wed, 4 Jun 2025 10:21:27 -0400 Subject: [PATCH 2/4] Update host and make it configurable --- lib/browserless/configuration.rb | 7 +++---- test/models/configuration_test.rb | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/browserless/configuration.rb b/lib/browserless/configuration.rb index b9b9dc3..120bd92 100644 --- a/lib/browserless/configuration.rb +++ b/lib/browserless/configuration.rb @@ -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, :style_tag def initialize @api_key = nil + @host = "production-sfo.browserless.io" end def api_key @@ -21,7 +20,7 @@ def api_key end def url - "#{BASE_URL}#{api_key}" + "https://#{host}/pdf?token=#{api_key}" end end end diff --git a/test/models/configuration_test.rb b/test/models/configuration_test.rb index c5bd07b..3ad1621 100644 --- a/test/models/configuration_test.rb +++ b/test/models/configuration_test.rb @@ -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 From ea78cadfc25a96a406b72862e0dbc7f05fc798d3 Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Wed, 4 Jun 2025 10:29:18 -0400 Subject: [PATCH 3/4] Update default options * safeMode is no longer available. * emulateMedia changed to emulateMediaType. Rename it for consistency. * add a waitUntil that should be helpful for most people. --- README.md | 6 +++--- lib/browserless/client.rb | 15 +++++---------- lib/browserless/configuration.rb | 2 +- test/models/client_test.rb | 6 +++--- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 4b906af..16a5a5b 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/lib/browserless/client.rb b/lib/browserless/client.rb index d2203df..087035c 100644 --- a/lib/browserless/client.rb +++ b/lib/browserless/client.rb @@ -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 @@ -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 diff --git a/lib/browserless/configuration.rb b/lib/browserless/configuration.rb index 120bd92..d122910 100644 --- a/lib/browserless/configuration.rb +++ b/lib/browserless/configuration.rb @@ -5,7 +5,7 @@ class ConfigurationError < StandardError; end class Configuration attr_writer :api_key - attr_accessor :host, :options, :emulate_media, :style_tag + attr_accessor :host, :options, :emulate_media_type, :style_tag def initialize @api_key = nil diff --git a/test/models/client_test.rb b/test/models/client_test.rb index 2051722..1273ea7 100644 --- a/test/models/client_test.rb +++ b/test/models/client_test.rb @@ -23,7 +23,7 @@ def test_initialize client = Browserless::Client.new(html: "") assert_equal "", 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 @@ -50,12 +50,12 @@ def test_initialize_with_custom_options client = Browserless::Client.new( html: "", - emulate_media: "print", + emulate_media_type: "print", options: {display_header_footer: true} ) assert_equal "", client.html - assert_equal "print", client.emulate_media + assert_equal "print", client.emulate_media_type assert_equal({ landscape: false, displayHeaderFooter: true, From f0f3599e459978a808ad28791e321ce0097d9b53 Mon Sep 17 00:00:00 2001 From: Daniel Morrison Date: Wed, 4 Jun 2025 10:36:28 -0400 Subject: [PATCH 4/4] Add link to docs in README --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 16a5a5b..7b84354 100644 --- a/README.md +++ b/README.md @@ -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: "...",