ferrum_pdf 2.1.0 → 3.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e10a66bff6f06cfd7388c12247ca55e92a79bc48ff741e889e0ac0bfde0286e0
4
- data.tar.gz: 2a92a79c5a7ca34091644fcd5795d1d1c84d49f7d834f02afd6d9ec462ba892b
3
+ metadata.gz: a0c9bdbd12753b4efc5b29bb208908735218fc2614e45eb74e57b6623ca61688
4
+ data.tar.gz: cb96c86e8c45a34037db93555c9ac5d692f7ccc4d0f37c88e211e6dcb17f7aa1
5
5
  SHA512:
6
- metadata.gz: c6d4472cb10faebb29ab64634f16063c7aac7927baf76e3e58dc72478d3d21819935138e6595a872f9e0766ff51a17303cad51a1857d62110b2e13fd4877234f
7
- data.tar.gz: 9915cadbed613d26c113a57d5a23268ec3758ef9f566b5f6f387469ed4cce948d5348173a840cf9d36a44d935fda1a3e5f7cb06af5c5f042d73f2fb06707a892
6
+ metadata.gz: b3e3efc16d9c2601e6ef725d9d1aa6f73d631c8dcfaff8a4aa76a2254e933d7205e33f00a98ac780c7cf0b99fa074e2d820abee42de372fb68966a00fb533faa
7
+ data.tar.gz: 292b03680170140e5e901f8e84128970cb88a17e17147b57142ad686edca9c31da24c327e8c733505e6188a50310cac7d112a3ce1f92f89ed50b301ec7a0cd9b
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # 📄 FerrumPdf
2
2
 
3
- PDFs & screentshots for Rails using [Ferrum](https://github.com/rubycdp/ferrum) & headless Chrome.
3
+ PDFs & screenshots for Rails using [Ferrum](https://github.com/rubycdp/ferrum) & headless Chrome.
4
4
 
5
5
  Inspired by [Grover](https://github.com/Studiosity/grover), but without the Node.js and puppeteer dependencies. 🎉
6
6
 
@@ -70,10 +70,11 @@ FerrumPdf.render_pdf(
70
70
  url: "https://example.com/page", # Provide a URL to the content
71
71
 
72
72
  html: content, # or provide HTML
73
- base_url: request.base_url, # Preprocesses `html` to convert relative paths and protocols. Example: "https://example.org"
73
+ display_url: request.original_url, # When supplying content via :html its best to give Chrome a hint of the current url so that it can process relative paths in the document. If you don't provide this, http://example.com will be used instead.
74
74
 
75
75
  authorize: { user: "username", password: "password" }, # Used for authenticating with basic auth
76
76
  wait_for_idle_options: { connections: 0, duration: 0.05, timeout: 5 }, # Used for setting network wait_for_idle options
77
+ timeout_if_open_connections: true,
77
78
 
78
79
  pdf_options: {
79
80
  landscape: false, # paper orientation
@@ -166,7 +167,7 @@ FerrumPdf.render_screenshot(
166
167
  url: "https://example.com/page", # Provide a URL to the content
167
168
 
168
169
  html: content, # or provide HTML
169
- base_url: request.base_url, # Preprocesses `html` to convert relative paths and protocols. Example: "https://example.org"
170
+ display_url: request.original_url, # When supplying content via :html its best to give Chrome a hint of the current url so that it can process relative paths in the document. If you don't provide this, http://example.com will be used instead.
170
171
 
171
172
  screenshot_options: {
172
173
  format: "png" # or "jpeg"
@@ -186,7 +187,6 @@ You can set default values for page loads, PDF renders, and screenshot renders w
186
187
 
187
188
  ```ruby
188
189
  FerrumPdf.configure do |config|
189
- config.page_options.base_url = "https://example.com/"
190
190
  config.page_options.authorize = { user: "username", password: "password" }
191
191
  config.page_options.wait_for_idle_options = { timeout: 90 }
192
192
  config.page_options.retries = 3
@@ -1,11 +1,5 @@
1
1
  module FerrumPdf
2
2
  class Railtie < ::Rails::Railtie
3
- initializer "ferrum_pdf.assets_helper" do
4
- ActiveSupport.on_load(:action_view) do
5
- include FerrumPdf::AssetsHelper
6
- end
7
- end
8
-
9
3
  initializer "ferrum_pdf.controller" do
10
4
  ActiveSupport.on_load(:action_controller) do
11
5
  # render ferrum_pdf: { pdf options }, template: "whatever", disposition: :inline, filename: "example.pdf"
@@ -13,7 +7,7 @@ module FerrumPdf
13
7
  send_data_options = options.extract!(:disposition, :filename, :status)
14
8
  url = pdf_options.delete(:url)
15
9
  html = render_to_string(**options.with_defaults(formats: [ :html ])) if url.blank?
16
- pdf = FerrumPdf.render_pdf(html: html, base_url: request.base_url, url: url, pdf_options: pdf_options)
10
+ pdf = FerrumPdf.render_pdf(html: html, display_url: request.original_url, url: url, pdf_options: pdf_options)
17
11
  send_data(pdf, **send_data_options.with_defaults(type: :pdf))
18
12
  end
19
13
 
@@ -22,7 +16,7 @@ module FerrumPdf
22
16
  send_data_options = options.extract!(:disposition, :filename, :status)
23
17
  url = screenshot_options.delete(:url)
24
18
  html = render_to_string(**options.with_defaults(formats: [ :html ])) if url.blank?
25
- screenshot = FerrumPdf.render_screenshot(url: url, html: html, base_url: request.base_url, screenshot_options: screenshot_options)
19
+ screenshot = FerrumPdf.render_screenshot(url: url, html: html, display_url: request.original_url, screenshot_options: screenshot_options)
26
20
  send_data(screenshot, **send_data_options.with_defaults(type: screenshot_options.fetch(:format, :png)))
27
21
  end
28
22
  end
@@ -1,3 +1,3 @@
1
1
  module FerrumPdf
2
- VERSION = "2.1.0"
2
+ VERSION = "3.0.0"
3
3
  end
data/lib/ferrum_pdf.rb CHANGED
@@ -9,9 +9,6 @@ module FerrumPdf
9
9
  <div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
10
10
  HTML
11
11
 
12
- autoload :AssetsHelper, "ferrum_pdf/assets_helper"
13
- autoload :HTMLPreprocessor, "ferrum_pdf/html_preprocessor"
14
-
15
12
  mattr_accessor :browser_mutex, default: Mutex.new
16
13
  mattr_accessor :config, default: ActiveSupport::OrderedOptions.new.merge(
17
14
  window_size: [ 1920, 1080 ],
@@ -57,9 +54,9 @@ module FerrumPdf
57
54
  # render_pdf(url: "https://example.org/receipts/example.pdf")
58
55
  # render_pdf(html: "<h1>Hello world</h1>")
59
56
  #
60
- # For rendering HTML, we also need the base_url for preprocessing URLs with relative paths & protocols
57
+ # For rendering HTML, we also need display_url for so that Chrome can interpret URLs with relative paths & protocols
61
58
  #
62
- # render_pdf(html: "<h1>Hello world</h1>", base_url: "https://example.org/")
59
+ # render_pdf(html: "<h1>Hello world</h1>", display_url: "https://example.org/hello_world")
63
60
  #
64
61
  def render_pdf(pdf_options: {}, **load_page_args)
65
62
  load_page(**load_page_args) do |browser, page|
@@ -73,9 +70,9 @@ module FerrumPdf
73
70
  # render_screenshot(url: "https://example.org/receipts/example.pdf")
74
71
  # render_screenshot(html: "<h1>Hello world</h1>")
75
72
  #
76
- # For rendering HTML, we also need the base_url for preprocessing URLs with relative paths & protocols
73
+ # For rendering HTML, we also need display_url for so that Chrome can interpret URLs with relative paths & protocols
77
74
  #
78
- # render_screenshot(html: "<h1>Hello world</h1>", base_url: "https://example.org/")
75
+ # render_screenshot(html: "<h1>Hello world</h1>", display_url: "https://example.org/hello_world")
79
76
  #
80
77
  def render_screenshot(screenshot_options: {}, **load_page_args)
81
78
  load_page(**load_page_args) do |browser, page|
@@ -86,14 +83,12 @@ module FerrumPdf
86
83
 
87
84
  # Loads page into the browser to be used for rendering PDFs or screenshots
88
85
  #
89
- # This automatically applies HTML preprocessing if `html:` is present
90
- #
91
- def load_page(url: nil, html: nil, base_url: nil, authorize: nil, wait_for_idle_options: nil, browser: nil, retries: nil)
86
+ def load_page(url: nil, html: nil, display_url: nil, authorize: nil, wait_for_idle_options: nil, timeout_if_open_connections: true, browser: nil, retries: nil)
92
87
  try ||= 0
93
88
  authorize ||= config.dig(:page_options, :authorize)
94
- base_url ||= config.dig(:page_options, :base_url)
95
89
  retries ||= config.page_options.fetch(:retries, 1)
96
90
  wait_for_idle_options = config.page_options.fetch(:wait_for_idle_options, {}).merge(wait_for_idle_options || {})
91
+ timeout_if_open_connections ||= config.page_options.fetch(:timeout_if_open_connections, true)
97
92
 
98
93
  with_browser(browser) do |browser|
99
94
  # Closes page automatically after block finishes
@@ -103,13 +98,24 @@ module FerrumPdf
103
98
 
104
99
  # Load content
105
100
  if html
106
- page.content = FerrumPdf::HTMLPreprocessor.process(html, base_url)
101
+ html_intercepted = false
102
+ page.network.intercept
103
+ page.on(:request) do |request|
104
+ if html_intercepted
105
+ request.continue
106
+ else
107
+ html_intercepted = true
108
+ request.respond(body: html.blank? ? " " : html)
109
+ end
110
+ end
111
+ page.go_to(display_url || "http://example.com")
107
112
  else
108
113
  page.go_to(url)
109
114
  end
110
115
 
111
116
  # Wait for everything to load
112
- page.network.wait_for_idle!(**wait_for_idle_options)
117
+ idle = page.network.wait_for_idle(**wait_for_idle_options)
118
+ raise Ferrum::TimeoutError if timeout_if_open_connections && !idle
113
119
 
114
120
  yield browser, page
115
121
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferrum_pdf
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Oliver
@@ -49,8 +49,6 @@ files:
49
49
  - README.md
50
50
  - Rakefile
51
51
  - lib/ferrum_pdf.rb
52
- - lib/ferrum_pdf/assets_helper.rb
53
- - lib/ferrum_pdf/html_preprocessor.rb
54
52
  - lib/ferrum_pdf/railtie.rb
55
53
  - lib/ferrum_pdf/version.rb
56
54
  homepage: https://github.com/excid3/ferrum_pdf
@@ -74,7 +72,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
74
72
  - !ruby/object:Gem::Version
75
73
  version: '0'
76
74
  requirements: []
77
- rubygems_version: 3.7.1
75
+ rubygems_version: 3.7.2
78
76
  specification_version: 4
79
77
  summary: PDFs & screenshots for Rails using Ferrum & headless Chrome
80
78
  test_files: []
@@ -1,65 +0,0 @@
1
- module FerrumPdf
2
- class BaseAsset
3
- def initialize(asset)
4
- @asset = asset
5
- end
6
- end
7
-
8
- class PropshaftAsset < BaseAsset
9
- def content_type
10
- @asset.content_type.to_s
11
- end
12
-
13
- def content
14
- @asset.content
15
- end
16
- end
17
-
18
- class SprocketsAsset < BaseAsset
19
- def content_type
20
- @asset.content_type
21
- end
22
-
23
- def content
24
- @asset.source
25
- end
26
- end
27
-
28
- class AssetFinder
29
- class << self
30
- def find(path)
31
- if Rails.application.assets.respond_to?(:load_path)
32
- propshaft_asset(path)
33
- elsif Rails.application.assets.respond_to?(:find_asset)
34
- sprockets_asset(path)
35
- else
36
- nil
37
- end
38
- end
39
-
40
- def propshaft_asset(path)
41
- (asset = Rails.application.assets.load_path.find(path)) ? PropshaftAsset.new(asset) : nil
42
- end
43
-
44
- def sprockets_asset(path)
45
- (asset = Rails.application.assets.find_asset(path)) ? SprocketsAsset.new(asset) : nil
46
- end
47
- end
48
- end
49
-
50
- module AssetsHelper
51
- def ferrum_pdf_inline_stylesheet(path)
52
- (asset = AssetFinder.find(path)) ? "<style>#{asset.content}</style>".html_safe : nil
53
- end
54
-
55
- def ferrum_pdf_inline_javascript(path)
56
- (asset = AssetFinder.find(path)) ? "<script>#{asset.content}</script>".html_safe : nil
57
- end
58
-
59
- def ferrum_pdf_base64_asset(path)
60
- return nil unless (asset = AssetFinder.find(path))
61
-
62
- "data:#{asset.content_type};base64,#{Base64.encode64(asset.content).gsub(/\s+/, '')}"
63
- end
64
- end
65
- end
@@ -1,33 +0,0 @@
1
- module FerrumPdf
2
- # Helper module for preparing HTML for conversion
3
- #
4
- # Sourced from the PDFKit project
5
- # @see https://github.com/pdfkit/pdfkit
6
- module HTMLPreprocessor
7
- # Change relative paths to absolute, and relative protocols to absolute protocols
8
- #
9
- # process("Some HTML", "https://example.org")
10
- #
11
- def self.process(html, base_url)
12
- return html if base_url.blank?
13
-
14
- base_url += "/" unless base_url.end_with? "/"
15
- protocol = base_url.split("://").first
16
- html = translate_relative_paths(html, base_url) if base_url
17
- html = translate_relative_protocols(html, protocol) if protocol
18
- html
19
- end
20
-
21
- def self.translate_relative_paths(html, base_url)
22
- # Try out this regexp using rubular http://rubular.com/r/hiAxBNX7KE
23
- html.gsub(%r{(href|src)=(['"])/([^/"']([^"']*|[^"']*))?['"]}, "\\1=\\2#{base_url}\\3\\2")
24
- end
25
- private_class_method :translate_relative_paths
26
-
27
- def self.translate_relative_protocols(body, protocol)
28
- # Try out this regexp using rubular http://rubular.com/r/0Ohk0wFYxV
29
- body.gsub(%r{(href|src)=(['"])//([^"']*|[^"']*)['"]}, "\\1=\\2#{protocol}://\\3\\2")
30
- end
31
- private_class_method :translate_relative_protocols
32
- end
33
- end