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