Module: FerrumPdf

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

Constant Summary collapse

DEFAULT_DISPLAY_URL =

Reserved .invalid TLD so relative paths fail fast on DNS instead of hanging against a domain we don't control.

"http://ferrum-pdf.invalid"
DEFAULT_HEADER_TEMPLATE =
"<div class='date text left'></div><div class='title text center'></div>"
"<div class='url text left grow'></div>\n<div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>\n"
VERSION =
"3.2.0"
@@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



37
38
39
40
41
42
# File 'lib/ferrum_pdf.rb', line 37

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

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

Yields:

  • (config)


31
32
33
# File 'lib/ferrum_pdf.rb', line 31

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")


67
68
69
70
71
72
# File 'lib/ferrum_pdf.rb', line 67

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: "https://example.org/receipts/example.pdf&quot;) render_screenshot(html: "

Hello world

")

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")


83
84
85
86
87
88
# File 'lib/ferrum_pdf.rb', line 83

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



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ferrum_pdf.rb', line 45

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