|
| 1 | +# <copyright file="print_options.rb" company="Selenium Committers"> |
| 2 | +# Licensed to the Software Freedom Conservancy (SFC) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The SFC licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# </copyright> |
| 19 | + |
| 20 | +module Selenium |
| 21 | + module WebDriver |
| 22 | + # Represents options for printing a page. |
| 23 | + class PrintOptions |
| 24 | + DEFAULT_SCALE = 1.0 |
| 25 | + DEFAULT_ORIENTATION = 'portrait'.freeze |
| 26 | + DEFAULT_PAGE_SIZE = {width: 21.0, height: 29.7}.freeze # A4 size in cm |
| 27 | + DEFAULT_MARGINS = {top: 1.0, bottom: 1.0, left: 1.0, right: 1.0}.freeze |
| 28 | + |
| 29 | + attr_accessor :orientation, :scale, :background, :page_ranges, :page_size, :margins |
| 30 | + |
| 31 | + def initialize |
| 32 | + @orientation = DEFAULT_ORIENTATION |
| 33 | + @scale = DEFAULT_SCALE |
| 34 | + @background = false |
| 35 | + @page_ranges = nil |
| 36 | + @page_size = DEFAULT_PAGE_SIZE |
| 37 | + @margins = DEFAULT_MARGINS |
| 38 | + end |
| 39 | + |
| 40 | + # Converts the options to a hash format to be used by WebDriver. |
| 41 | + # |
| 42 | + # @return [Hash] |
| 43 | + def to_h |
| 44 | + options = { |
| 45 | + orientation: @orientation, |
| 46 | + scale: @scale, |
| 47 | + background: @background, |
| 48 | + pageRanges: @page_ranges, |
| 49 | + paperWidth: @page_size[:width], |
| 50 | + paperHeight: @page_size[:height], |
| 51 | + marginTop: @margins[:top], |
| 52 | + marginBottom: @margins[:bottom], |
| 53 | + marginLeft: @margins[:left], |
| 54 | + marginRight: @margins[:right] |
| 55 | + } |
| 56 | + |
| 57 | + options.compact |
| 58 | + end |
| 59 | + |
| 60 | + # Sets the page size to a predefined size. |
| 61 | + # |
| 62 | + # @param [Symbol] size The predefined size (:letter, :legal, :a4, :tabloid). |
| 63 | + def set_page_size(size) |
| 64 | + predefined_sizes = { |
| 65 | + letter: {width: 21.59, height: 27.94}, |
| 66 | + legal: {width: 21.59, height: 35.56}, |
| 67 | + a4: {width: 21.0, height: 29.7}, |
| 68 | + tabloid: {width: 27.94, height: 43.18} |
| 69 | + } |
| 70 | + |
| 71 | + raise ArgumentError, "Invalid page size: #{size}" unless predefined_sizes.key?(size) |
| 72 | + |
| 73 | + @page_size = predefined_sizes[size] |
| 74 | + end |
| 75 | + end |
| 76 | + end |
| 77 | +end |
0 commit comments