forked from thecodingmachine/gotenberg-go-client
-
Notifications
You must be signed in to change notification settings - Fork 2
/
chromium.go
240 lines (191 loc) · 8.23 KB
/
chromium.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package gotenberg
import (
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/runatal/gotenberg-go-client/v8/document"
)
type chromiumRequest struct {
header document.Document
footer document.Document
*baseRequest
}
func newChromiumRequest() *chromiumRequest {
return &chromiumRequest{nil, nil, newBaseRequest()}
}
// WaitDelay sets the duration (i.e., "1s", "2ms", etc.) to wait when loading an
// HTML document before converting it to PDF.
func (req *chromiumRequest) WaitDelay(delay time.Duration) {
req.fields[fieldChromiumWaitDelay] = delay.String()
}
// WaitForExpression sets the JavaScript expression to wait before converting an
// HTML document into PDF until it returns true.
func (req *chromiumRequest) WaitForExpression(expression string) {
req.fields[fieldChromiumWaitForExpression] = expression
}
// EmulatePrintMediaType forces Chromium to emulate the media type "print".
func (req *chromiumRequest) EmulatePrintMediaType() {
req.fields[fieldChromiumEmulatedMediaType] = "print"
}
// EmulateScreenMediaType forces Chromium to emulate the media type "screen".
func (req *chromiumRequest) EmulateScreenMediaType() {
req.fields[fieldChromiumEmulatedMediaType] = "screen"
}
// Cookies to store in the Chromium cookie jar.
func (req *chromiumRequest) Cookies(cookies []Cookie) error {
for _, cookie := range cookies {
if err := cookie.validate(); err != nil {
return fmt.Errorf("validate cookies: %w", err)
}
}
marshaledCookies, err := json.Marshal(cookies)
if err != nil {
return fmt.Errorf("marshal cookies to JSON: %w", err)
}
req.fields[fieldChromiumCookies] = string(marshaledCookies)
return nil
}
// UserAgent overrides the default User-Agent HTTP header.
func (req *chromiumRequest) UserAgent(ua string) {
req.fields[fieldChromiumUserAgent] = ua
}
// ExtraHTTPHeaders sets extra HTTP headers that Chromium will send when loading the HTML document.
func (req *chromiumRequest) ExtraHTTPHeaders(headers map[string]string) error {
marshaledHeaders, err := json.Marshal(headers)
if err != nil {
return fmt.Errorf("marshal headers to JSON: %w", err)
}
req.fields[fieldChromiumExtraHTTPHeaders] = string(marshaledHeaders)
return nil
}
// FailOnHTTPStatusCodes forces Gotenberg to return a 409 Conflict response
// if the HTTP status code from the main page is not acceptable.
func (req *chromiumRequest) FailOnHTTPStatusCodes(statusCodes []int) error {
marshaledStatusCodes, err := json.Marshal(statusCodes)
if err != nil {
return fmt.Errorf("marshal HTTP status codes to JSON: %w", err)
}
req.fields[fieldChromiumFailOnHTTPStatusCodes] = string(marshaledStatusCodes)
return nil
}
// FailOnResourceHTTPStatusCodes forces Gotenberg to return a 409 Conflict response
// if the HTTP status code from at least one resource is not acceptable.
func (req *chromiumRequest) FailOnResourceHTTPStatusCodes(statusCodes []int) error {
marshaledStatusCodes, err := json.Marshal(statusCodes)
if err != nil {
return fmt.Errorf("marshal HTTP status codes to JSON: %w", err)
}
req.fields[fieldChromiumFailOnResourceHTTPStatusCodes] = string(marshaledStatusCodes)
return nil
}
// FailOnConsoleExceptions forces Gotenberg to return a 409 Conflict response
// if there are exceptions in the Chromium console.
func (req *chromiumRequest) FailOnConsoleExceptions() {
req.fields[fieldChromiumFailOnConsoleExceptions] = strconv.FormatBool(true)
}
// FailOnResourceLoadingFailed forces Gotenberg to return a 409 Conflict if Chromium
// fails to load at least one resource.
func (req *chromiumRequest) FailOnResourceLoadingFailed() {
req.fields[fieldChromiumFailOnResourceLoadingFailed] = strconv.FormatBool(true)
}
// SkipNetworkIdleEvent specifies whether Chromium have to wait or not for its network to be idle.
// Enabled by default in Gotenberg >= 8.11.0.
func (req *chromiumRequest) SkipNetworkIdleEvent() {
req.fields[fieldChromiumSkipNetworkIdleEvent] = strconv.FormatBool(true)
}
// SinglePage defines whether to print the entire content in one single page.
func (req *chromiumRequest) SinglePage() {
req.fields[fieldChromiumSinglePage] = strconv.FormatBool(true)
}
// PaperSize sets paperWidth and paperHeight form fields with the provided unit.
// If unit is empty, it defaults to inches. Default is Letter (8.5 x 11 inches).
func (req *chromiumRequest) PaperSize(size PaperDimensions) {
if size.Unit == "" {
size.Unit = IN
}
req.fields[fieldChromiumPaperWidth] = fmt.Sprintf("%f%s", size.Width, size.Unit)
req.fields[fieldChromiumPaperHeight] = fmt.Sprintf("%f%s", size.Height, size.Unit)
}
// Margins sets marginTop, marginBottom, marginLeft and marginRight form fields. Default unit is inches.
func (req *chromiumRequest) Margins(margins PageMargins) {
if margins.Unit == "" {
margins.Unit = IN
}
req.fields[fieldChromiumMarginTop] = fmt.Sprintf("%f%s", margins.Top, margins.Unit)
req.fields[fieldChromiumMarginBottom] = fmt.Sprintf("%f%s", margins.Bottom, margins.Unit)
req.fields[fieldChromiumMarginLeft] = fmt.Sprintf("%f%s", margins.Left, margins.Unit)
req.fields[fieldChromiumMarginRight] = fmt.Sprintf("%f%s", margins.Right, margins.Unit)
}
// PreferCSSPageSize forces page size as defined by CSS.
func (req *chromiumRequest) PreferCSSPageSize() {
req.fields[fieldChromiumPreferCSSPageSize] = strconv.FormatBool(true)
}
// PrintBackground prints the background graphics.
func (req *chromiumRequest) PrintBackground() {
req.fields[fieldChromiumPrintBackground] = strconv.FormatBool(true)
}
// OmitBackground hides default white background and allows generating PDFs with transparency.
func (req *chromiumRequest) OmitBackground() {
req.fields[fieldChromiumOmitBackground] = strconv.FormatBool(true)
}
// Landscape sets the paper orientation to landscape.
func (req *chromiumRequest) Landscape() {
req.fields[fieldChromiumLandscapeChrome] = strconv.FormatBool(true)
}
// Scale overrides the default scale of the page rendering (i.e., 1.0).
func (req *chromiumRequest) Scale(factor float64) {
req.fields[fieldChromiumScale] = fmt.Sprintf("%f", factor)
}
// NativePageRanges sets the page ranges to print, e.g., "1-5, 8, 11-13". Empty means all pages.
func (req *chromiumRequest) NativePageRanges(ranges string) {
req.fields[fieldChromiumNativePageRanges] = ranges
}
// GenerateDocumentOutline embeds the document outline into the PDF.
func (req *chromiumRequest) GenerateDocumentOutline() {
req.fields[fieldChromiumGenerateDocumentOutline] = strconv.FormatBool(true)
}
// Header adds a header to each page.
func (req *chromiumRequest) Header(header document.Document) {
req.header = header
}
// Footer adds a footer to each page.
func (req *chromiumRequest) Footer(footer document.Document) {
req.footer = footer
}
// PdfA sets the PDF/A format of the resulting PDF.
func (req *chromiumRequest) PdfA(pdfa PdfAFormat) {
req.fields[fieldOfficePdfA] = string(pdfa)
}
// PdfUA enables PDF for Universal Access for optimal accessibility.
func (req *chromiumRequest) PdfUA() {
req.fields[fieldOfficePdfUa] = strconv.FormatBool(true)
}
// Metadata sets the metadata to write.
func (req *chromiumRequest) Metadata(jsonData []byte) {
req.fields[fieldMetadata] = string(jsonData)
}
// ScreenshotWidth Width sets the device screen width in pixels.
func (req *chromiumRequest) ScreenshotWidth(width float64) {
req.fields[fieldScreenshotWidth] = fmt.Sprintf("%f", width)
}
// ScreenshotHeight sets the device screen height in pixels.
func (req *chromiumRequest) ScreenshotHeight(height float64) {
req.fields[fieldScreenshotHeight] = fmt.Sprintf("%f", height)
}
// ScreenshotClip defines whether to clip the screenshot according to the device dimensions.
func (req *chromiumRequest) ScreenshotClip() {
req.fields[fieldScreenshotClip] = strconv.FormatBool(true)
}
// ScreenshotQuality sets the compression quality from range 0 to 100 (jpeg only).
func (req *chromiumRequest) ScreenshotQuality(quality int) {
req.fields[fieldScreenshotQuality] = strconv.Itoa(quality)
}
// ScreenshotOptimizeForSpeed defines whether to optimize image encoding for speed, not for resulting size.
func (req *chromiumRequest) ScreenshotOptimizeForSpeed() {
req.fields[fieldScreenshotOptimizeForSpeed] = strconv.FormatBool(true)
}
// Format sets the image compression format, either PNG, JPEG or WEBP. Default is PNG.
func (req *chromiumRequest) Format(format ImageFormat) {
req.fields[fieldScreenshotFormat] = string(format)
}