Class: Bidi2pdf::Bidi::Commands::PagePrint

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/bidi2pdf/bidi/commands/page_print.rb

Instance Method Summary collapse

Methods included from Base

#==, #as_payload, #eql?, #hash, #inspect

Constructor Details

#initialize(cdp_session:, print_options:) ⇒ PagePrint

Returns a new instance of PagePrint.



11
12
13
14
15
16
17
18
19
20
# File 'lib/bidi2pdf/bidi/commands/page_print.rb', line 11

def initialize(cdp_session:, print_options:)
  @cdp_session = cdp_session
  @print_options = print_options || { background: true }

  PrintParametersValidator.validate!(@print_options)

  return unless @print_options[:page]&.key?(:format)

  @print_options[:page] = Bidi2pdf.translate_paper_format @print_options[:page][:format]
end

Instance Method Details

#method_nameObject

rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



58
59
60
# File 'lib/bidi2pdf/bidi/commands/page_print.rb', line 58

def method_name
  "goog:cdp.sendCommand"
end

#paramsObject

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/bidi2pdf/bidi/commands/page_print.rb', line 23

def params
  {
    # https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF
    method: "Page.printToPDF",
    session: @cdp_session,
    params: {
      "printBackground" => @print_options[:background],

      "marginTop" => cm_to_inch(@print_options.dig(:margin, :top) || 0),
      "marginBottom" => cm_to_inch(@print_options.dig(:margin, :bottom) || 0),
      "marginLeft" => cm_to_inch(@print_options.dig(:margin, :left) || 0),
      "marginRight" => cm_to_inch(@print_options.dig(:margin, :right) || 0),
      "landscape" => (@print_options[:orientation] || "portrait").to_sym == :landscape,

      "paperWidth" => cm_to_inch(@print_options.dig(:page, :width)),
      "paperHeight" => cm_to_inch(@print_options.dig(:page, :height)),
      "pageRanges" => page_ranges_to_string(@print_options[:pageRanges]),
      "scale" => @print_options[:scale] || 1.0,

      "displayHeaderFooter" => @print_options[:display_header_footer],
      "headerTemplate" => @print_options[:header_template] || "",
      "footerTemplate" => @print_options[:footer_template] || "",

      "preferCSSPageSize" => @print_options.fetch(:prefer_css_page_size, true),

      "generateTaggedPDF" => @print_options.fetch(:generate_tagged_pdf, false),
      "generateDocumentOutline" => @print_options.fetch(:generate_document_outline, false),

      transferMode: "ReturnAsBase64"
    }.compact
  }
end