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