1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Swis\PdfcrowdClient; |
6
|
|
|
|
7
|
|
|
use Swis\PdfcrowdClient\Exceptions\PdfcrowdException; |
8
|
|
|
use Swis\PdfcrowdClient\Http\FactoryInterface; |
9
|
|
|
use Swis\PdfcrowdClient\Http\RequestFactory; |
10
|
|
|
use Swis\PdfcrowdClient\Http\RequestInterface; |
11
|
|
|
|
12
|
|
|
class Pdfcrowd |
13
|
|
|
{ |
14
|
|
|
const CLIENT_VERSION = '0.1'; |
15
|
|
|
const API_PREFIX = 'https://pdfcrowd.com/api'; |
16
|
|
|
|
17
|
|
|
const PAGE_LAYOUT_SINGLE_PAGE = 1; |
18
|
|
|
const PAGE_LAYOUT_CONTINUOUS = 2; |
19
|
|
|
const PAGE_LAYOUT_CONTINUOUS_FACING = 3; |
20
|
|
|
|
21
|
|
|
const PAGE_MODE_NONE_VISIBLE = 1; |
22
|
|
|
const PAGE_MODE_THUMBNAILS_VISIBLE = 2; |
23
|
|
|
const PAGE_MODE_FULLSCREEN = 3; |
24
|
|
|
|
25
|
|
|
const INITIAL_PDF_ZOOM_TYPE_FIT_WIDTH = 1; |
26
|
|
|
const INITIAL_PDF_ZOOM_TYPE_FIT_HEIGHT = 2; |
27
|
|
|
const INITIAL_PDF_ZOOM_TYPE_FIT_PAGE = 3; |
28
|
|
|
|
29
|
|
|
/* |
30
|
|
|
* Request options |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** @var array */ |
34
|
|
|
protected $requestBody; |
35
|
|
|
|
36
|
|
|
/** @var int */ |
37
|
|
|
protected $timeout; |
38
|
|
|
|
39
|
|
|
/** @var string */ |
40
|
|
|
protected $user_agent; |
41
|
|
|
|
42
|
|
|
/** @var mixed */ |
43
|
|
|
protected $output_destination; |
44
|
|
|
|
45
|
|
|
/** @var string */ |
46
|
|
|
protected $proxy_name; |
47
|
|
|
|
48
|
|
|
/** @var int */ |
49
|
|
|
protected $proxy_port; |
50
|
|
|
|
51
|
|
|
/** @var string */ |
52
|
|
|
protected $proxy_username = ''; |
53
|
|
|
|
54
|
|
|
/** @var string */ |
55
|
|
|
protected $proxy_password = ''; |
56
|
|
|
|
57
|
|
|
/* |
58
|
|
|
* Tracking tokens |
59
|
|
|
*/ |
60
|
|
|
|
61
|
|
|
/** @var bool */ |
62
|
|
|
protected $track_tokens = false; |
63
|
|
|
|
64
|
|
|
/** @var int */ |
65
|
|
|
protected $num_tokens_before = false; |
66
|
|
|
|
67
|
|
|
/** @var FactoryInterface */ |
68
|
|
|
protected $requestFactory; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Pdfcrowd constructor. |
72
|
|
|
* |
73
|
|
|
* @param string $username |
74
|
|
|
* @param string $key |
75
|
|
|
*/ |
76
|
146 |
|
public function __construct(string $username, string $key) |
77
|
|
|
{ |
78
|
146 |
|
$this->requestBody = [ |
79
|
146 |
|
'username' => $username, |
80
|
146 |
|
'key' => $key, |
81
|
146 |
|
'pdf_scaling_factor' => 1, |
82
|
146 |
|
'html_zoom' => 200, |
83
|
|
|
]; |
84
|
|
|
|
85
|
146 |
|
$this->user_agent = $this->getUserAgent(); |
86
|
|
|
|
87
|
146 |
|
$this->requestFactory = new RequestFactory(); |
88
|
146 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* This method allows you to override the default CurlRequest object. Added for testing purposes. |
92
|
|
|
* |
93
|
|
|
* @param \Swis\PdfcrowdClient\Http\FactoryInterface $requestFactory |
94
|
|
|
*/ |
95
|
136 |
|
public function setRequestFactory(FactoryInterface $requestFactory) |
96
|
|
|
{ |
97
|
136 |
|
$this->requestFactory = $requestFactory; |
98
|
136 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Each httpPost-call uses a clean request object. |
102
|
|
|
* |
103
|
|
|
* @return \Swis\PdfcrowdClient\Http\RequestInterface |
104
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
105
|
|
|
*/ |
106
|
132 |
|
protected function getNewRequestObject(): RequestInterface |
107
|
|
|
{ |
108
|
132 |
|
$request = $this->requestFactory->create(); |
109
|
|
|
|
110
|
132 |
|
return $request; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Converts an in-memory html document. |
115
|
|
|
* |
116
|
|
|
* @param string $src a string containing a html document |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
120
|
|
|
*/ |
121
|
128 |
|
public function convertHtml($src) |
122
|
|
|
{ |
123
|
128 |
|
if (!$src) { |
124
|
2 |
|
throw new PdfcrowdException('convertHTML(): the src parameter must not be empty'); |
125
|
|
|
} |
126
|
|
|
|
127
|
126 |
|
$this->requestBody['src'] = $src; |
128
|
|
|
|
129
|
126 |
|
$uri = $this->getApiUri('/pdf/convert/html/'); |
130
|
|
|
|
131
|
126 |
|
if ($this->track_tokens) { |
132
|
2 |
|
$this->num_tokens_before = $this->availableTokens(); |
133
|
|
|
} |
134
|
|
|
|
135
|
126 |
|
return $this->httpPost($uri, $this->requestBody); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Converts a web page. |
140
|
|
|
* |
141
|
|
|
* @param string $src a web page URL |
142
|
|
|
* |
143
|
|
|
* @return mixed |
144
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
145
|
|
|
*/ |
146
|
6 |
|
public function convertURI(string $src) |
147
|
|
|
{ |
148
|
6 |
|
$src = trim($src); |
149
|
6 |
|
if (!preg_match("/^https?:\/\/.*/i", $src)) { |
150
|
2 |
|
throw new PdfcrowdException("convertURI(): the URL must start with http:// or https:// (got '$src')"); |
151
|
|
|
} |
152
|
|
|
|
153
|
4 |
|
$this->requestBody['src'] = $src; |
154
|
4 |
|
$uri = $this->getApiUri('/pdf/convert/uri/'); |
155
|
|
|
|
156
|
4 |
|
if ($this->track_tokens) { |
157
|
2 |
|
$this->num_tokens_before = $this->availableTokens(); |
158
|
|
|
} |
159
|
|
|
|
160
|
4 |
|
return $this->httpPost($uri, $this->requestBody); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns the number of available conversion tokens. |
165
|
|
|
* |
166
|
|
|
* @return int |
167
|
|
|
*/ |
168
|
6 |
|
public function availableTokens(): int |
169
|
|
|
{ |
170
|
6 |
|
$username = $this->requestBody['username']; |
171
|
|
|
|
172
|
6 |
|
$uri = $this->getApiUri("/user/{$username}/tokens/"); |
173
|
|
|
|
174
|
|
|
$arr = [ |
175
|
6 |
|
'username' => $this->requestBody['username'], |
176
|
6 |
|
'key' => $this->requestBody['key'], |
177
|
|
|
]; |
178
|
|
|
|
179
|
6 |
|
$ntokens = $this->httpPost($uri, $arr); |
180
|
|
|
|
181
|
6 |
|
$response = (string) $ntokens; |
182
|
|
|
|
183
|
6 |
|
return (int) $response; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Get the number of tokens used in the last conversion. |
188
|
|
|
* This is only possible if you enable tracking tokens using trackTokens(true). |
189
|
|
|
* |
190
|
|
|
* @see trackTokens() |
191
|
|
|
* |
192
|
|
|
* @return int |
193
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
194
|
|
|
*/ |
195
|
8 |
|
public function getUsedTokens(): int |
196
|
|
|
{ |
197
|
8 |
|
if (!$this->track_tokens) { |
198
|
2 |
|
throw new PdfcrowdException( |
199
|
2 |
|
'getUsedTokens() only works if you enable tracking tokens with trackTokens(true)' |
200
|
|
|
); |
201
|
|
|
} |
202
|
|
|
|
203
|
6 |
|
if ($this->num_tokens_before === false) { |
204
|
2 |
|
throw new PdfcrowdException( |
205
|
2 |
|
'getUsedTokens() should not be called on its own, call a convert call first.' |
206
|
|
|
); |
207
|
|
|
} |
208
|
|
|
|
209
|
4 |
|
$num_tokens_after = $this->availableTokens(); |
210
|
|
|
|
211
|
4 |
|
return (int) $this->num_tokens_before - $num_tokens_after; |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Track how many tokens are available before each request. |
216
|
|
|
* After a request you can ask the number of used tokens with getUsedTokens. |
217
|
|
|
* |
218
|
|
|
* @see getUsedTokens() |
219
|
|
|
* |
220
|
|
|
* @param bool $trackTokens |
221
|
|
|
*/ |
222
|
10 |
|
public function trackTokens(bool $trackTokens = true) |
223
|
|
|
{ |
224
|
10 |
|
$this->track_tokens = $trackTokens; |
225
|
10 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Save the pdf to the given output destination. The variable $file_handle will serve as input to |
229
|
|
|
* the sink-option of Guzzle. |
230
|
|
|
* |
231
|
|
|
* @see http://docs.guzzlephp.org/en/stable/request-options.html#sink |
232
|
|
|
* |
233
|
|
|
* @example $pdfcrowd->setOutputDestination(fopen('/path/to/output.pdf', 'w'); |
234
|
|
|
* |
235
|
|
|
* @param $file_handle |
236
|
|
|
*/ |
237
|
2 |
|
public function setOutputDestination($file_handle) |
238
|
|
|
{ |
239
|
2 |
|
$this->output_destination = $file_handle; |
240
|
2 |
|
} |
241
|
|
|
|
242
|
2 |
|
public function setPageWidth($value) |
243
|
|
|
{ |
244
|
2 |
|
$this->requestBody['width'] = $value; |
245
|
2 |
|
} |
246
|
|
|
|
247
|
2 |
|
public function setPageHeight($value) |
248
|
|
|
{ |
249
|
2 |
|
$this->requestBody['height'] = $value; |
250
|
2 |
|
} |
251
|
|
|
|
252
|
4 |
|
public function setHorizontalMargin($value) |
253
|
|
|
{ |
254
|
4 |
|
$this->requestBody['margin_right'] = $this->requestBody['margin_left'] = $value; |
255
|
4 |
|
} |
256
|
|
|
|
257
|
4 |
|
public function setVerticalMargin($value) |
258
|
|
|
{ |
259
|
4 |
|
$this->requestBody['margin_top'] = $this->requestBody['margin_bottom'] = $value; |
260
|
4 |
|
} |
261
|
|
|
|
262
|
2 |
|
public function setBottomMargin($value) |
263
|
|
|
{ |
264
|
2 |
|
$this->requestBody['margin_bottom'] = $value; |
265
|
2 |
|
} |
266
|
|
|
|
267
|
8 |
|
public function setPageMargins($top, $right, $bottom, $left) |
268
|
|
|
{ |
269
|
8 |
|
$this->requestBody['margin_top'] = $top; |
270
|
8 |
|
$this->requestBody['margin_right'] = $right; |
271
|
8 |
|
$this->requestBody['margin_bottom'] = $bottom; |
272
|
8 |
|
$this->requestBody['margin_left'] = $left; |
273
|
8 |
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* If value is set to True then the PDF is encrypted. This prevents search engines from indexing the document. |
277
|
|
|
* The default is False. |
278
|
|
|
* |
279
|
|
|
* @param bool $val |
280
|
|
|
*/ |
281
|
2 |
|
public function setEncrypted(bool $val = true) |
282
|
|
|
{ |
283
|
2 |
|
$this->setOrUnset($val, 'encrypted'); |
284
|
2 |
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Protects the PDF with a user password. When a PDF has a user password, it must be supplied in order to view the |
288
|
|
|
* document and to perform operations allowed by the access permissions. At most 32 characters. |
289
|
|
|
* |
290
|
|
|
* @param string $pwd |
291
|
|
|
*/ |
292
|
2 |
|
public function setUserPassword(string $pwd) |
293
|
|
|
{ |
294
|
2 |
|
$this->setOrUnset($pwd, 'user_pwd'); |
295
|
2 |
|
} |
296
|
|
|
|
297
|
|
|
/** |
298
|
|
|
* Protects the PDF with an owner password. Supplying an owner password grants unlimited access to the PDF |
299
|
|
|
* including changing the passwords and access permissions. At most 32 characters. |
300
|
|
|
* |
301
|
|
|
* @param string $pwd |
302
|
|
|
*/ |
303
|
2 |
|
public function setOwnerPassword(string $pwd) |
304
|
|
|
{ |
305
|
2 |
|
$this->setOrUnset($pwd, 'owner_pwd'); |
306
|
2 |
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Set value to True disables printing the generated PDF. The default is False. |
310
|
|
|
* |
311
|
|
|
* @param bool $val |
312
|
|
|
*/ |
313
|
2 |
|
public function setNoPrint(bool $val = true) |
314
|
|
|
{ |
315
|
2 |
|
$this->setOrUnset($val, 'no_print'); |
316
|
2 |
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Set value to True to disable modifying the PDF. The default is False. |
320
|
|
|
* |
321
|
|
|
* @param bool $val |
322
|
|
|
*/ |
323
|
2 |
|
public function setNoModify(bool $val = true) |
324
|
|
|
{ |
325
|
2 |
|
$this->setOrUnset($val, 'no_modify'); |
326
|
2 |
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Set value to True to disable extracting text and graphics from the PDF. The default is False. |
330
|
|
|
* |
331
|
|
|
* @param bool $val |
332
|
|
|
*/ |
333
|
2 |
|
public function setNoCopy(bool $val = true) |
334
|
|
|
{ |
335
|
2 |
|
$this->setOrUnset($val, 'no_copy'); |
336
|
2 |
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Specifies the initial page layout when the PDF is opened in a viewer. |
340
|
|
|
* |
341
|
|
|
* Possible values: |
342
|
|
|
* \Swis\PdfcrowdClient\Pdfcrowd::SINGLE_PAGE |
343
|
|
|
* \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS |
344
|
|
|
* \Swis\PdfcrowdClient\Pdfcrowd::CONTINUOUS_FACING |
345
|
|
|
* |
346
|
|
|
* @param int $value |
347
|
|
|
* |
348
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
349
|
|
|
*/ |
350
|
8 |
View Code Duplication |
public function setPageLayout(int $value) |
|
|
|
|
351
|
|
|
{ |
352
|
8 |
|
if (!in_array($value, [ |
353
|
8 |
|
self::PAGE_LAYOUT_SINGLE_PAGE, |
354
|
8 |
|
self::PAGE_LAYOUT_CONTINUOUS, |
355
|
8 |
|
self::PAGE_LAYOUT_CONTINUOUS_FACING |
356
|
|
|
])) { |
357
|
2 |
|
throw new PdfcrowdException('Invalid page layout value!'); |
358
|
|
|
} |
359
|
|
|
|
360
|
6 |
|
$this->requestBody['page_layout'] = $value; |
361
|
6 |
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* Specifies the appearance of the PDF when opened. |
365
|
|
|
* |
366
|
|
|
* Possible values: |
367
|
|
|
* \Swis\PdfcrowdClient\Pdfcrowd::NONE_VISIBLE |
368
|
|
|
* \Swis\PdfcrowdClient\Pdfcrowd::THUMBNAILS_VISIBLE |
369
|
|
|
* \Swis\PdfcrowdClient\Pdfcrowd::PAGE_MODE_FULLSCREEN |
370
|
|
|
* |
371
|
|
|
* @param int $value |
372
|
|
|
* |
373
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
374
|
|
|
*/ |
375
|
8 |
View Code Duplication |
public function setPageMode(int $value) |
|
|
|
|
376
|
|
|
{ |
377
|
8 |
|
if (!in_array($value, [ |
378
|
8 |
|
self::PAGE_MODE_NONE_VISIBLE, |
379
|
8 |
|
self::PAGE_MODE_THUMBNAILS_VISIBLE, |
380
|
8 |
|
self::PAGE_MODE_FULLSCREEN |
381
|
|
|
])) { |
382
|
2 |
|
throw new PdfcrowdException('Invalid page mode value!'); |
383
|
|
|
} |
384
|
|
|
|
385
|
6 |
|
$this->requestBody['page_mode'] = $value; |
386
|
6 |
|
} |
387
|
|
|
|
388
|
|
|
/** |
389
|
|
|
* @param string $value |
390
|
|
|
*/ |
391
|
2 |
|
public function setFooterText(string $value) |
392
|
|
|
{ |
393
|
2 |
|
$this->setOrUnset($value, 'footer_text'); |
394
|
2 |
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Set value to False to disable printing images to the PDF. The default is True. |
398
|
|
|
* |
399
|
|
|
* @param bool $value |
400
|
|
|
*/ |
401
|
2 |
|
public function enableImages(bool $value = true) |
402
|
|
|
{ |
403
|
2 |
|
$this->setOrUnset(!$value, 'no_images'); |
404
|
2 |
|
} |
405
|
|
|
|
406
|
|
|
/** |
407
|
|
|
* Set value to False to disable printing backgrounds to the PDF. The default is True. |
408
|
|
|
* |
409
|
|
|
* @param bool $value |
410
|
|
|
*/ |
411
|
2 |
|
public function enableBackgrounds(bool $value = true) |
412
|
|
|
{ |
413
|
2 |
|
$this->setOrUnset(!$value, 'no_backgrounds'); |
414
|
2 |
|
} |
415
|
|
|
|
416
|
|
|
/** |
417
|
|
|
* Set HTML zoom in percents. It determines the precision used for rendering of the HTML content. Despite its name, |
418
|
|
|
* it does not zoom the HTML content. Higher values can improve glyph positioning and can lead to overall better |
419
|
|
|
* visual appearance of generated PDF .The default value is 200. |
420
|
|
|
* |
421
|
|
|
* @see setPdfScalingFactor |
422
|
|
|
* |
423
|
|
|
* @param int $value |
424
|
|
|
*/ |
425
|
2 |
|
public function setHtmlZoom(int $value) |
426
|
|
|
{ |
427
|
2 |
|
$this->setOrUnset($value, 'html_zoom'); |
428
|
2 |
|
} |
429
|
|
|
|
430
|
|
|
/** |
431
|
|
|
* Set value to False to disable JavaScript in web pages. The default is True. |
432
|
|
|
* |
433
|
|
|
* @param bool $value |
434
|
|
|
*/ |
435
|
2 |
|
public function enableJavaScript(bool $value = true) |
436
|
|
|
{ |
437
|
2 |
|
$this->setOrUnset(!$value, 'no_javascript'); |
438
|
2 |
|
} |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* Set value to False to disable hyperlinks in the PDF. The default is True. |
442
|
|
|
* |
443
|
|
|
* @param bool $value |
444
|
|
|
*/ |
445
|
2 |
|
public function enableHyperlinks(bool $value = true) |
446
|
|
|
{ |
447
|
2 |
|
$this->setOrUnset(!$value, 'no_hyperlinks'); |
448
|
2 |
|
} |
449
|
|
|
|
450
|
|
|
/** |
451
|
|
|
* Value is the text encoding used when none is specified in a web page. The default is utf-8. |
452
|
|
|
* |
453
|
|
|
* @param string $value |
454
|
|
|
*/ |
455
|
2 |
|
public function setDefaultTextEncoding(string $value) |
456
|
|
|
{ |
457
|
2 |
|
$this->setOrUnset($value, 'text_encoding'); |
458
|
2 |
|
} |
459
|
|
|
|
460
|
|
|
/** |
461
|
|
|
* If value is True then the print CSS media type is used (if available). |
462
|
|
|
* |
463
|
|
|
* @param bool $value |
464
|
|
|
*/ |
465
|
2 |
|
public function usePrintMedia(bool $value = true) |
466
|
|
|
{ |
467
|
2 |
|
$this->setOrUnset($value, 'use_print_media'); |
468
|
2 |
|
} |
469
|
|
|
|
470
|
|
|
/** |
471
|
|
|
* Prints at most npages pages. |
472
|
|
|
* |
473
|
|
|
* @param int $value |
474
|
|
|
*/ |
475
|
2 |
|
public function setMaxPages(int $value) |
476
|
|
|
{ |
477
|
2 |
|
$this->requestBody['max_pages'] = $value; |
478
|
2 |
|
} |
479
|
|
|
|
480
|
|
|
/** |
481
|
|
|
* @param bool $value |
482
|
|
|
*/ |
483
|
2 |
|
public function enablePdfcrowdLogo(bool $value = true) |
484
|
|
|
{ |
485
|
2 |
|
$this->setOrUnset($value, 'pdfcrowd_logo'); |
486
|
2 |
|
} |
487
|
|
|
|
488
|
|
|
/** |
489
|
|
|
* value specifies the appearance of the PDF when opened. |
490
|
|
|
* |
491
|
|
|
* Possible values: |
492
|
|
|
* \Swis\Pdfcrowd\Pdfcrowd::FIT_WIDTH |
493
|
|
|
* \Swis\Pdfcrowd\Pdfcrowd::FIT_HEIGHT |
494
|
|
|
* \Swis\Pdfcrowd\Pdfcrowd::FIT_PAGE |
495
|
|
|
* |
496
|
|
|
* @param int $value |
497
|
|
|
* |
498
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
499
|
|
|
*/ |
500
|
8 |
View Code Duplication |
public function setInitialPdfZoomType(int $value) |
|
|
|
|
501
|
|
|
{ |
502
|
8 |
|
if (!in_array($value, [ |
503
|
8 |
|
self::INITIAL_PDF_ZOOM_TYPE_FIT_WIDTH, |
504
|
8 |
|
self::INITIAL_PDF_ZOOM_TYPE_FIT_HEIGHT, |
505
|
8 |
|
self::INITIAL_PDF_ZOOM_TYPE_FIT_PAGE, |
506
|
|
|
])) { |
507
|
2 |
|
throw new PdfcrowdException('Invalid initial pdf zoom type value!'); |
508
|
|
|
} |
509
|
|
|
|
510
|
6 |
|
$this->requestBody['initial_pdf_zoom_type'] = $value; |
511
|
6 |
|
} |
512
|
|
|
|
513
|
|
|
/** |
514
|
|
|
* value specifies the initial page zoom of the PDF when opened. |
515
|
|
|
* |
516
|
|
|
* @param $value |
517
|
|
|
*/ |
518
|
4 |
|
public function setInitialPdfExactZoom($value) |
519
|
|
|
{ |
520
|
4 |
|
$this->requestBody['initial_pdf_zoom_type'] = 4; |
521
|
4 |
|
$this->requestBody['initial_pdf_zoom'] = $value; |
522
|
4 |
|
} |
523
|
|
|
|
524
|
|
|
/** |
525
|
|
|
* The scaling factor used to convert between HTML and PDF. The default value is 1.0. |
526
|
|
|
* |
527
|
|
|
* @param float $value |
528
|
|
|
*/ |
529
|
2 |
|
public function setPdfScalingFactor(float $value) |
530
|
|
|
{ |
531
|
2 |
|
$this->requestBody['pdf_scaling_factor'] = $value; |
532
|
2 |
|
} |
533
|
|
|
|
534
|
|
|
/** |
535
|
|
|
* Sets the author field in the created PDF. |
536
|
|
|
* |
537
|
|
|
* @param string $value |
538
|
|
|
*/ |
539
|
2 |
|
public function setAuthor(string $value) |
540
|
|
|
{ |
541
|
2 |
|
$this->requestBody['author'] = $value; |
542
|
2 |
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* If value is True then the conversion will fail when the source URI returns 4xx or 5xx HTTP status code. The |
546
|
|
|
* default is False. |
547
|
|
|
* |
548
|
|
|
* @param bool $value |
549
|
|
|
*/ |
550
|
2 |
|
public function setFailOnNon200(bool $value) |
551
|
|
|
{ |
552
|
2 |
|
$this->requestBody['fail_on_non200'] = $value; |
553
|
2 |
|
} |
554
|
|
|
|
555
|
|
|
/** |
556
|
|
|
* Places the specified html code inside the page footer. The following variables are expanded: |
557
|
|
|
* %u - URL to convert. |
558
|
|
|
* %p - The current page number. |
559
|
|
|
* %n - Total number of pages. |
560
|
|
|
* |
561
|
|
|
* @param string $value |
562
|
|
|
*/ |
563
|
2 |
|
public function setFooterHtml(string $value) |
564
|
|
|
{ |
565
|
2 |
|
$this->requestBody['footer_html'] = $value; |
566
|
2 |
|
} |
567
|
|
|
|
568
|
|
|
/** |
569
|
|
|
* Loads HTML code from the specified url and places it inside the page footer. See setFooterHtml for the list of |
570
|
|
|
* variables that are expanded. |
571
|
|
|
* |
572
|
|
|
* @see setFooterHtml |
573
|
|
|
* |
574
|
|
|
* @param string $value |
575
|
|
|
*/ |
576
|
2 |
|
public function setFooterUrl(string $value) |
577
|
|
|
{ |
578
|
2 |
|
$this->requestBody['footer_url'] = $value; |
579
|
2 |
|
} |
580
|
|
|
|
581
|
|
|
/** |
582
|
|
|
* Places the specified html code inside the page header. See setFooterHtml for the list of variables that are |
583
|
|
|
* expanded. |
584
|
|
|
* |
585
|
|
|
* @see setFooterHtml |
586
|
|
|
* |
587
|
|
|
* @param string $value |
588
|
|
|
*/ |
589
|
2 |
|
public function setHeaderHtml(string $value) |
590
|
|
|
{ |
591
|
2 |
|
$this->requestBody['header_html'] = $value; |
592
|
2 |
|
} |
593
|
|
|
|
594
|
|
|
/** |
595
|
|
|
* Loads HTML code from the specified url and places it inside the page header. See setFooterHtml for the list of |
596
|
|
|
* variables that are expanded. |
597
|
|
|
* |
598
|
|
|
* @see setFooterHtml |
599
|
|
|
* |
600
|
|
|
* @param string $value |
601
|
|
|
*/ |
602
|
2 |
|
public function setHeaderUrl(string $value) |
603
|
|
|
{ |
604
|
2 |
|
$this->requestBody['header_url'] = $value; |
605
|
2 |
|
} |
606
|
|
|
|
607
|
|
|
/** |
608
|
|
|
* The page background color in RRGGBB hexadecimal format. |
609
|
|
|
* |
610
|
|
|
* @param string $value |
611
|
|
|
*/ |
612
|
2 |
|
public function setPageBackgroundColor(string $value) |
613
|
|
|
{ |
614
|
2 |
|
$this->requestBody['page_background_color'] = $value; |
615
|
2 |
|
} |
616
|
|
|
|
617
|
|
|
/** |
618
|
|
|
* Does not print the body background. Requires the following CSS rule to be declared: |
619
|
|
|
* body {background-color:rgba(255,255,255,0.0);} |
620
|
|
|
* |
621
|
|
|
* @param bool $value |
622
|
|
|
*/ |
623
|
2 |
|
public function setTransparentBackground(bool $value = true) |
624
|
|
|
{ |
625
|
2 |
|
$this->setOrUnset($value, 'transparent_background'); |
626
|
2 |
|
} |
627
|
|
|
|
628
|
|
|
/** |
629
|
|
|
* An offset between physical and logical page numbers. The default value is 0. |
630
|
|
|
* |
631
|
|
|
* @example if set to "1" then the page numbering will start with 1 on the second page. |
632
|
|
|
* |
633
|
|
|
* @param int $value |
634
|
|
|
*/ |
635
|
2 |
|
public function setPageNumberingOffset(int $value) |
636
|
|
|
{ |
637
|
2 |
|
$this->requestBody['page_numbering_offset'] = $value; |
638
|
2 |
|
} |
639
|
|
|
|
640
|
|
|
/** |
641
|
|
|
* Value is a comma seperated list of physical page numbers on which the header a footer are not printed. Negative |
642
|
|
|
* numbers count backwards from the last page: -1 is the last page, -2 is the last but one page, and so on. |
643
|
|
|
* |
644
|
|
|
* @example "1,-1" will not print the header and footer on the first and the last page. |
645
|
|
|
* |
646
|
|
|
* @param string $value |
647
|
|
|
*/ |
648
|
2 |
|
public function setHeaderFooterPageExcludeList(string $value) |
649
|
|
|
{ |
650
|
2 |
|
$this->requestBody['header_footer_page_exclude_list'] = $value; |
651
|
2 |
|
} |
652
|
|
|
|
653
|
|
|
/** |
654
|
|
|
* url is a public absolute URL of the watermark image (must start either with http:// or https://). The supported |
655
|
|
|
* formats are PNG and JPEG. offset_x and offset_y is the watermark offset in units. The default offset is (0,0). |
656
|
|
|
* |
657
|
|
|
* @param string $url |
658
|
|
|
* @param int $offset_x |
659
|
|
|
* @param int $offset_y |
660
|
|
|
*/ |
661
|
6 |
|
public function setWatermark(string $url, $offset_x = 0, $offset_y = 0) |
662
|
|
|
{ |
663
|
6 |
|
$this->requestBody['watermark_url'] = $url; |
664
|
6 |
|
$this->requestBody['watermark_offset_x'] = $offset_x; |
665
|
6 |
|
$this->requestBody['watermark_offset_y'] = $offset_y; |
666
|
6 |
|
} |
667
|
|
|
|
668
|
|
|
/** |
669
|
|
|
* Rotates the watermark by angle degrees. |
670
|
|
|
* |
671
|
|
|
* @param int $angle |
672
|
|
|
*/ |
673
|
2 |
|
public function setWatermarkRotationsetWatermarkRotation(int $angle) |
674
|
|
|
{ |
675
|
2 |
|
$this->requestBody['watermark_rotation'] = $angle; |
676
|
2 |
|
} |
677
|
|
|
|
678
|
|
|
/** |
679
|
|
|
* When value is set to True then the watermark is be placed in the background. By default, the watermark is |
680
|
|
|
* placed in the foreground. |
681
|
|
|
* |
682
|
|
|
* @param bool $val |
683
|
|
|
*/ |
684
|
4 |
|
public function setWatermarkInBackground(bool $val = true) |
685
|
|
|
{ |
686
|
4 |
|
$this->setOrUnset($val, 'watermark_in_background'); |
687
|
4 |
|
} |
688
|
|
|
|
689
|
|
|
/** |
690
|
|
|
* @param string $proxyname |
691
|
|
|
* @param int $port |
692
|
|
|
* @param string $username |
693
|
|
|
* @param string $password |
694
|
|
|
*/ |
695
|
2 |
|
public function setProxy(string $proxyname, int $port, string $username = '', string $password = '') |
696
|
|
|
{ |
697
|
2 |
|
$this->proxy_name = $proxyname; |
698
|
2 |
|
$this->proxy_port = $port; |
699
|
2 |
|
$this->proxy_username = $username; |
700
|
2 |
|
$this->proxy_password = $password; |
701
|
2 |
|
} |
702
|
|
|
|
703
|
|
|
/** |
704
|
|
|
* Override the default user agent value that is sent with each request to the Pdfcrowd API. |
705
|
|
|
* |
706
|
|
|
* @param string $user_agent |
707
|
|
|
*/ |
708
|
2 |
|
public function setUserAgent(string $user_agent) |
709
|
|
|
{ |
710
|
2 |
|
$this->user_agent = $user_agent; |
711
|
2 |
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Set the timeout request option. |
715
|
|
|
* |
716
|
|
|
* @param int $timeout |
717
|
|
|
*/ |
718
|
2 |
|
public function setTimeout(int $timeout) |
719
|
|
|
{ |
720
|
2 |
|
if (is_int($timeout) && $timeout > 0) { |
721
|
2 |
|
$this->timeout = $timeout; |
722
|
|
|
} |
723
|
2 |
|
} |
724
|
|
|
|
725
|
|
|
/** |
726
|
|
|
* @param string $url |
727
|
|
|
* @param array $requestBody |
728
|
|
|
* |
729
|
|
|
* @return mixed |
730
|
|
|
* @throws \Swis\PdfcrowdClient\Exceptions\PdfcrowdException |
731
|
|
|
*/ |
732
|
132 |
|
private function httpPost(string $url, array $requestBody) |
733
|
|
|
{ |
734
|
132 |
|
$request = $this->buildRequest($url, $requestBody); |
735
|
|
|
|
736
|
|
|
try { |
737
|
132 |
|
$response = $request->execute(); |
738
|
|
|
|
739
|
130 |
|
$http_code = $request->getHttpStatusCode(); |
740
|
2 |
|
} catch (\Exception $e) { |
741
|
2 |
|
throw new PdfcrowdException("Unknown error during request to Pdfcrowd", 0, $e); |
742
|
|
|
} |
743
|
|
|
|
744
|
130 |
|
if ($http_code !== 200) { |
745
|
2 |
|
throw new PdfcrowdException((string) $response, $http_code); |
746
|
|
|
} |
747
|
|
|
|
748
|
128 |
|
return $response; |
749
|
|
|
} |
750
|
|
|
|
751
|
132 |
|
protected function buildRequest(string $url, array $requestBody): RequestInterface |
752
|
|
|
{ |
753
|
132 |
|
$request = $this->getNewRequestObject(); |
754
|
|
|
|
755
|
132 |
|
$request->setUserAgent($this->user_agent); |
756
|
|
|
|
757
|
132 |
|
if (isset($this->timeout)) { |
758
|
2 |
|
$request->setTimeout($this->timeout); |
759
|
|
|
} |
760
|
|
|
|
761
|
132 |
|
if ($this->proxy_name) { |
762
|
2 |
|
$request->setProxy($this->proxy_name, $this->proxy_port); |
763
|
2 |
|
if ($this->proxy_username) { |
764
|
2 |
|
$request->setProxyAuth($this->proxy_username, $this->proxy_password); |
765
|
|
|
} |
766
|
|
|
} |
767
|
|
|
|
768
|
132 |
|
$request->setUrl($url); |
769
|
|
|
|
770
|
132 |
|
$request->setBody($requestBody); |
771
|
|
|
|
772
|
132 |
|
if (isset($this->output_destination)) { |
773
|
2 |
|
$request->setOutputDestination($this->output_destination); |
774
|
|
|
} |
775
|
|
|
|
776
|
132 |
|
return $request; |
777
|
|
|
} |
778
|
|
|
|
779
|
|
|
/** |
780
|
|
|
* Set or unset a parameter that will be sent with the request to the pdfcrowd API. |
781
|
|
|
* |
782
|
|
|
* @param mixed $val |
783
|
|
|
* @param string $field |
784
|
|
|
*/ |
785
|
36 |
|
private function setOrUnset($val, string $field) |
786
|
|
|
{ |
787
|
36 |
|
if ($val) { |
788
|
36 |
|
$this->requestBody[$field] = $val; |
789
|
|
|
} else { |
790
|
2 |
|
unset($this->requestBody[$field]); |
791
|
|
|
} |
792
|
36 |
|
} |
793
|
|
|
|
794
|
|
|
/** |
795
|
|
|
* @param string $endpoint |
796
|
|
|
* |
797
|
|
|
* @return string |
798
|
|
|
*/ |
799
|
132 |
|
protected function getApiUri(string $endpoint): string |
800
|
|
|
{ |
801
|
132 |
|
return self::API_PREFIX.$endpoint; |
802
|
|
|
} |
803
|
|
|
|
804
|
146 |
|
protected function getUserAgent() |
805
|
|
|
{ |
806
|
146 |
|
return 'swisnl_pdfcrowd_client_'.self::CLIENT_VERSION; |
807
|
|
|
} |
808
|
|
|
} |
809
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.