Module: FerrumPdf

Defined in:
lib/ferrum_pdf.rb,
lib/ferrum_pdf/version.rb

Constant Summary collapse

DEFAULT_HEADER_TEMPLATE =
"<div class='date text left'></div><div class='title text center'></div>"
<<~HTML
  <div class='url text left grow'></div>
  <div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
HTML
VERSION =
"3.0.1"
@@browser =

This doesn’t use mattr_accessor because having a ‘.browser` getter and also having local variables named `browser` would be confusing. For simplicity, this is explicitly accessed.

nil

Class Method Summary collapse

Class Method Details

.browser=(browser_instance) ⇒ Object

Sets the browser instance to use for all operations If a browser is already set, it will be shut down before setting the new one



34
35
36
37
38
39
# File 'lib/ferrum_pdf.rb', line 34

def browser=(browser_instance)
  browser_mutex.synchronize do
    @@browser&.quit
    @@browser = browser_instance
  end
end

.configure {|config| ... } ⇒ Object

Yields:

  • (config)


28
29
30
# File 'lib/ferrum_pdf.rb', line 28

def configure
  yield config
end

.render_pdf(pdf_options: {}, **load_page_args) ⇒ Object

Renders HTML or URL to PDF

render_pdf(url: "https://example.org/receipts/example.pdf")
render_pdf(html: "<h1>Hello world</h1>")

For rendering HTML, we also need display_url for so that Chrome can interpret URLs with relative paths & protocols

render_pdf(html: "<h1>Hello world</h1>", display_url: "https://example.org/hello_world")


63
64
65
66
67
68
# File 'lib/ferrum_pdf.rb', line 63

def render_pdf(pdf_options: {}, **load_page_args)
  load_page(**load_page_args) do |browser, page|
    yield browser, page if block_given?
    page.pdf(**pdf_options.with_defaults(encoding: :binary, **config.pdf_options))
  end
end

.render_screenshot(screenshot_options: {}, **load_page_args) ⇒ Object

Renders HTML or URL to Screenshot

render_screenshot(url: “example.org/receipts/example.pdf”) render_screenshot(html: “<h1>Hello world</h1>”)

For rendering HTML, we also need display_url for so that Chrome can interpret URLs with relative paths & protocols

render_screenshot(html: "<h1>Hello world</h1>", display_url: "https://example.org/hello_world")


79
80
81
82
83
84
# File 'lib/ferrum_pdf.rb', line 79

def render_screenshot(screenshot_options: {}, **load_page_args)
  load_page(**load_page_args) do |browser, page|
    yield browser, page if block_given?
    page.screenshot(**screenshot_options.with_defaults(encoding: :binary, full: true, **config.screenshot_options))
  end
end

.with_browser(browser = nil) ⇒ Object

Provides thread-safe access to the browser instance



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/ferrum_pdf.rb', line 42

def with_browser(browser = nil)
  if browser
    yield browser
  else
    browser_mutex.synchronize do
      @@browser ||= Ferrum::Browser.new(config.except(:page_options, :pdf_options, :screenshot_options))
      @@browser.restart unless @@browser.client.present?
      yield @@browser
    end
  end
end