Spaces:
Running
Running
File size: 15,356 Bytes
971eb1e | 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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 | <?php
// Copyright (C) 2010-2014 pdfcrowd.com
//
// Inspired by code written by Jawaad Mahmood
// <http://www.tokyomuslim.com/2010/04/php-class-to-run-pdfcrowd-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.
//
// Thrown when an error occurs.
//
class PdfcrowdException extends Exception {
// custom string representation of object
public function __toString() {
if ($this->code) {
return "[{$this->code}] {$this->message}\n";
} else {
return "{$this->message}\n";
}
}
}
//
// Pdfcrowd API client.
//
class PdfCrowd {
//
// Pdfcrowd constructor.
//
// $username - your username at Pdfcrowd
// $apikey - your API key
// $hostname - API hostname, defaults to pdfcrowd.com
//
function __construct($username, $apikey, $hostname=null){
if ($hostname)
$this->hostname = $hostname;
else
$this->hostname = self::$api_host;
$this->useSSL(false);
$this->fields = array(
'username' => $username,
'key' => $apikey,
'pdf_scaling_factor' => 1,
'html_zoom' => 200);
$this->proxy_name = null;
$this->proxy_port = null;
$this->proxy_username = "";
$this->proxy_password = "";
$this->user_agent = "pdfcrowd_php_client_".self::$client_version."_(http://pdfcrowd.com)";
}
//
// Converts an in-memory html document.
//
// $src - a string containing a html document
// $outstream - output stream, if null then the return value is a string
// containing the PDF
//
function convertHtml($src, $outstream=null){
if (!$src) {
throw new PdfcrowdException("convertHTML(): the src parameter must not be empty");
}
$this->fields['src'] = $src;
$uri = $this->api_prefix . "/pdf/convert/html/";
$postfields = http_build_query($this->fields, '', '&');
return $this->http_post($uri, $postfields, $outstream);
}
//
// Converts an html file.
//
// $src - a path to an html file
// $outstream - output stream, if null then the return value is a string
// containing the PDF
//
function convertFile($src, $outstream=null) {
$src = trim($src);
if (!file_exists($src)) {
$cwd = getcwd();
throw new PdfcrowdException("convertFile(): '{$src}' not found
Possible reasons:
1. The file is missing.
2. You misspelled the file name.
3. You use a relative file path (e.g. 'index.html') but the current working
directory is somewhere else than you expect: '${cwd}'
Generally, it is safer to use an absolute file path instead of a relative one.
");
}
if (is_dir($src)) {
throw new PdfcrowdException("convertFile(): '{$src}' must be file, not a directory");
}
if (!is_readable($src)) {
throw new PdfcrowdException("convertFile(): cannot read '{$src}', please check if the process has sufficient permissions");
}
if (!filesize($src)) {
throw new PdfcrowdException("convertFile(): '{$src}' must not be empty");
}
if (version_compare(PHP_VERSION, '5.5.0') >= 0) {
$this->fields['src'] = new CurlFile($src);
} else {
$this->fields['src'] = '@' . $src;
}
$uri = $this->api_prefix . "/pdf/convert/html/";
return $this->http_post($uri, $this->fields, $outstream);
}
//
// Converts a web page.
//
// $src - a web page URL
// $outstream - output stream, if null then the return value is a string
// containing the PDF
//
function convertURI($src, $outstream=null){
$src = trim($src);
if (!preg_match("/^https?:\/\/.*/i", $src)) {
throw new PdfcrowdException("convertURI(): the URL must start with http:// or https:// (got '$src')");
}
$this->fields['src'] = $src;
$uri = $this->api_prefix . "/pdf/convert/uri/";
$postfields = http_build_query($this->fields, '', '&');
return $this->http_post($uri, $postfields, $outstream);
}
//
// Returns the number of available conversion tokens.
//
function numTokens() {
$username = $this->fields['username'];
$uri = $this->api_prefix . "/user/{$username}/tokens/";
$arr = array('username' => $this->fields['username'],
'key' => $this->fields['key']);
$postfields = http_build_query($arr, '', '&');
$ntokens = $this->http_post($uri, $postfields, NULL);
return (int)$ntokens;
}
function useSSL($use_ssl) {
if($use_ssl) {
$this->port = self::$https_port;
$this->scheme = 'https';
}
else {
$this->port = self::$http_port;
$this->scheme = 'http';
}
$this->api_prefix = "{$this->scheme}://{$this->hostname}/api";
}
function setPageWidth($value) {
$this->fields['width'] = $value;
}
function setPageHeight($value) {
$this->fields['height'] = $value;
}
function setHorizontalMargin($value) {
$this->fields['margin_right'] = $this->fields['margin_left'] = $value;
}
function setVerticalMargin($value) {
$this->fields['margin_top'] = $this->fields['margin_bottom'] = $value;
}
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;
}
function setEncrypted($val=True) {
$this->set_or_unset($val, 'encrypted');
}
function setUserPassword($pwd) {
$this->set_or_unset($pwd, 'user_pwd');
}
function setOwnerPassword($pwd) {
$this->set_or_unset($pwd, 'owner_pwd');
}
function setNoPrint($val=True) {
$this->set_or_unset($val, 'no_print');
}
function setNoModify($val=True) {
$this->set_or_unset($val, 'no_modify');
}
function setNoCopy($val=True) {
$this->set_or_unset($val, 'no_copy');
}
// constants for setPageLayout()
const SINGLE_PAGE = 1;
const CONTINUOUS = 2;
const CONTINUOUS_FACING = 3;
function setPageLayout($value) {
assert($value > 0 && $value <= 3);
$this->fields['page_layout'] = $value;
}
// constants for setPageMode()
const NONE_VISIBLE = 1;
const THUMBNAILS_VISIBLE = 2;
const FULLSCREEN = 3;
function setPageMode($value) {
assert($value > 0 && $value <= 3);
$this->fields['page_mode'] = $value;
}
function setFooterText($value) {
$this->set_or_unset($value, 'footer_text');
}
function enableImages($value=True) {
$this->set_or_unset(!$value, 'no_images');
}
function enableBackgrounds($value=True) {
$this->set_or_unset(!$value, 'no_backgrounds');
}
function setHtmlZoom($value) {
$this->set_or_unset($value, 'html_zoom');
}
function enableJavaScript($value=True) {
$this->set_or_unset(!$value, 'no_javascript');
}
function enableHyperlinks($value=True) {
$this->set_or_unset(!$value, 'no_hyperlinks');
}
function setDefaultTextEncoding($value) {
$this->set_or_unset($value, 'text_encoding');
}
function usePrintMedia($value=True) {
$this->set_or_unset($value, 'use_print_media');
}
function setMaxPages($value) {
$this->fields['max_pages'] = $value;
}
function enablePdfcrowdLogo($value=True) {
$this->set_or_unset($value, 'pdfcrowd_logo');
}
// constants for setInitialPdfZoomType()
const FIT_WIDTH = 1;
const FIT_HEIGHT = 2;
const FIT_PAGE = 3;
function setInitialPdfZoomType($value) {
assert($value>0 && $value<=3);
$this->fields['initial_pdf_zoom_type'] = $value;
}
function setInitialPdfExactZoom($value) {
$this->fields['initial_pdf_zoom_type'] = 4;
$this->fields['initial_pdf_zoom'] = $value;
}
function setPdfScalingFactor($value) {
$this->fields['pdf_scaling_factor'] = $value;
}
function setAuthor($value) {
$this->fields['author'] = $value;
}
function setFailOnNon200($value) {
$this->fields['fail_on_non200'] = $value;
}
function setFooterHtml($value) {
$this->fields['footer_html'] = $value;
}
function setFooterUrl($value) {
$this->fields['footer_url'] = $value;
}
function setHeaderHtml($value) {
$this->fields['header_html'] = $value;
}
function setHeaderUrl($value) {
$this->fields['header_url'] = $value;
}
function setPageBackgroundColor($value) {
$this->fields['page_background_color'] = $value;
}
function setTransparentBackground($value=True) {
$this->set_or_unset($value, 'transparent_background');
}
function setPageNumberingOffset($value) {
$this->fields['page_numbering_offset'] = $value;
}
function setHeaderFooterPageExcludeList($value) {
$this->fields['header_footer_page_exclude_list'] = $value;
}
function setWatermark($url, $offset_x=0, $offset_y=0) {
$this->fields["watermark_url"] = $url;
$this->fields["watermark_offset_x"] = $offset_x;
$this->fields["watermark_offset_y"] = $offset_y;
}
function setWatermarkRotation($angle) {
$this->fields["watermark_rotation"] = $angle;
}
function setWatermarkInBackground($val=True) {
$this->set_or_unset($val, "watermark_in_background");
}
function setProxy($proxyname, $port, $username="", $password="") {
$this->proxy_name = $proxyname;
$this->proxy_port = $port;
$this->proxy_username = $username;
$this->proxy_password = $password;
}
function setUserAgent($user_agent) {
$this->user_agent = $user_agent;
}
// ----------------------------------------------------------------------
//
// Private stuff
//
private $fields, $scheme, $port, $api_prefix;
public static $client_version = "2.7";
public static $http_port = 80;
public static $https_port = 443;
public static $api_host = 'pdfcrowd.com';
private static $missing_curl = 'pdfcrowd.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: <http://curl.haxx.se/libcurl/php/install.html>
PHP/cURL documentation: <http://cz.php.net/manual/en/book.curl.php>';
private function http_post($url, $postfields, $outstream) {
if (!function_exists("curl_init")) {
throw new PdfcrowdException(self::$missing_curl);
}
$c = curl_init();
curl_setopt($c, CURLOPT_URL,$url);
curl_setopt($c, CURLOPT_HEADER, false);
curl_setopt($c, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_PORT, $this->port);
curl_setopt($c, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($c, CURLOPT_DNS_USE_GLOBAL_CACHE, false);
curl_setopt($c, CURLOPT_USERAGENT, $this->user_agent);
if ($outstream) {
$this->outstream = $outstream;
curl_setopt($c, CURLOPT_WRITEFUNCTION, array($this, 'receive_to_stream'));
}
if ($this->scheme == 'https' && self::$api_host == 'pdfcrowd.com') {
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, true);
} else {
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
}
if ($this->proxy_name) {
curl_setopt($c, CURLOPT_PROXY, $this->proxy_name . ":" . $this->proxy_port);
if ($this->proxy_username) {
curl_setopt($c, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($c, CURLOPT_PROXYUSERPWD, $this->proxy_username . ":" . $this->proxy_password);
}
}
$this->http_code = 0;
$this->error = "";
$response = curl_exec($c);
$this->http_code = curl_getinfo($c, CURLINFO_HTTP_CODE);
$error_str = curl_error($c);
$error_nr = curl_errno($c);
curl_close($c);
if ($error_nr != 0) {
throw new PdfcrowdException($error_str, $error_nr);
}
else if ($this->http_code == 200) {
if ($outstream == NULL) {
return $response;
}
} else {
throw new PdfcrowdException($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 PdfcrowdException("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 PdfcrowdException('Writing the PDF file failed. The disk may be full.');
}
}
return $written;
}
private function set_or_unset($val, $field) {
if ($val)
$this->fields[$field] = $val;
else
unset($this->fields[$field]);
}
}
?>
|