Module: Gotenberg::Chromium::Properties

Included in:
Gotenberg::Chromium
Defined in:
lib/gotenberg/chromium/properties.rb

Instance Method Summary collapse

Instance Method Details

#emulate_media_type(type) ⇒ Object

Forces Chromium to emulate the media type "print" or "screen".



131
132
133
134
135
# File 'lib/gotenberg/chromium/properties.rb', line 131

def emulate_media_type type
  properties['emulatedMediaType'] = type

  self
end

#extra_http_headers(headers) ⇒ Object

Sets extra HTTP headers that Chromium will send when loading the HTML document.



117
118
119
120
121
# File 'lib/gotenberg/chromium/properties.rb', line 117

def extra_http_headers headers
  properties['extraHttpHeaders'] = headers.to_json

  self
end

#fail_on_console_exceptionsObject

Forces Gotenberg to return a 409 Conflict response if there are exceptions in the Chromium console



124
125
126
127
128
# File 'lib/gotenberg/chromium/properties.rb', line 124

def fail_on_console_exceptions
  properties['failOnConsoleExceptions'] = true

  self
end

#landscapeObject

Sets the paper orientation to landscape.



56
57
58
59
60
# File 'lib/gotenberg/chromium/properties.rb', line 56

def landscape
  properties['landscape'] = true

  self
end

#margins(top: nil, bottom: nil, left: nil, right: nil) ⇒ Object

Overrides the default margins (i.e., 0.39), in inches.



32
33
34
35
36
37
38
39
# File 'lib/gotenberg/chromium/properties.rb', line 32

def margins top: nil, bottom: nil, left: nil, right: nil
  properties['marginTop'] = top if top
  properties['marginBottom'] = bottom if bottom
  properties['marginLeft'] = left if left
  properties['marginRight'] = right if right

  self
end

#native_page_ranges(ranges) ⇒ Object

Set the page ranges to print, e.g., "1-5, 8, 11-13". Empty means all pages.



70
71
72
73
74
# File 'lib/gotenberg/chromium/properties.rb', line 70

def native_page_ranges ranges
  properties['nativePageRanges'] = ranges

  self
end

#paper_size(width, height) ⇒ Object

Overrides the default paper size, in inches. Examples of paper size (width x height): Letter - 8.5 x 11 (default) Legal - 8.5 x 14 Tabloid - 11 x 17 Ledger - 17 x 11 A0 - 33.1 x 46.8 A1 - 23.4 x 33.1 A2 - 16.54 x 23.4 A3 - 11.7 x 16.54 A4 - 8.27 x 11.7 A5 - 5.83 x 8.27 A6 - 4.13 x 5.83



24
25
26
27
28
29
# File 'lib/gotenberg/chromium/properties.rb', line 24

def paper_size width, height
  properties['paperWidth'] = width
  properties['paperHeight'] = height

  self
end

#pdf_format(format) ⇒ Object

Sets the PDF format of the resulting PDF. See https://gotenberg.dev/docs/modules/pdf-engines#engines.



139
140
141
142
143
# File 'lib/gotenberg/chromium/properties.rb', line 139

def pdf_format format
  properties['pdfFormat'] = format

  self
end

#prefer_css_page_sizeObject

Forces page size as defined by CSS.



42
43
44
45
46
# File 'lib/gotenberg/chromium/properties.rb', line 42

def prefer_css_page_size
  properties['preferCssPageSize'] = true

  self
end

Prints the background graphics.



49
50
51
52
53
# File 'lib/gotenberg/chromium/properties.rb', line 49

def print_background
  properties['printBackground'] = true

  self
end

#scale(scale) ⇒ Object

Overrides the default scale of the page rendering (i.e., 1.0).



63
64
65
66
67
# File 'lib/gotenberg/chromium/properties.rb', line 63

def scale scale
  properties['scale'] = scale

  self
end

#single_pageObject

Define whether to print the entire content in one single page



8
9
10
# File 'lib/gotenberg/chromium/properties.rb', line 8

def single_page
  properties['singlePage'] = true
end

#url(url, extra_link_tags = [], extra_script_tags = []) ⇒ Object

Converts a target URL to PDF. See https://gotenberg.dev/docs/modules/chromium#url.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/gotenberg/chromium/properties.rb', line 147

def url url, extra_link_tags = [], extra_script_tags = []
  links = extra_link_tags.flat_map { |url| { 'href' => url } }
  scripts = extra_script_tags.flat_map { |url| { 'src' => url } }

  properties['url'] = url
  properties['extraLinkTags'] = links.to_json
  properties['extraScriptTags'] = scripts.to_json

  @endpoint = '/forms/chromium/convert/url'

  self
end

#user_agent(user_agent) ⇒ Object

DEPRECATED in Gotenberg 8. Overrides the default "User-Agent" header.



110
111
112
113
114
# File 'lib/gotenberg/chromium/properties.rb', line 110

def user_agent user_agent
  properties['userAgent'] = user_agent

  self
end

#wait_delay(delay) ⇒ Object

Sets the duration (i.e., "1s", "2ms", etc.) to wait when loading an HTML document before converting it to PDF.



77
78
79
80
81
# File 'lib/gotenberg/chromium/properties.rb', line 77

def wait_delay delay
  properties['waitDelay'] = delay

  self
end

#wait_for_expression(expression) ⇒ Object

Sets the JavaScript expression to wait before converting an HTML document to PDF until it returns true. For instance: "window.status === 'ready'".



85
86
87
88
89
# File 'lib/gotenberg/chromium/properties.rb', line 85

def wait_for_expression expression
  properties['waitForExpression'] = expression

  self
end

#wait_for_network_almost_idleObject

Waits until at most two network connections remain active for 500ms. Prefer this for long polling, WebSockets, analytics, or heartbeat requests. See https://gotenberg.dev/docs/convert-with-chromium/convert-html-to-pdf#network-errors.



103
104
105
106
107
# File 'lib/gotenberg/chromium/properties.rb', line 103

def wait_for_network_almost_idle
  properties['skipNetworkAlmostIdleEvent'] = false

  self
end

#wait_for_network_idleObject

Waits until no network connections remain active for 500ms. Avoid this for long polling or WebSockets, which may prevent network idle. See https://gotenberg.dev/docs/convert-with-chromium/convert-html-to-pdf#network-errors.



94
95
96
97
98
# File 'lib/gotenberg/chromium/properties.rb', line 94

def wait_for_network_idle
  properties['skipNetworkIdleEvent'] = false

  self
end