1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Gotenberg; |
4
|
|
|
|
5
|
|
|
abstract class ChromeRequest extends Request implements GotenbergRequestInterface |
6
|
|
|
{ |
7
|
|
|
/** @var Document|null */ |
8
|
|
|
protected $header; |
9
|
|
|
|
10
|
|
|
/** @var Document|null */ |
11
|
|
|
protected $footer; |
12
|
|
|
|
13
|
|
|
/** @var float|null */ |
14
|
|
|
protected $paperWidth; |
15
|
|
|
|
16
|
|
|
/** @var float|null */ |
17
|
|
|
protected $paperHeight; |
18
|
|
|
|
19
|
|
|
/** @var float|null */ |
20
|
|
|
protected $marginTop; |
21
|
|
|
|
22
|
|
|
/** @var float|null */ |
23
|
|
|
protected $marginBottom; |
24
|
|
|
|
25
|
|
|
/** @var float|null */ |
26
|
|
|
protected $marginLeft; |
27
|
|
|
|
28
|
|
|
/** @var float|null */ |
29
|
|
|
protected $marginRight; |
30
|
|
|
|
31
|
|
|
/** @var bool */ |
32
|
|
|
protected $landscape; |
33
|
|
|
|
34
|
|
|
/** @var int|null */ |
35
|
|
|
protected $webFontsTimeout; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @return array<string,mixed> |
39
|
|
|
*/ |
40
|
|
|
public function getFormValues(): array |
41
|
|
|
{ |
42
|
|
|
$values = []; |
43
|
|
|
if (!empty($this->webhookURL)) { |
44
|
|
|
$values[self::WEBHOOK_URL] = $this->webhookURL; |
45
|
|
|
} |
46
|
|
|
if (!is_null($this->paperWidth)) { |
47
|
|
|
$values[self::PAPER_WIDTH] = $this->paperWidth; |
48
|
|
|
} |
49
|
|
|
if (!is_null($this->paperHeight)) { |
50
|
|
|
$values[self::PAPER_HEIGHT] = $this->paperHeight; |
51
|
|
|
} |
52
|
|
|
if (!is_null($this->marginTop)) { |
53
|
|
|
$values[self::MARGIN_TOP] = $this->marginTop; |
54
|
|
|
} |
55
|
|
|
if (!is_null($this->marginBottom)) { |
56
|
|
|
$values[self::MARGIN_BOTTOM] = $this->marginBottom; |
57
|
|
|
} |
58
|
|
|
if (!is_null($this->marginLeft)) { |
59
|
|
|
$values[self::MARGIN_LEFT] = $this->marginLeft; |
60
|
|
|
} |
61
|
|
|
if (!is_null($this->marginRight)) { |
62
|
|
|
$values[self::MARGIN_RIGHT] = $this->marginRight; |
63
|
|
|
} |
64
|
|
|
if (!is_null($this->webFontsTimeout)) { |
65
|
|
|
$values[self::WEB_FONTS_TIMEOUT] = $this->webFontsTimeout; |
66
|
|
|
} |
67
|
|
|
$values[self::LANDSCAPE] = $this->landscape; |
68
|
|
|
return $values; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array<string,Document> |
73
|
|
|
*/ |
74
|
|
|
public function getFormFiles(): array |
75
|
|
|
{ |
76
|
|
|
$files = []; |
77
|
|
|
if (!empty($this->header)) { |
78
|
|
|
$files['header.html'] = $this->header; |
79
|
|
|
} |
80
|
|
|
if (!empty($this->footer)) { |
81
|
|
|
$files['footer.html'] = $this->footer; |
82
|
|
|
} |
83
|
|
|
return $files; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @param float[] $paperSize |
88
|
|
|
* @throws RequestException |
89
|
|
|
*/ |
90
|
|
|
public function setPaperSize(array $paperSize): void |
91
|
|
|
{ |
92
|
|
|
if (count($paperSize) !== 2) { |
93
|
|
|
throw new RequestException('Wrong paper size.'); |
94
|
|
|
} |
95
|
|
|
$this->paperWidth = $paperSize[0]; |
96
|
|
|
$this->paperHeight = $paperSize[1]; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param float[] $margins |
101
|
|
|
* @throws RequestException |
102
|
|
|
*/ |
103
|
|
|
public function setMargins(array $margins): void |
104
|
|
|
{ |
105
|
|
|
if (count($margins) !== 4) { |
106
|
|
|
throw new RequestException('Wrong margins.'); |
107
|
|
|
} |
108
|
|
|
$this->marginTop = $margins[0]; |
109
|
|
|
$this->marginBottom = $margins[1]; |
110
|
|
|
$this->marginLeft = $margins[2]; |
111
|
|
|
$this->marginRight = $margins[3]; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param Document|null $header |
116
|
|
|
*/ |
117
|
|
|
public function setHeader(?Document $header): void |
118
|
|
|
{ |
119
|
|
|
$this->header = $header; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Document|null $footer |
124
|
|
|
*/ |
125
|
|
|
public function setFooter(?Document $footer): void |
126
|
|
|
{ |
127
|
|
|
$this->footer = $footer; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* @param float|null $paperWidth |
132
|
|
|
*/ |
133
|
|
|
public function setPaperWidth(?float $paperWidth): void |
134
|
|
|
{ |
135
|
|
|
$this->paperWidth = $paperWidth; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param float|null $paperHeight |
140
|
|
|
*/ |
141
|
|
|
public function setPaperHeight(?float $paperHeight): void |
142
|
|
|
{ |
143
|
|
|
$this->paperHeight = $paperHeight; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param float|null $marginTop |
148
|
|
|
*/ |
149
|
|
|
public function setMarginTop(?float $marginTop): void |
150
|
|
|
{ |
151
|
|
|
$this->marginTop = $marginTop; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param float|null $marginBottom |
156
|
|
|
*/ |
157
|
|
|
public function setMarginBottom(?float $marginBottom): void |
158
|
|
|
{ |
159
|
|
|
$this->marginBottom = $marginBottom; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param float|null $marginLeft |
164
|
|
|
*/ |
165
|
|
|
public function setMarginLeft(?float $marginLeft): void |
166
|
|
|
{ |
167
|
|
|
$this->marginLeft = $marginLeft; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @param float|null $marginRight |
172
|
|
|
*/ |
173
|
|
|
public function setMarginRight(?float $marginRight): void |
174
|
|
|
{ |
175
|
|
|
$this->marginRight = $marginRight; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param bool $landscape |
180
|
|
|
*/ |
181
|
|
|
public function setLandscape(bool $landscape): void |
182
|
|
|
{ |
183
|
|
|
$this->landscape = $landscape; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @param int|null $webFontsTimeout |
188
|
|
|
*/ |
189
|
|
|
public function setWebFontsTimeout(?int $webFontsTimeout): void |
190
|
|
|
{ |
191
|
|
|
$this->webFontsTimeout = $webFontsTimeout; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|