-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectpdf.php
286 lines (244 loc) · 9.45 KB
/
selectpdf.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
// Copyright (C) 2014-2017 selectpdf.com
// https://selectpdf.com
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
namespace {
//
// Custom exception thrown when an error occurs.
//
class SelectPdfException extends Exception {
public function __toString() {
if ($this->code) {
return "[{$this->code}] {$this->message}\n";
} else {
return "{$this->message}\n";
}
}
}
//
// SelectPdf HTML to PDF REST API - PHP client.
//
class SelectPdf {
//
// SelectPdf API client constructor.
//
// $key - SelectPdf REST API key
//
function __construct($key){
$this->fields = array(
'key' => $key
);
}
//
// Converts a html string to PDF.
//
// $html - a string containing a html document
// $outstream - output stream, if null then the return value is a string
// containing the PDF
//
function convertHtmlString($html, $base_url='', $outstream=null){
if (!$html) {
throw new SelectPdfException("convertHtmlString(): the html parameter must not be empty");
}
$this->fields['html'] = $html;
if (!$base_url) {
$this->fields['base_url'] = $base_url;
}
$postfields = http_build_query($this->fields, '', '&');
return $this->http_post(self::$api_endpoint_convert, $postfields, $outstream);
}
//
// Converts a web page to PDF.
//
// $url - a web page URL
// $outstream - output stream, if null then the return value is a string
// containing the PDF
//
function convertUrl($url, $outstream=null){
$url = trim($url);
if (!$url) {
throw new SelectPdfException("convertUrl(): the url parameter must not be empty");
}
if (!preg_match("/^https?:\/\/.*/i", $url)) {
throw new SelectPdfException("convertUrl(): the URL must start with http:// or https:// (currently '$url')");
}
$this->fields['url'] = $url;
$postfields = http_build_query($this->fields, '', '&');
return $this->http_post(self::$api_endpoint_convert, $postfields, $outstream);
}
//
// Returns the number of available conversions.
//
function availableConversions() {
$arr = array('key' => $this->fields['key']);
$postfields = http_build_query($arr, '', '&');
$response = $this->http_post(self::$api_endpoint_usage, $postfields, NULL);
$json = json_decode($response);
return (int)$json->{'available'};
}
//
// Returns the usage details as an associative array.
//
function usageDetails($get_history = false) {
$arr = array('key' => $this->fields['key']);
if ($get_history) {
$arr['get_history'] = "True";
}
$postfields = http_build_query($arr, '', '&');
$response = $this->http_post(self::$api_endpoint_usage, $postfields, NULL);
$json_array = json_decode($response, true);
return $json_array;
}
//
// Specifies the page size of the generated pdf document. The default value is A4. All possible values are: A1, A2, A3, A4, A5, Letter, HalfLetter, Ledger, Legal.
//
function setPageSize($value) {
$this->fields['page_size'] = $value;
}
//
// Specifies the page orientation of the generated pdf document. The default value is Portrait. All possible values are: Portrait, Landscape.
//
function setPageOrientation($value) {
$this->fields['page_orientation'] = $value;
}
//
// Set page margins. The margins are specified in points. 1 point is 1 / 72 inch. By default all margins are 5pt.
//
function setHorizontalMargin($value) {
$this->fields['margin_right'] = $this->fields['margin_left'] = $value;
}
//
// Set page margins. The margins are specified in points. 1 point is 1 / 72 inch. By default all margins are 5pt.
//
function setVerticalMargin($value) {
$this->fields['margin_top'] = $this->fields['margin_bottom'] = $value;
}
//
// Set page margins. The margins are specified in points. 1 point is 1 / 72 inch. By default all margins are 5pt.
//
function setPageMargins($top, $right, $bottom, $left) {
$this->fields['margin_top'] = $top;
$this->fields['margin_right'] = $right;
$this->fields['margin_bottom'] = $bottom;
$this->fields['margin_left'] = $left;
}
//
// Set page margins. The margins are specified in points. 1 point is 1 / 72 inch. By default all margins are 5pt.
//
function setAllMargins($value) {
$this->fields['margin_top'] = $value;
$this->fields['margin_right'] = $value;
$this->fields['margin_bottom'] = $value;
$this->fields['margin_left'] = $value;
}
function setTimeout($timeout) {
if (is_int($timeout) && $timeout > 0) {
$this->curlopt_timeout = $timeout;
}
}
// ----------------------------------------------------------------------
//
// Private stuff
//
private $fields, $curlopt_timeout;
public static $api_endpoint_convert = "https://selectpdf.com/api2/convert/";
public static $api_endpoint_usage = "https://selectpdf.com/api2/usage/";
private static $missing_curl_error = 'selectpdf.php requires cURL which is not installed on your system.
How to install:
Windows: uncomment/add the "extension=php_curl.dll" line in php.ini
Linux: should be a part of the distribution,
e.g. on Debian/Ubuntu run "sudo apt-get install php5-curl"
You need to restart your web server after installation.
Links:
Installing the PHP/cURL binding: <https://curl.haxx.se/libcurl/php/install.html>
PHP/cURL documentation: <http://www.php.net/manual/en/book.curl.php>';
private function http_post($url, $postfields, $outstream) {
if (!function_exists("curl_init")) {
throw new SelectPdfException(self::$missing_curl_error);
}
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
if (isset($this->curlopt_timeout)) {
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curlopt_timeout);
}
if ($outstream) {
$this->outstream = $outstream;
curl_setopt($ch, CURLOPT_WRITEFUNCTION, array($this, 'receive_to_stream'));
}
$request = curl_getinfo($ch);
//var_dump($request);
$this->http_code = 0;
$this->error = "";
$response = curl_exec($ch);
$this->http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error_str = curl_error($ch);
$error_nr = curl_errno($ch);
curl_close($ch);
if ($error_nr != 0) {
throw new SelectPdfException($error_str, $error_nr);
}
else if ($this->http_code == 200) {
if ($outstream == NULL) {
return $response;
}
} else {
throw new SelectPdfException($this->error ? $this->error : $response, $this->http_code);
}
}
private function receive_to_stream($curl, $data) {
if ($this->http_code == 0) {
$this->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
}
if ($this->http_code >= 400) {
$this->error = $this->error . $data;
return strlen($data);
}
$written = fwrite($this->outstream, $data);
if ($written != strlen($data)) {
if (get_magic_quotes_runtime()) {
throw new SelectPdfException("Cannot write the PDF file because the 'magic_quotes_runtime' setting is enabled.
Please disable it either in your php.ini file, or in your code by calling 'set_magic_quotes_runtime(false)'.");
} else {
throw new SelectPdfException('Writing the PDF file failed. An error occurred.');
}
}
return $written;
}
private function set_or_unset($val, $field) {
if ($val)
$this->fields[$field] = $val;
else
unset($this->fields[$field]);
}
}
}
?>