class Selenium::WebDriver::PrintOptions

Represents options for printing a page.

Constants

DEFAULT_MARGINS
DEFAULT_ORIENTATION
DEFAULT_PAGE_SIZE
DEFAULT_SCALE

Attributes

background[RW]
margins[RW]
orientation[RW]
page_ranges[RW]
page_size[R]

Gets the current page size.

@return [Hash] The current page size hash with :width and :height.

scale[RW]

Public Class Methods

new() click to toggle source
# File lib/selenium/webdriver/common/print_options.rb, line 31
def initialize
  @orientation = DEFAULT_ORIENTATION
  @scale = DEFAULT_SCALE
  @background = false
  @page_ranges = nil
  @page_size = DEFAULT_PAGE_SIZE
  @margins = DEFAULT_MARGINS
end

Public Instance Methods

page_size=(value) click to toggle source

Sets the page size. Can be a predefined symbol or custom size hash.

@param [Symbol, Hash] value The predefined size (:letter, :legal, :a4, :tabloid) or a custom hash.

# File lib/selenium/webdriver/common/print_options.rb, line 68
def page_size=(value)
  predefined_sizes = {
    letter: {width: 21.59, height: 27.94},
    legal: {width: 21.59, height: 35.56},
    a4: {width: 21.0, height: 29.7},
    tabloid: {width: 27.94, height: 43.18}
  }

  case value
  when Symbol
    raise ArgumentError, "Invalid page size: #{value}" unless predefined_sizes.key?(value)

    @page_size = predefined_sizes[value]
  when Hash
    unless value.key?(:width) && value.key?(:height)
      raise ArgumentError, 'Custom page size must include :width and :height'
    end

    @page_size = value
  else
    raise ArgumentError, 'Page size must be a Symbol or a Hash'
  end
end
to_h() click to toggle source

Converts the options to a hash format to be used by WebDriver.

@return [Hash]

# File lib/selenium/webdriver/common/print_options.rb, line 43
def to_h
  options = {
    orientation: @orientation,
    scale: @scale,
    background: @background,
    pageRanges: @page_ranges,
    paperWidth: @page_size[:width],
    paperHeight: @page_size[:height],
    marginTop: @margins[:top],
    marginBottom: @margins[:bottom],
    marginLeft: @margins[:left],
    marginRight: @margins[:right]
  }

  options.compact
end