[3019] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
| 4 | * |
---|
| 5 | * File: $RCSfile: pdflib_adapter.cls.php,v $ |
---|
| 6 | * Created on: 2005-02-28 |
---|
| 7 | * |
---|
| 8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 9 | * |
---|
| 10 | * This library is free software; you can redistribute it and/or |
---|
| 11 | * modify it under the terms of the GNU Lesser General Public |
---|
| 12 | * License as published by the Free Software Foundation; either |
---|
| 13 | * version 2.1 of the License, or (at your option) any later version. |
---|
| 14 | * |
---|
| 15 | * This library is distributed in the hope that it will be useful, |
---|
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * Lesser General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU Lesser General Public License |
---|
| 21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
| 22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
| 23 | * 02111-1307 USA |
---|
| 24 | * |
---|
| 25 | * Alternatively, you may distribute this software under the terms of the |
---|
| 26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
| 27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
| 28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
| 29 | * |
---|
| 30 | * The latest version of DOMPDF might be available at: |
---|
| 31 | * http://www.digitaljunkies.ca/dompdf |
---|
| 32 | * |
---|
| 33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
| 34 | * @copyright 2004 Benj Carson |
---|
| 35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
| 36 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 37 | * @package dompdf |
---|
| 38 | * @version 0.5.1 |
---|
| 39 | * |
---|
| 40 | * Changes |
---|
| 41 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
| 42 | * @version 0.5.1.htischer.20090507 |
---|
| 43 | * - Clarify temp file name, optional debug output for temp file tracking |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | /* $Id: pdflib_adapter.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * PDF rendering interface |
---|
| 50 | * |
---|
| 51 | * PDFLib_Adapter provides a simple, stateless interface to the one |
---|
| 52 | * provided by PDFLib. |
---|
| 53 | * |
---|
| 54 | * Unless otherwise mentioned, all dimensions are in points (1/72 in). |
---|
| 55 | * The coordinate origin is in the top left corner and y values |
---|
| 56 | * increase downwards. |
---|
| 57 | * |
---|
| 58 | * See {@link http://www.pdflib.com/} for more complete documentation |
---|
| 59 | * on the underlying PDFlib functions. |
---|
| 60 | * |
---|
| 61 | * @package dompdf |
---|
| 62 | */ |
---|
| 63 | class PDFLib_Adapter implements Canvas { |
---|
| 64 | |
---|
| 65 | /** |
---|
| 66 | * Dimensions of paper sizes in points |
---|
| 67 | * |
---|
| 68 | * @var array; |
---|
| 69 | */ |
---|
| 70 | static public $PAPER_SIZES = array(); // Set to |
---|
| 71 | // CPDF_Adapter::$PAPER_SIZES below. |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * Fudge factor to adjust reported font heights |
---|
| 75 | * |
---|
| 76 | * CPDF reports larger font heights than PDFLib. This factor |
---|
| 77 | * adjusts the height reported by get_font_height(). |
---|
| 78 | * |
---|
| 79 | * @var float |
---|
| 80 | */ |
---|
| 81 | const FONT_HEIGHT_SCALE = 1.2; |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * Whether to create PDFs in memory or on disk |
---|
| 85 | * |
---|
| 86 | * @var bool |
---|
| 87 | */ |
---|
| 88 | static $IN_MEMORY = true; |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Instance of PDFLib class |
---|
| 92 | * |
---|
| 93 | * @var PDFlib |
---|
| 94 | */ |
---|
| 95 | private $_pdf; |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Name of temporary file used for PDFs created on disk |
---|
| 99 | * |
---|
| 100 | * @var string |
---|
| 101 | */ |
---|
| 102 | private $_file; |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * PDF width, in points |
---|
| 106 | * |
---|
| 107 | * @var float |
---|
| 108 | */ |
---|
| 109 | private $_width; |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * PDF height, in points |
---|
| 113 | * |
---|
| 114 | * @var height |
---|
| 115 | */ |
---|
| 116 | private $_height; |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Last fill colour used |
---|
| 120 | * |
---|
| 121 | * @var array |
---|
| 122 | */ |
---|
| 123 | private $_last_fill_color; |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Last stroke colour used |
---|
| 127 | * |
---|
| 128 | * @var array |
---|
| 129 | */ |
---|
| 130 | private $_last_stroke_color; |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Cache of image handles |
---|
| 134 | * |
---|
| 135 | * @var array |
---|
| 136 | */ |
---|
| 137 | private $_imgs; |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Cache of font handles |
---|
| 141 | * |
---|
| 142 | * @var array |
---|
| 143 | */ |
---|
| 144 | private $_fonts; |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * List of objects (templates) to add to multiple pages |
---|
| 148 | * |
---|
| 149 | * @var array |
---|
| 150 | */ |
---|
| 151 | private $_objs; |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Current page number |
---|
| 155 | * |
---|
| 156 | * @var int |
---|
| 157 | */ |
---|
| 158 | private $_page_number; |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * Total number of pages |
---|
| 162 | * |
---|
| 163 | * @var int |
---|
| 164 | */ |
---|
| 165 | private $_page_count; |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Text to display on every page |
---|
| 169 | * |
---|
| 170 | * @var array |
---|
| 171 | */ |
---|
| 172 | private $_page_text; |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Array of pages for accesing after rendering is initially complete |
---|
| 176 | * |
---|
| 177 | * @var array |
---|
| 178 | */ |
---|
| 179 | private $_pages; |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Class constructor |
---|
| 183 | * |
---|
| 184 | * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or |
---|
| 185 | * an array(xmin,ymin,xmax,ymax) |
---|
| 186 | * @param string $orientation The orientation of the document (either 'landscape' or 'portrait') |
---|
| 187 | */ |
---|
| 188 | function __construct($paper = "letter", $orientation = "portrait") { |
---|
| 189 | if ( is_array($paper) ) |
---|
| 190 | $size = $paper; |
---|
| 191 | else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) |
---|
| 192 | $size = self::$PAPER_SIZES[mb_strtolower($paper)]; |
---|
| 193 | else |
---|
| 194 | $size = self::$PAPER_SIZES["letter"]; |
---|
| 195 | |
---|
| 196 | if ( mb_strtolower($orientation) == "landscape" ) { |
---|
| 197 | $a = $size[3]; |
---|
| 198 | $size[3] = $size[2]; |
---|
| 199 | $size[2] = $a; |
---|
| 200 | } |
---|
| 201 | $this->_width = $size[2] - $size[0]; |
---|
| 202 | $this->_height= $size[3] - $size[1]; |
---|
| 203 | |
---|
| 204 | $this->_pdf = new PDFLib(); |
---|
| 205 | |
---|
| 206 | if ( defined("DOMPDF_PDFLIB_LICENSE") ) |
---|
| 207 | $this->_pdf->set_parameter( "license", DOMPDF_PDFLIB_LICENSE); |
---|
| 208 | |
---|
| 209 | $this->_pdf->set_parameter("textformat", "utf8"); |
---|
| 210 | $this->_pdf->set_parameter("fontwarning", "false"); |
---|
| 211 | |
---|
| 212 | $this->_pdf->set_info("Creator", "DOMPDF Converter"); |
---|
| 213 | |
---|
| 214 | // Silence pedantic warnings about missing TZ settings |
---|
| 215 | if ( function_exists("date_default_timezone_get") ) { |
---|
| 216 | $tz = @date_default_timezone_get(); |
---|
| 217 | date_default_timezone_set("UTC"); |
---|
| 218 | $this->_pdf->set_info("Date", date("Y-m-d")); |
---|
| 219 | date_default_timezone_set($tz); |
---|
| 220 | } else { |
---|
| 221 | $this->_pdf->set_info("Date", date("Y-m-d")); |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | if ( self::$IN_MEMORY ) |
---|
| 225 | $this->_pdf->begin_document("",""); |
---|
| 226 | else { |
---|
| 227 | $this->_file = tempnam(DOMPDF_TEMP_DIR, "libdompdf_pdf_").'.pdf'; |
---|
| 228 | $this->_pdf->begin_document($this->_file,""); |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | $this->_pdf->begin_page_ext($this->_width, $this->_height, ""); |
---|
| 232 | |
---|
| 233 | $this->_page_number = $this->_page_count = 1; |
---|
| 234 | $this->_page_text = array(); |
---|
| 235 | |
---|
| 236 | $this->_imgs = array(); |
---|
| 237 | $this->_fonts = array(); |
---|
| 238 | $this->_objs = array(); |
---|
| 239 | |
---|
| 240 | // Set up font paths |
---|
| 241 | $families = Font_Metrics::get_font_families(); |
---|
| 242 | foreach ($families as $family => $files) { |
---|
| 243 | foreach ($files as $style => $file) { |
---|
| 244 | $face = basename($file); |
---|
| 245 | |
---|
| 246 | // Prefer ttfs to afms |
---|
| 247 | if ( file_exists($file.".ttf") ) { |
---|
| 248 | $outline = "$file.ttf"; |
---|
| 249 | $afm = null; |
---|
| 250 | |
---|
| 251 | } else if ( file_exists($file .".TTF") ) { |
---|
| 252 | $outline = "$file.TTF"; |
---|
| 253 | $afm = null; |
---|
| 254 | |
---|
| 255 | } else if ( file_exists($file . ".pfb") ) { |
---|
| 256 | $outline = "$file.pfb"; |
---|
| 257 | |
---|
| 258 | if ( file_exists($file . ".afm") ) |
---|
| 259 | $afm = "$file.afm"; |
---|
| 260 | |
---|
| 261 | } else if ( file_exists($file . ".PFB") ) { |
---|
| 262 | $outline = "$file.PFB"; |
---|
| 263 | if ( file_exists($file . ".AFM") ) |
---|
| 264 | $afm = "$file.AFM"; |
---|
| 265 | } else |
---|
| 266 | continue; |
---|
| 267 | |
---|
| 268 | $this->_pdf->set_parameter("FontOutline", "\{$face\}=\{$outline\}"); |
---|
| 269 | if ( !is_null($afm) ) |
---|
| 270 | $this->_pdf->set_parameter("FontAFM", "\{$face\}=\{$afm\}"); |
---|
| 271 | } |
---|
| 272 | } |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * Close the pdf |
---|
| 277 | */ |
---|
| 278 | protected function _close() { |
---|
| 279 | $this->_place_objects(); |
---|
| 280 | |
---|
| 281 | // Close all pages |
---|
| 282 | $this->_pdf->suspend_page(""); |
---|
| 283 | for ($p = 1; $p <= $this->_page_count; $p++) { |
---|
| 284 | $this->_pdf->resume_page("pagenumber=$p"); |
---|
| 285 | $this->_pdf->end_page_ext(""); |
---|
| 286 | } |
---|
| 287 | |
---|
| 288 | $this->_pdf->end_document(""); |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | |
---|
| 292 | /** |
---|
| 293 | * Returns the PDFLib instance |
---|
| 294 | * |
---|
| 295 | * @return PDFLib |
---|
| 296 | */ |
---|
| 297 | function get_pdflib() { return $this->_pdf; } |
---|
| 298 | |
---|
| 299 | /** |
---|
| 300 | * Add meta information to the PDF |
---|
| 301 | * |
---|
| 302 | * @param string $label label of the value (Creator, Producter, etc.) |
---|
| 303 | * @param string $value the text to set |
---|
| 304 | */ |
---|
| 305 | function add_info($label, $value) { |
---|
| 306 | $this->_pdf->set_info($label, $value); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * Opens a new 'object' (template in PDFLib-speak) |
---|
| 311 | * |
---|
| 312 | * While an object is open, all drawing actions are recorded to the |
---|
| 313 | * object instead of being drawn on the current page. Objects can |
---|
| 314 | * be added later to a specific page or to several pages. |
---|
| 315 | * |
---|
| 316 | * The return value is an integer ID for the new object. |
---|
| 317 | * |
---|
| 318 | * @see PDFLib_Adapter::close_object() |
---|
| 319 | * @see PDFLib_Adapter::add_object() |
---|
| 320 | * |
---|
| 321 | * @return int |
---|
| 322 | */ |
---|
| 323 | function open_object() { |
---|
| 324 | $this->_pdf->suspend_page(""); |
---|
| 325 | $ret = $this->_pdf->begin_template($this->_width, $this->_height); |
---|
| 326 | $this->_pdf->save(); |
---|
| 327 | $this->_objs[$ret] = array("start_page" => $this->_page_number); |
---|
| 328 | return $ret; |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /** |
---|
| 332 | * Reopen an existing object (NOT IMPLEMENTED) |
---|
| 333 | * |
---|
| 334 | * PDFLib does not seem to support reopening templates. |
---|
| 335 | * |
---|
| 336 | * @param int $object the ID of a previously opened object |
---|
| 337 | */ |
---|
| 338 | function reopen_object($object) { |
---|
| 339 | throw new DOMPDF_Exception("PDFLib does not support reopening objects."); |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /** |
---|
| 343 | * Close the current template |
---|
| 344 | * |
---|
| 345 | * @see PDFLib_Adapter::open_object() |
---|
| 346 | */ |
---|
| 347 | function close_object() { |
---|
| 348 | $this->_pdf->restore(); |
---|
| 349 | $this->_pdf->end_template(); |
---|
| 350 | $this->_pdf->resume_page("pagenumber=".$this->_page_number); |
---|
| 351 | } |
---|
| 352 | |
---|
| 353 | /** |
---|
| 354 | * Adds the specified object to the document |
---|
| 355 | * |
---|
| 356 | * $where can be one of: |
---|
| 357 | * - 'add' add to current page only |
---|
| 358 | * - 'all' add to every page from the current one onwards |
---|
| 359 | * - 'odd' add to all odd numbered pages from now on |
---|
| 360 | * - 'even' add to all even numbered pages from now on |
---|
| 361 | * - 'next' add the object to the next page only |
---|
| 362 | * - 'nextodd' add to all odd numbered pages from the next one |
---|
| 363 | * - 'nexteven' add to all even numbered pages from the next one |
---|
| 364 | * |
---|
| 365 | * @param int $object the object handle returned by open_object() |
---|
| 366 | * @param string $where |
---|
| 367 | */ |
---|
| 368 | function add_object($object, $where = 'all') { |
---|
| 369 | |
---|
| 370 | if ( mb_strpos($where, "next") !== false ) { |
---|
| 371 | $this->_objs[$object]["start_page"]++; |
---|
| 372 | $where = str_replace("next", "", $where); |
---|
| 373 | if ( $where == "" ) |
---|
| 374 | $where = "add"; |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | $this->_objs[$object]["where"] = $where; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | /** |
---|
| 381 | * Stops the specified template from appearing in the document. |
---|
| 382 | * |
---|
| 383 | * The object will stop being displayed on the page following the |
---|
| 384 | * current one. |
---|
| 385 | * |
---|
| 386 | * @param int $object |
---|
| 387 | */ |
---|
| 388 | function stop_object($object) { |
---|
| 389 | |
---|
| 390 | if ( !isset($this->_objs[$object]) ) |
---|
| 391 | return; |
---|
| 392 | |
---|
| 393 | $start = $this->_objs[$object]["start_page"]; |
---|
| 394 | $where = $this->_objs[$object]["where"]; |
---|
| 395 | |
---|
| 396 | // Place the object on this page if required |
---|
| 397 | if ( $this->_page_number >= $start && |
---|
| 398 | (($this->_page_number % 2 == 0 && $where == "even") || |
---|
| 399 | ($this->_page_number % 2 == 1 && $where == "odd") || |
---|
| 400 | ($where == "all")) ) |
---|
| 401 | $this->_pdf->fit_image($object,0,0,""); |
---|
| 402 | |
---|
| 403 | unset($this->_objs[$object]); |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | /** |
---|
| 407 | * Add all active objects to the current page |
---|
| 408 | */ |
---|
| 409 | protected function _place_objects() { |
---|
| 410 | |
---|
| 411 | foreach ( $this->_objs as $obj => $props ) { |
---|
| 412 | $start = $props["start_page"]; |
---|
| 413 | $where = $props["where"]; |
---|
| 414 | |
---|
| 415 | // Place the object on this page if required |
---|
| 416 | if ( $this->_page_number >= $start && |
---|
| 417 | (($this->_page_number % 2 == 0 && $where == "even") || |
---|
| 418 | ($this->_page_number % 2 == 1 && $where == "odd") || |
---|
| 419 | ($where == "all")) ) { |
---|
| 420 | $this->_pdf->fit_image($obj,0,0,""); |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | function get_width() { return $this->_width; } |
---|
| 427 | |
---|
| 428 | function get_height() { return $this->_height; } |
---|
| 429 | |
---|
| 430 | function get_page_number() { return $this->_page_number; } |
---|
| 431 | |
---|
| 432 | function get_page_count() { return $this->_page_count; } |
---|
| 433 | |
---|
| 434 | function set_page_number($num) { $this->_page_number = (int)$num; } |
---|
| 435 | |
---|
| 436 | function set_page_count($count) { $this->_page_count = (int)$count; } |
---|
| 437 | |
---|
| 438 | |
---|
| 439 | /** |
---|
| 440 | * Sets the line style |
---|
| 441 | * |
---|
| 442 | * @param float width |
---|
| 443 | * @param string corner |
---|
| 444 | * @param string join |
---|
| 445 | * @param array dash |
---|
| 446 | */ |
---|
| 447 | protected function _set_line_style($width, $cap, $join, $dash) { |
---|
| 448 | |
---|
| 449 | if ( count($dash) == 1 ) |
---|
| 450 | $dash[] = $dash[0]; |
---|
| 451 | |
---|
| 452 | if ( count($dash) > 1 ) |
---|
| 453 | $this->_pdf->setdashpattern("dasharray={" . join(" ", $dash) . "}"); |
---|
| 454 | else |
---|
| 455 | $this->_pdf->setdash(0,0); |
---|
| 456 | |
---|
| 457 | switch ( $join ) { |
---|
| 458 | case "miter": |
---|
| 459 | $this->_pdf->setlinejoin(0); |
---|
| 460 | break; |
---|
| 461 | |
---|
| 462 | case "round": |
---|
| 463 | $this->_pdf->setlinejoin(1); |
---|
| 464 | break; |
---|
| 465 | |
---|
| 466 | case "bevel": |
---|
| 467 | $this->_pdf->setlinejoin(2); |
---|
| 468 | break; |
---|
| 469 | |
---|
| 470 | default: |
---|
| 471 | break; |
---|
| 472 | } |
---|
| 473 | |
---|
| 474 | switch ( $cap ) { |
---|
| 475 | case "butt": |
---|
| 476 | $this->_pdf->setlinecap(0); |
---|
| 477 | break; |
---|
| 478 | |
---|
| 479 | case "round": |
---|
| 480 | $this->_pdf->setlinecap(1); |
---|
| 481 | break; |
---|
| 482 | |
---|
| 483 | case "square": |
---|
| 484 | $this->_pdf->setlinecap(2); |
---|
| 485 | break; |
---|
| 486 | |
---|
| 487 | default: |
---|
| 488 | break; |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | $this->_pdf->setlinewidth($width); |
---|
| 492 | |
---|
| 493 | } |
---|
| 494 | |
---|
| 495 | /** |
---|
| 496 | * Sets the line color |
---|
| 497 | * |
---|
| 498 | * @param array $color array(r,g,b) |
---|
| 499 | */ |
---|
| 500 | protected function _set_stroke_color($color) { |
---|
| 501 | if($this->_last_stroke_color == $color) |
---|
| 502 | return; |
---|
| 503 | |
---|
| 504 | $this->_last_stroke_color = $color; |
---|
| 505 | |
---|
| 506 | list($r,$g,$b) = $color; |
---|
| 507 | $this->_pdf->setcolor("stroke", "rgb", $r, $g, $b, 0); |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | /** |
---|
| 511 | * Sets the fill color |
---|
| 512 | * |
---|
| 513 | * @param array $color array(r,g,b) |
---|
| 514 | */ |
---|
| 515 | protected function _set_fill_color($color) { |
---|
| 516 | if($this->_last_fill_color == $color) |
---|
| 517 | return; |
---|
| 518 | |
---|
| 519 | $this->_last_fill_color = $color; |
---|
| 520 | |
---|
| 521 | list($r,$g,$b) = $color; |
---|
| 522 | $this->_pdf->setcolor("fill", "rgb", $r, $g, $b, 0); |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | /** |
---|
| 526 | * Loads a specific font and stores the corresponding descriptor. |
---|
| 527 | * |
---|
| 528 | * @param string $font |
---|
| 529 | * @return int the font descriptor for the font |
---|
| 530 | */ |
---|
| 531 | protected function _load_font($font, $encoding = null, $options = "") { |
---|
| 532 | |
---|
| 533 | // Check if the font is a native PDF font |
---|
| 534 | // Embed non-native fonts |
---|
| 535 | $native_fonts = array("courier", "courier-bold", "courier-oblique", "courier-boldoblique", |
---|
| 536 | "helvetica", "helvetica-bold", "helvetica-oblique", "helvetica-boldoblique", |
---|
| 537 | "times-roman", "times-bold", "times-italic", "times-bolditalic", |
---|
| 538 | "symbol", "zapfdinbats"); |
---|
| 539 | |
---|
| 540 | $test = strtolower(basename($font)); |
---|
| 541 | if ( in_array($test, $native_fonts) ) { |
---|
| 542 | $font = basename($font); |
---|
| 543 | |
---|
| 544 | } else { |
---|
| 545 | // Embed non-native fonts |
---|
| 546 | $options .= " embedding=true"; |
---|
| 547 | } |
---|
| 548 | |
---|
| 549 | if ( is_null($encoding) ) { |
---|
| 550 | |
---|
| 551 | // Unicode encoding is only available for the commerical |
---|
| 552 | // version of PDFlib and not PDFlib-Lite |
---|
| 553 | if ( defined("DOMPDF_PDFLIB_LICENSE") ) |
---|
| 554 | $encoding = "unicode"; |
---|
| 555 | else |
---|
| 556 | $encoding = "auto"; |
---|
| 557 | |
---|
| 558 | } |
---|
| 559 | |
---|
| 560 | $key = $font .":". $encoding .":". $options; |
---|
| 561 | |
---|
| 562 | if ( isset($this->_fonts[$key]) ) |
---|
| 563 | return $this->_fonts[$key]; |
---|
| 564 | |
---|
| 565 | else { |
---|
| 566 | |
---|
| 567 | $this->_fonts[$key] = $this->_pdf->load_font($font, $encoding, $options); |
---|
| 568 | return $this->_fonts[$key]; |
---|
| 569 | |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | } |
---|
| 573 | |
---|
| 574 | /** |
---|
| 575 | * Remaps y coords from 4th to 1st quadrant |
---|
| 576 | * |
---|
| 577 | * @param float $y |
---|
| 578 | * @return float |
---|
| 579 | */ |
---|
| 580 | protected function y($y) { return $this->_height - $y; } |
---|
| 581 | |
---|
| 582 | //........................................................................ |
---|
| 583 | |
---|
| 584 | function line($x1, $y1, $x2, $y2, $color, $width, $style = null) { |
---|
| 585 | $this->_set_line_style($width, "butt", "", $style); |
---|
| 586 | $this->_set_stroke_color($color); |
---|
| 587 | |
---|
| 588 | $y1 = $this->y($y1); |
---|
| 589 | $y2 = $this->y($y2); |
---|
| 590 | |
---|
| 591 | $this->_pdf->moveto($x1,$y1); |
---|
| 592 | $this->_pdf->lineto($x2, $y2); |
---|
| 593 | $this->_pdf->stroke(); |
---|
| 594 | } |
---|
| 595 | |
---|
| 596 | //........................................................................ |
---|
| 597 | |
---|
| 598 | function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) { |
---|
| 599 | $this->_set_stroke_color($color); |
---|
| 600 | $this->_set_line_style($width, "square", "miter", $style); |
---|
| 601 | |
---|
| 602 | $y1 = $this->y($y1) - $h; |
---|
| 603 | |
---|
| 604 | $this->_pdf->rect($x1, $y1, $w, $h); |
---|
| 605 | $this->_pdf->stroke(); |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | //........................................................................ |
---|
| 609 | |
---|
| 610 | function filled_rectangle($x1, $y1, $w, $h, $color) { |
---|
| 611 | $this->_set_fill_color($color); |
---|
| 612 | |
---|
| 613 | $y1 = $this->y($y1) - $h; |
---|
| 614 | |
---|
| 615 | $this->_pdf->rect($x1, $y1, $w, $h); |
---|
| 616 | $this->_pdf->fill(); |
---|
| 617 | } |
---|
| 618 | |
---|
| 619 | //........................................................................ |
---|
| 620 | |
---|
| 621 | function polygon($points, $color, $width = null, $style = null, $fill = false) { |
---|
| 622 | |
---|
| 623 | $this->_set_fill_color($color); |
---|
| 624 | $this->_set_stroke_color($color); |
---|
| 625 | |
---|
| 626 | if ( !$fill && isset($width) ) |
---|
| 627 | $this->_set_line_style($width, "square", "miter", $style); |
---|
| 628 | |
---|
| 629 | $y = $this->y(array_pop($points)); |
---|
| 630 | $x = array_pop($points); |
---|
| 631 | $this->_pdf->moveto($x,$y); |
---|
| 632 | |
---|
| 633 | while (count($points) > 1) { |
---|
| 634 | $y = $this->y(array_pop($points)); |
---|
| 635 | $x = array_pop($points); |
---|
| 636 | $this->_pdf->lineto($x,$y); |
---|
| 637 | } |
---|
| 638 | |
---|
| 639 | if ( $fill ) |
---|
| 640 | $this->_pdf->fill(); |
---|
| 641 | else |
---|
| 642 | $this->_pdf->closepath_stroke(); |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | //........................................................................ |
---|
| 646 | |
---|
| 647 | function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false) { |
---|
| 648 | |
---|
| 649 | $this->_set_fill_color($color); |
---|
| 650 | $this->_set_stroke_color($color); |
---|
| 651 | |
---|
| 652 | if ( !$fill && isset($width) ) |
---|
| 653 | $this->_set_line_style($width, "round", "round", $style); |
---|
| 654 | |
---|
| 655 | $y = $this->y($y); |
---|
| 656 | |
---|
| 657 | $this->_pdf->circle($x, $y, $r); |
---|
| 658 | |
---|
| 659 | if ( $fill ) |
---|
| 660 | $this->_pdf->fill(); |
---|
| 661 | else |
---|
| 662 | $this->_pdf->stroke(); |
---|
| 663 | |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | //........................................................................ |
---|
| 667 | |
---|
| 668 | function image($img_url, $img_type, $x, $y, $w, $h) { |
---|
| 669 | $w = (int)$w; |
---|
| 670 | $h = (int)$h; |
---|
| 671 | |
---|
| 672 | $img_type = strtolower($img_type); |
---|
| 673 | |
---|
| 674 | if ( $img_type == "jpg" ) |
---|
| 675 | $img_type = "jpeg"; |
---|
| 676 | |
---|
| 677 | if ( isset($this->_imgs[$img_url]) ) |
---|
| 678 | $img = $this->_imgs[$img_url]; |
---|
| 679 | |
---|
| 680 | else { |
---|
| 681 | |
---|
| 682 | $img = $this->_imgs[$img_url] = $this->_pdf->load_image($img_type, $img_url, ""); |
---|
| 683 | } |
---|
| 684 | |
---|
| 685 | $y = $this->y($y) - $h; |
---|
| 686 | $this->_pdf->fit_image($img, $x, $y, 'boxsize={'. "$w $h" .'} fitmethod=entire'); |
---|
| 687 | |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | //........................................................................ |
---|
| 691 | |
---|
| 692 | function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0, $angle = 0) { |
---|
| 693 | $fh = $this->_load_font($font); |
---|
| 694 | |
---|
| 695 | $this->_pdf->setfont($fh, $size); |
---|
| 696 | $this->_set_fill_color($color); |
---|
| 697 | |
---|
| 698 | $y = $this->y($y) - Font_Metrics::get_font_height($font, $size); |
---|
| 699 | |
---|
| 700 | $adjust = (float)$adjust; |
---|
| 701 | $angle = -(float)$angle; |
---|
| 702 | |
---|
| 703 | //$this->_pdf->fit_textline(utf8_decode($text), $x, $y, "rotate=$angle wordspacing=$adjust"); |
---|
| 704 | $this->_pdf->fit_textline($text, $x, $y, "rotate=$angle wordspacing=$adjust"); |
---|
| 705 | |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | //........................................................................ |
---|
| 709 | |
---|
| 710 | /** |
---|
| 711 | * Add a named destination (similar to <a name="foo">...</a> in html) |
---|
| 712 | * |
---|
| 713 | * @param string $anchorname The name of the named destination |
---|
| 714 | */ |
---|
| 715 | function add_named_dest($anchorname) { |
---|
| 716 | $this->_pdf->add_nameddest($anchorname,""); |
---|
| 717 | } |
---|
| 718 | |
---|
| 719 | //........................................................................ |
---|
| 720 | |
---|
| 721 | /** |
---|
| 722 | * Add a link to the pdf |
---|
| 723 | * |
---|
| 724 | * @param string $url The url to link to |
---|
| 725 | * @param float $x The x position of the link |
---|
| 726 | * @param float $y The y position of the link |
---|
| 727 | * @param float $width The width of the link |
---|
| 728 | * @param float $height The height of the link |
---|
| 729 | */ |
---|
| 730 | function add_link($url, $x, $y, $width, $height) { |
---|
| 731 | |
---|
| 732 | $y = $this->y($y) - $height; |
---|
| 733 | if ( strpos($url, '#') === 0 ) { |
---|
| 734 | // Local link |
---|
| 735 | $name = substr($url,1); |
---|
| 736 | if ( $name ) |
---|
| 737 | $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} destname=". substr($url,1) . " linewidth=0"); |
---|
| 738 | } else { |
---|
| 739 | |
---|
| 740 | list($proto, $host, $path, $file) = explode_url($url); |
---|
| 741 | |
---|
| 742 | if ( $proto == "" || $proto == "file://" ) |
---|
| 743 | return; // Local links are not allowed |
---|
| 744 | $url = build_url($proto, $host, $path, $file); |
---|
| 745 | $url = '{' . rawurldecode($url) . '}'; |
---|
| 746 | |
---|
| 747 | $action = $this->_pdf->create_action("URI", "url=" . $url); |
---|
| 748 | $this->_pdf->create_annotation($x, $y, $x + $width, $y + $height, 'Link', "contents={$url} action={activate=$action} linewidth=0"); |
---|
| 749 | } |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | //........................................................................ |
---|
| 753 | |
---|
| 754 | function get_text_width($text, $font, $size, $spacing = 0) { |
---|
| 755 | $fh = $this->_load_font($font); |
---|
| 756 | |
---|
| 757 | // Determine the additional width due to extra spacing |
---|
| 758 | $num_spaces = mb_substr_count($text," "); |
---|
| 759 | $delta = $spacing * $num_spaces; |
---|
| 760 | return $this->_pdf->stringwidth($text, $fh, $size) + $delta; |
---|
| 761 | } |
---|
| 762 | |
---|
| 763 | //........................................................................ |
---|
| 764 | |
---|
| 765 | function get_font_height($font, $size) { |
---|
| 766 | |
---|
| 767 | $fh = $this->_load_font($font); |
---|
| 768 | |
---|
| 769 | $this->_pdf->setfont($fh, $size); |
---|
| 770 | |
---|
| 771 | $asc = $this->_pdf->get_value("ascender", $fh); |
---|
| 772 | $desc = $this->_pdf->get_value("descender", $fh); |
---|
| 773 | |
---|
| 774 | // $desc is usually < 0, |
---|
| 775 | return self::FONT_HEIGHT_SCALE * $size * ($asc - $desc); |
---|
| 776 | } |
---|
| 777 | |
---|
| 778 | //........................................................................ |
---|
| 779 | |
---|
| 780 | /** |
---|
| 781 | * Writes text at the specified x and y coordinates on every page |
---|
| 782 | * |
---|
| 783 | * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced |
---|
| 784 | * with their current values. |
---|
| 785 | * |
---|
| 786 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 787 | * |
---|
| 788 | * @param float $x |
---|
| 789 | * @param float $y |
---|
| 790 | * @param string $text the text to write |
---|
| 791 | * @param string $font the font file to use |
---|
| 792 | * @param float $size the font size, in points |
---|
| 793 | * @param array $color |
---|
| 794 | * @param float $adjust word spacing adjustment |
---|
| 795 | * @param float $angle angle to write the text at, measured CW starting from the x-axis |
---|
| 796 | */ |
---|
| 797 | function page_text($x, $y, $text, $font, $size, $color = array(0,0,0), |
---|
| 798 | $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) { |
---|
| 799 | $_t = "text"; |
---|
| 800 | $this->_page_text[] = compact("_t", "x", "y", "text", "font", "size", "color", "adjust", "angle"); |
---|
| 801 | } |
---|
| 802 | |
---|
| 803 | //........................................................................ |
---|
| 804 | |
---|
| 805 | /** |
---|
| 806 | * Processes a script on every page |
---|
| 807 | * |
---|
| 808 | * The variables $pdf, $PAGE_NUM, and $PAGE_COUNT are available. |
---|
| 809 | * |
---|
| 810 | * This function can be used to add page numbers to all pages |
---|
| 811 | * after the first one, for example. |
---|
| 812 | * |
---|
| 813 | * @param string $code the script code |
---|
| 814 | * @param string $type the language type for script |
---|
| 815 | */ |
---|
| 816 | function page_script($code, $type = "text/php") { |
---|
| 817 | $_t = "script"; |
---|
| 818 | $this->_page_text[] = compact("_t", "code", "type"); |
---|
| 819 | } |
---|
| 820 | |
---|
| 821 | //........................................................................ |
---|
| 822 | |
---|
| 823 | function new_page() { |
---|
| 824 | |
---|
| 825 | // Add objects to the current page |
---|
| 826 | $this->_place_objects(); |
---|
| 827 | |
---|
| 828 | $this->_pdf->suspend_page(""); |
---|
| 829 | $this->_pdf->begin_page_ext($this->_width, $this->_height, ""); |
---|
| 830 | $this->_page_number = ++$this->_page_count; |
---|
| 831 | |
---|
| 832 | } |
---|
| 833 | |
---|
| 834 | //........................................................................ |
---|
| 835 | |
---|
| 836 | /** |
---|
| 837 | * Add text to each page after rendering is complete |
---|
| 838 | */ |
---|
| 839 | protected function _add_page_text() { |
---|
| 840 | |
---|
| 841 | if ( !count($this->_page_text) ) |
---|
| 842 | return; |
---|
| 843 | |
---|
| 844 | $this->_pdf->suspend_page(""); |
---|
| 845 | |
---|
| 846 | for ($p = 1; $p <= $this->_page_count; $p++) { |
---|
| 847 | $this->_pdf->resume_page("pagenumber=$p"); |
---|
| 848 | |
---|
| 849 | foreach ($this->_page_text as $pt) { |
---|
| 850 | extract($pt); |
---|
| 851 | |
---|
| 852 | switch ($_t) { |
---|
| 853 | |
---|
| 854 | case "text": |
---|
| 855 | $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"), |
---|
| 856 | array($p, $this->_page_count), $text); |
---|
| 857 | $this->text($x, $y, $text, $font, $size, $color, $adjust, $angle); |
---|
| 858 | break; |
---|
| 859 | |
---|
| 860 | case "script": |
---|
| 861 | if (!$eval) { |
---|
| 862 | $eval = new PHP_Evaluator($this); |
---|
| 863 | } |
---|
| 864 | $eval->evaluate($code, array('PAGE_NUM' => $p, 'PAGE_COUNT' => $this->_page_count)); |
---|
| 865 | break; |
---|
| 866 | } |
---|
| 867 | } |
---|
| 868 | |
---|
| 869 | $this->_pdf->suspend_page(""); |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | $this->_pdf->resume_page("pagenumber=".$this->_page_number); |
---|
| 873 | } |
---|
| 874 | |
---|
| 875 | //........................................................................ |
---|
| 876 | |
---|
| 877 | function stream($filename, $options = null) { |
---|
| 878 | |
---|
| 879 | // Add page text |
---|
| 880 | $this->_add_page_text(); |
---|
| 881 | |
---|
| 882 | if ( isset($options["compress"]) && $options["compress"] != 1 ) |
---|
| 883 | $this->_pdf->set_value("compress", 0); |
---|
| 884 | else |
---|
| 885 | $this->_pdf->set_value("compress", 6); |
---|
| 886 | |
---|
| 887 | $this->_close(); |
---|
| 888 | |
---|
| 889 | if ( self::$IN_MEMORY ) { |
---|
| 890 | $data = $this->_pdf->get_buffer(); |
---|
| 891 | $size = strlen($data); |
---|
| 892 | |
---|
| 893 | } else |
---|
| 894 | $size = filesize($this->_file); |
---|
| 895 | |
---|
| 896 | |
---|
| 897 | $filename = str_replace(array("\n","'"),"", $filename); |
---|
| 898 | $attach = (isset($options["Attachment"]) && $options["Attachment"]) ? "attachment" : "inline"; |
---|
| 899 | |
---|
| 900 | header("Cache-Control: private"); |
---|
| 901 | header("Content-type: application/pdf"); |
---|
| 902 | header("Content-Disposition: $attach; filename=\"$filename\""); |
---|
| 903 | |
---|
| 904 | //header("Content-length: " . $size); |
---|
| 905 | |
---|
| 906 | if ( self::$IN_MEMORY ) |
---|
| 907 | echo $data; |
---|
| 908 | |
---|
| 909 | else { |
---|
| 910 | |
---|
| 911 | // Chunked readfile() |
---|
| 912 | $chunk = (1 << 21); // 2 MB |
---|
| 913 | $fh = fopen($this->_file, "rb"); |
---|
| 914 | if ( !$fh ) |
---|
| 915 | throw new DOMPDF_Exception("Unable to load temporary PDF file: " . $this->_file); |
---|
| 916 | |
---|
| 917 | while ( !feof($fh) ) |
---|
| 918 | echo fread($fh,$chunk); |
---|
| 919 | fclose($fh); |
---|
| 920 | |
---|
| 921 | //debugpng |
---|
| 922 | if (DEBUGPNG) print '[pdflib stream unlink '.$this->_file.']'; |
---|
| 923 | if (!DEBUGKEEPTEMP) |
---|
| 924 | |
---|
| 925 | unlink($this->_file); |
---|
| 926 | $this->_file = null; |
---|
| 927 | } |
---|
| 928 | |
---|
| 929 | flush(); |
---|
| 930 | |
---|
| 931 | |
---|
| 932 | } |
---|
| 933 | |
---|
| 934 | //........................................................................ |
---|
| 935 | |
---|
| 936 | function output($options = null) { |
---|
| 937 | |
---|
| 938 | // Add page text |
---|
| 939 | $this->_add_page_text(); |
---|
| 940 | |
---|
| 941 | if ( isset($options["compress"]) && $options["compress"] != 1 ) |
---|
| 942 | $this->_pdf->set_value("compress", 0); |
---|
| 943 | else |
---|
| 944 | $this->_pdf->set_value("compress", 6); |
---|
| 945 | |
---|
| 946 | $this->_close(); |
---|
| 947 | |
---|
| 948 | if ( self::$IN_MEMORY ) |
---|
| 949 | $data = $this->_pdf->get_buffer(); |
---|
| 950 | |
---|
| 951 | else { |
---|
| 952 | $data = file_get_contents($this->_file); |
---|
| 953 | |
---|
| 954 | //debugpng |
---|
| 955 | if (DEBUGPNG) print '[pdflib output unlink '.$this->_file.']'; |
---|
| 956 | if (!DEBUGKEEPTEMP) |
---|
| 957 | |
---|
| 958 | unlink($this->_file); |
---|
| 959 | $this->_file = null; |
---|
| 960 | } |
---|
| 961 | |
---|
| 962 | return $data; |
---|
| 963 | } |
---|
| 964 | } |
---|
| 965 | |
---|
| 966 | // Workaround for idiotic limitation on statics... |
---|
| 967 | PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES; |
---|
| 968 | ?> |
---|