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