Class: Puppeteer::Page::PDFOptions

Inherits:
Object
  • Object
show all
Defined in:
lib/puppeteer/page/pdf_options.rb

Overview

/**

  • @typedef Object PDFOptions

  • @property number= scale

  • @property boolean= displayHeaderFooter

  • @property string= headerTemplate

  • @property string= footerTemplate

  • @property boolean= printBackground

  • @property boolean= landscape

  • @property string= pageRanges

  • @property string= format

  • @property string|number= width

  • @property string|number= height

  • @property boolean= preferCSSPageSize

  • @property string|number, bottom?: string|number, left?: string|number, right?: string|number=} margin

  • @property string= path

*/

Defined Under Namespace

Classes: Margin, PaperSize

Constant Summary collapse

PAPER_FORMATS =
{
  letter: PaperSize.new(width: 8.5, height: 11),
  legal: PaperSize.new(width: 8.5, height: 14),
  tabloid: PaperSize.new(width: 11, height: 17),
  ledger: PaperSize.new(width: 17, height: 11),
  a0: PaperSize.new(width: 33.1, height: 46.8),
  a1: PaperSize.new(width: 23.4, height: 33.1),
  a2: PaperSize.new(width: 16.54, height: 23.4),
  a3: PaperSize.new(width: 11.7, height: 16.54),
  a4: PaperSize.new(width: 8.27, height: 11.7),
  a5: PaperSize.new(width: 5.83, height: 8.27),
  a6: PaperSize.new(width: 4.13, height: 5.83),
}
UNIT_TO_PIXELS =
{
  px: 1,
  in: 96,
  cm: 37.8,
  mm: 3.78,
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ PDFOptions

Returns a new instance of PDFOptions.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/puppeteer/page/pdf_options.rb', line 22

def initialize(options)
  unless options[:path]
    # Original puppeteer allows path = nil, however nothing to do without path actually.
    # Also in most case, users forget to specify path parameter. So let's raise ArgumentError.
    raise ArgumentError('"path" parameter must be specified.')
  end

  @scale = options[:scale]
  @display_header_footer = options[:display_header_footer]
  @header_template = options[:header_template]
  @footer_template = options[:footer_template]
  @print_background = options[:print_background]
  @landscape = options[:landscape]
  @page_ranges = options[:page_ranges]
  @format = options[:format]
  @width = options[:width]
  @height = options[:height]
  @prefer_css_page_size = options[:prefer_css_page_size]
  @margin = Margin.new(options[:margin] || {})
  @path = options[:path]
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



44
45
46
# File 'lib/puppeteer/page/pdf_options.rb', line 44

def path
  @path
end

Instance Method Details

#page_print_argsObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/puppeteer/page/pdf_options.rb', line 146

def page_print_args
  {
    transferMode: 'ReturnAsStream',
    landscape: @landscape || false,
    displayHeaderFooter: @display_header_footer || false,
    headerTemplate: @header_template || '',
    footerTemplate: @footer_template || '',
    printBackground: @print_background || false,
    scale: @scale || 1,
    paperWidth: paper_size.width,
    paperHeight: paper_size.height,
    marginTop: margin.top,
    marginBottom: margin.bottom,
    marginLeft: margin.left,
    marginRight: margin.right,
    pageRanges: @page_ranges || '',
    preferCSSPageSize: @prefer_css_page_size || false,
  }
end