[1575] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
| 4 | * |
---|
| 5 | * File: $RCSfile: cpdf_adapter.cls.php,v $ |
---|
| 6 | * Created on: 2004-08-04 |
---|
| 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 | * @package dompdf |
---|
| 37 | * @version 0.5.1 |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* $Id: cpdf_adapter.cls.php,v 1.16 2006/07/07 21:31:03 benjcarson Exp $ */ |
---|
| 41 | |
---|
| 42 | // FIXME: Need to sanity check inputs to this class |
---|
| 43 | require_once(DOMPDF_LIB_DIR . "/class.pdf.php"); |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * PDF rendering interface |
---|
| 47 | * |
---|
| 48 | * CPDF_Adapter provides a simple stateless interface to the stateful one |
---|
| 49 | * provided by the Cpdf class. |
---|
| 50 | * |
---|
| 51 | * Unless otherwise mentioned, all dimensions are in points (1/72 in). The |
---|
| 52 | * coordinate origin is in the top left corner, and y values increase |
---|
| 53 | * downwards. |
---|
| 54 | * |
---|
| 55 | * See {@link http://www.ros.co.nz/pdf/} for more complete documentation |
---|
| 56 | * on the underlying {@link Cpdf} class. |
---|
| 57 | * |
---|
| 58 | * @package dompdf |
---|
| 59 | */ |
---|
| 60 | class CPDF_Adapter implements Canvas { |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Dimensions of paper sizes in points |
---|
| 64 | * |
---|
| 65 | * @var array; |
---|
| 66 | */ |
---|
| 67 | static $PAPER_SIZES = array("4a0" => array(0,0,4767.87,6740.79), |
---|
| 68 | "2a0" => array(0,0,3370.39,4767.87), |
---|
| 69 | "a0" => array(0,0,2383.94,3370.39), |
---|
| 70 | "a1" => array(0,0,1683.78,2383.94), |
---|
| 71 | "a2" => array(0,0,1190.55,1683.78), |
---|
| 72 | "a3" => array(0,0,841.89,1190.55), |
---|
| 73 | "a4" => array(0,0,595.28,841.89), |
---|
| 74 | "a5" => array(0,0,419.53,595.28), |
---|
| 75 | "a6" => array(0,0,297.64,419.53), |
---|
| 76 | "a7" => array(0,0,209.76,297.64), |
---|
| 77 | "a8" => array(0,0,147.40,209.76), |
---|
| 78 | "a9" => array(0,0,104.88,147.40), |
---|
| 79 | "a10" => array(0,0,73.70,104.88), |
---|
| 80 | "b0" => array(0,0,2834.65,4008.19), |
---|
| 81 | "b1" => array(0,0,2004.09,2834.65), |
---|
| 82 | "b2" => array(0,0,1417.32,2004.09), |
---|
| 83 | "b3" => array(0,0,1000.63,1417.32), |
---|
| 84 | "b4" => array(0,0,708.66,1000.63), |
---|
| 85 | "b5" => array(0,0,498.90,708.66), |
---|
| 86 | "b6" => array(0,0,354.33,498.90), |
---|
| 87 | "b7" => array(0,0,249.45,354.33), |
---|
| 88 | "b8" => array(0,0,175.75,249.45), |
---|
| 89 | "b9" => array(0,0,124.72,175.75), |
---|
| 90 | "b10" => array(0,0,87.87,124.72), |
---|
| 91 | "c0" => array(0,0,2599.37,3676.54), |
---|
| 92 | "c1" => array(0,0,1836.85,2599.37), |
---|
| 93 | "c2" => array(0,0,1298.27,1836.85), |
---|
| 94 | "c3" => array(0,0,918.43,1298.27), |
---|
| 95 | "c4" => array(0,0,649.13,918.43), |
---|
| 96 | "c5" => array(0,0,459.21,649.13), |
---|
| 97 | "c6" => array(0,0,323.15,459.21), |
---|
| 98 | "c7" => array(0,0,229.61,323.15), |
---|
| 99 | "c8" => array(0,0,161.57,229.61), |
---|
| 100 | "c9" => array(0,0,113.39,161.57), |
---|
| 101 | "c10" => array(0,0,79.37,113.39), |
---|
| 102 | "ra0" => array(0,0,2437.80,3458.27), |
---|
| 103 | "ra1" => array(0,0,1729.13,2437.80), |
---|
| 104 | "ra2" => array(0,0,1218.90,1729.13), |
---|
| 105 | "ra3" => array(0,0,864.57,1218.90), |
---|
| 106 | "ra4" => array(0,0,609.45,864.57), |
---|
| 107 | "sra0" => array(0,0,2551.18,3628.35), |
---|
| 108 | "sra1" => array(0,0,1814.17,2551.18), |
---|
| 109 | "sra2" => array(0,0,1275.59,1814.17), |
---|
| 110 | "sra3" => array(0,0,907.09,1275.59), |
---|
| 111 | "sra4" => array(0,0,637.80,907.09), |
---|
| 112 | "letter" => array(0,0,612.00,792.00), |
---|
| 113 | "legal" => array(0,0,612.00,1008.00), |
---|
| 114 | "ledger" => array(0,0,1224.00, 792.00), |
---|
| 115 | "tabloid" => array(0,0,792.00, 1224.00), |
---|
| 116 | "executive" => array(0,0,521.86,756.00), |
---|
| 117 | "folio" => array(0,0,612.00,936.00), |
---|
| 118 | "commerical #10 envelope" => array(0,0,684,297), |
---|
| 119 | "catalog #10 1/2 envelope" => array(0,0,648,864), |
---|
| 120 | "8.5x11" => array(0,0,612.00,792.00), |
---|
| 121 | "8.5x14" => array(0,0,612.00,1008.0), |
---|
| 122 | "11x17" => array(0,0,792.00, 1224.00)); |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Instance of Cpdf class |
---|
| 127 | * |
---|
| 128 | * @var Cpdf |
---|
| 129 | */ |
---|
| 130 | private $_pdf; |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * PDF width, in points |
---|
| 134 | * |
---|
| 135 | * @var float |
---|
| 136 | */ |
---|
| 137 | private $_width; |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * PDF height, in points |
---|
| 141 | * |
---|
| 142 | * @var float; |
---|
| 143 | */ |
---|
| 144 | private $_height; |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Current page number |
---|
| 148 | * |
---|
| 149 | * @var int |
---|
| 150 | */ |
---|
| 151 | private $_page_number; |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Total number of pages |
---|
| 155 | * |
---|
| 156 | * @var int |
---|
| 157 | */ |
---|
| 158 | private $_page_count; |
---|
| 159 | |
---|
| 160 | /** |
---|
| 161 | * Text to display on every page |
---|
| 162 | * |
---|
| 163 | * @var array |
---|
| 164 | */ |
---|
| 165 | private $_page_text; |
---|
| 166 | |
---|
| 167 | /** |
---|
| 168 | * Array of pages for accesing after rendering is initially complete |
---|
| 169 | * |
---|
| 170 | * @var array |
---|
| 171 | */ |
---|
| 172 | private $_pages; |
---|
| 173 | |
---|
| 174 | /** |
---|
| 175 | * Array of temporary cached images to be deleted when processing is complete |
---|
| 176 | * |
---|
| 177 | * @var array |
---|
| 178 | */ |
---|
| 179 | private $_image_cache; |
---|
| 180 | |
---|
| 181 | /** |
---|
| 182 | * Class constructor |
---|
| 183 | * |
---|
| 184 | * @param mixed $paper The size of paper to use in this PDF ({@link CPDF_Adapter::$PAPER_SIZES}) |
---|
| 185 | * @param string $orientation The orienation of the document (either 'landscape' or 'portrait') |
---|
| 186 | */ |
---|
| 187 | function __construct($paper = "letter", $orientation = "portrait") { |
---|
| 188 | |
---|
| 189 | if ( is_array($paper) ) |
---|
| 190 | $size = $paper; |
---|
| 191 | else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) |
---|
| 192 | $size = self::$PAPER_SIZES[$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 | |
---|
| 202 | $this->_pdf = new Cpdf($size); |
---|
| 203 | $this->_pdf->addInfo("Creator", "DOMPDF Converter"); |
---|
| 204 | |
---|
| 205 | // Silence pedantic warnings about missing TZ settings |
---|
| 206 | if ( function_exists("date_default_timezone_get") ) { |
---|
| 207 | $tz = @date_default_timezone_get(); |
---|
| 208 | date_default_timezone_set("UTC"); |
---|
| 209 | $this->_pdf->addInfo("CreationDate", date("Y-m-d")); |
---|
| 210 | date_default_timezone_set($tz); |
---|
| 211 | |
---|
| 212 | } else { |
---|
| 213 | $this->_pdf->addInfo("CreationDate", date("Y-m-d")); |
---|
| 214 | } |
---|
| 215 | |
---|
| 216 | $this->_width = $size[2] - $size[0]; |
---|
| 217 | $this->_height= $size[3] - $size[1]; |
---|
| 218 | $this->_pdf->openHere('Fit'); |
---|
| 219 | |
---|
| 220 | $this->_page_number = $this->_page_count = 1; |
---|
| 221 | $this->_page_text = array(); |
---|
| 222 | |
---|
| 223 | $this->_pages = array($this->_pdf->getFirstPageId()); |
---|
| 224 | |
---|
| 225 | $this->_image_cache = array(); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /** |
---|
| 229 | * Class destructor |
---|
| 230 | * |
---|
| 231 | * Deletes all temporary image files |
---|
| 232 | */ |
---|
| 233 | function __destruct() { |
---|
| 234 | foreach ($this->_image_cache as $img) { |
---|
| 235 | unlink($img); |
---|
| 236 | } |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Returns the Cpdf instance |
---|
| 241 | * |
---|
| 242 | * @return Cpdf |
---|
| 243 | */ |
---|
| 244 | function get_cpdf() { return $this->_pdf; } |
---|
| 245 | |
---|
| 246 | /** |
---|
| 247 | * Opens a new 'object' |
---|
| 248 | * |
---|
| 249 | * While an object is open, all drawing actions are recored in the object, |
---|
| 250 | * as opposed to being drawn on the current page. Objects can be added |
---|
| 251 | * later to a specific page or to several pages. |
---|
| 252 | * |
---|
| 253 | * The return value is an integer ID for the new object. |
---|
| 254 | * |
---|
| 255 | * @see CPDF_Adapter::close_object() |
---|
| 256 | * @see CPDF_Adapter::add_object() |
---|
| 257 | * |
---|
| 258 | * @return int |
---|
| 259 | */ |
---|
| 260 | function open_object() { |
---|
| 261 | $ret = $this->_pdf->openObject(); |
---|
| 262 | $this->_pdf->saveState(); |
---|
| 263 | return $ret; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | /** |
---|
| 267 | * Reopens an existing 'object' |
---|
| 268 | * |
---|
| 269 | * @see CPDF_Adapter::open_object() |
---|
| 270 | * @param int $object the ID of a previously opened object |
---|
| 271 | */ |
---|
| 272 | function reopen_object($object) { |
---|
| 273 | $this->_pdf->reopenObject($object); |
---|
| 274 | $this->_pdf->saveState(); |
---|
| 275 | } |
---|
| 276 | |
---|
| 277 | /** |
---|
| 278 | * Closes the current 'object' |
---|
| 279 | * |
---|
| 280 | * @see CPDF_Adapter::open_object() |
---|
| 281 | */ |
---|
| 282 | function close_object() { |
---|
| 283 | $this->_pdf->restoreState(); |
---|
| 284 | $this->_pdf->closeObject(); |
---|
| 285 | } |
---|
| 286 | |
---|
| 287 | /** |
---|
| 288 | * Adds a specified 'object' to the document |
---|
| 289 | * |
---|
| 290 | * $object int specifying an object created with {@link |
---|
| 291 | * CPDF_Adapter::open_object()}. $where can be one of: |
---|
| 292 | * - 'add' add to current page only |
---|
| 293 | * - 'all' add to every page from the current one onwards |
---|
| 294 | * - 'odd' add to all odd numbered pages from now on |
---|
| 295 | * - 'even' add to all even numbered pages from now on |
---|
| 296 | * - 'next' add the object to the next page only |
---|
| 297 | * - 'nextodd' add to all odd numbered pages from the next one |
---|
| 298 | * - 'nexteven' add to all even numbered pages from the next one |
---|
| 299 | * |
---|
| 300 | * @see Cpdf::addObject() |
---|
| 301 | * |
---|
| 302 | * @param int $object |
---|
| 303 | * @param string $where |
---|
| 304 | */ |
---|
| 305 | function add_object($object, $where = 'all') { |
---|
| 306 | $this->_pdf->addObject($object, $where); |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | /** |
---|
| 310 | * Stops the specified 'object' from appearing in the document. |
---|
| 311 | * |
---|
| 312 | * The object will stop being displayed on the page following the current |
---|
| 313 | * one. |
---|
| 314 | * |
---|
| 315 | * @param int $object |
---|
| 316 | */ |
---|
| 317 | function stop_object($object) { |
---|
| 318 | $this->_pdf->stopObject($object); |
---|
| 319 | } |
---|
| 320 | |
---|
| 321 | /** |
---|
| 322 | * @access private |
---|
| 323 | */ |
---|
| 324 | function serialize_object($id) { |
---|
| 325 | // Serialize the pdf object's current state for retrieval later |
---|
| 326 | return $this->_pdf->serializeObject($id); |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | /** |
---|
| 330 | * @access private |
---|
| 331 | */ |
---|
| 332 | function reopen_serialized_object($obj) { |
---|
| 333 | return $this->_pdf->restoreSerializedObject($obj); |
---|
| 334 | } |
---|
| 335 | |
---|
| 336 | //........................................................................ |
---|
| 337 | |
---|
| 338 | /** |
---|
| 339 | * Returns the PDF's width in points |
---|
| 340 | * @return float |
---|
| 341 | */ |
---|
| 342 | function get_width() { return $this->_width; } |
---|
| 343 | |
---|
| 344 | /** |
---|
| 345 | * Returns the PDF's height in points |
---|
| 346 | * @return float |
---|
| 347 | */ |
---|
| 348 | function get_height() { return $this->_height; } |
---|
| 349 | |
---|
| 350 | /** |
---|
| 351 | * Returns the current page number |
---|
| 352 | * @return int |
---|
| 353 | */ |
---|
| 354 | function get_page_number() { return $this->_page_number; } |
---|
| 355 | |
---|
| 356 | /** |
---|
| 357 | * Returns the total number of pages in the document |
---|
| 358 | * @return int |
---|
| 359 | */ |
---|
| 360 | function get_page_count() { return $this->_page_count; } |
---|
| 361 | |
---|
| 362 | /** |
---|
| 363 | * Sets the current page number |
---|
| 364 | * |
---|
| 365 | * @param int $num |
---|
| 366 | */ |
---|
| 367 | function set_page_number($num) { $this->_page_number = $num; } |
---|
| 368 | |
---|
| 369 | /** |
---|
| 370 | * Sets the page count |
---|
| 371 | * |
---|
| 372 | * @param int $count |
---|
| 373 | */ |
---|
| 374 | function set_page_count($count) { $this->_page_count = $count; } |
---|
| 375 | |
---|
| 376 | /** |
---|
| 377 | * Sets the stroke colour |
---|
| 378 | * |
---|
| 379 | * See {@link Style::set_colour()} for the format of the color array. |
---|
| 380 | * @param array $color |
---|
| 381 | */ |
---|
| 382 | protected function _set_stroke_color($color) { |
---|
| 383 | list($r, $g, $b) = $color; |
---|
| 384 | $this->_pdf->setStrokeColor($r, $g, $b); |
---|
| 385 | } |
---|
| 386 | |
---|
| 387 | /** |
---|
| 388 | * Sets the fill colour |
---|
| 389 | * |
---|
| 390 | * See {@link Style::set_colour()} for the format of the colour array. |
---|
| 391 | * @param array $color |
---|
| 392 | */ |
---|
| 393 | protected function _set_fill_color($color) { |
---|
| 394 | list($r, $g, $b) = $color; |
---|
| 395 | $this->_pdf->setColor($r, $g, $b); |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | /** |
---|
| 399 | * Sets line transparency |
---|
| 400 | * @see Cpdf::setLineTransparency() |
---|
| 401 | * |
---|
| 402 | * Valid blend modes are (case-sensitive): |
---|
| 403 | * |
---|
| 404 | * Normal, Multiply, Screen, Overlay, Darken, Lighten, |
---|
| 405 | * ColorDodge, ColorBurn, HardLight, SoftLight, Difference, |
---|
| 406 | * Exclusion |
---|
| 407 | * |
---|
| 408 | * @param string $mode the blending mode to use |
---|
| 409 | * @param float $opacity 0.0 fully transparent, 1.0 fully opaque |
---|
| 410 | */ |
---|
| 411 | protected function _set_line_transparency($mode, $opacity) { |
---|
| 412 | $this->_pdf->setLineTransparency($mode, $opacity); |
---|
| 413 | } |
---|
| 414 | |
---|
| 415 | /** |
---|
| 416 | * Sets fill transparency |
---|
| 417 | * @see Cpdf::setFillTransparency() |
---|
| 418 | * |
---|
| 419 | * Valid blend modes are (case-sensitive): |
---|
| 420 | * |
---|
| 421 | * Normal, Multiply, Screen, Overlay, Darken, Lighten, |
---|
| 422 | * ColorDogde, ColorBurn, HardLight, SoftLight, Difference, |
---|
| 423 | * Exclusion |
---|
| 424 | * |
---|
| 425 | * @param string $mode the blending mode to use |
---|
| 426 | * @param float $opacity 0.0 fully transparent, 1.0 fully opaque |
---|
| 427 | */ |
---|
| 428 | protected function _set_fill_transparency($mode, $opacity) { |
---|
| 429 | $this->_pdf->setFillTransparency($mode, $opacity); |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | /** |
---|
| 433 | * Sets the line style |
---|
| 434 | * |
---|
| 435 | * @see Cpdf::setLineStyle() |
---|
| 436 | * |
---|
| 437 | * @param float width |
---|
| 438 | * @param string cap |
---|
| 439 | * @param string join |
---|
| 440 | * @param array dash |
---|
| 441 | */ |
---|
| 442 | protected function _set_line_style($width, $cap, $join, $dash) { |
---|
| 443 | $this->_pdf->setLineStyle($width, $cap, $join, $dash); |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | //........................................................................ |
---|
| 447 | |
---|
| 448 | |
---|
| 449 | /** |
---|
| 450 | * Remaps y coords from 4th to 1st quadrant |
---|
| 451 | * |
---|
| 452 | * @param float $y |
---|
| 453 | * @return float |
---|
| 454 | */ |
---|
| 455 | protected function y($y) { return $this->_height - $y; } |
---|
| 456 | |
---|
| 457 | // Canvas implementation |
---|
| 458 | |
---|
| 459 | function line($x1, $y1, $x2, $y2, $color, $width, $style = array(), |
---|
| 460 | $blend = "Normal", $opacity = 1.0) { |
---|
| 461 | //pre_r(compact("x1", "y1", "x2", "y2", "color", "width", "style")); |
---|
| 462 | |
---|
| 463 | $this->_set_stroke_color($color); |
---|
| 464 | $this->_set_line_style($width, "butt", "", $style); |
---|
| 465 | $this->_set_line_transparency($blend, $opacity); |
---|
| 466 | |
---|
| 467 | $this->_pdf->line($x1, $this->y($y1), |
---|
| 468 | $x2, $this->y($y2)); |
---|
| 469 | } |
---|
| 470 | |
---|
| 471 | //........................................................................ |
---|
| 472 | |
---|
| 473 | /** |
---|
| 474 | * Convert a GIF image to a PNG image |
---|
| 475 | * |
---|
| 476 | * @return string The url of the newly converted image |
---|
| 477 | */ |
---|
| 478 | protected function _convert_gif_to_png($image_url) { |
---|
| 479 | global $_dompdf_warnings; |
---|
| 480 | |
---|
| 481 | if ( !function_exists("imagecreatefromgif") ) { |
---|
| 482 | $_dompdf_warnings[] = "Function imagecreatefromgif() not found. Cannot convert gif image: $image_url."; |
---|
| 483 | return DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 484 | } |
---|
| 485 | |
---|
| 486 | $old_err = set_error_handler("record_warnings"); |
---|
| 487 | $im = imagecreatefromgif($image_url); |
---|
| 488 | |
---|
| 489 | if ( $im ) { |
---|
| 490 | imageinterlace($im, 0); |
---|
| 491 | |
---|
| 492 | $filename = tempnam(DOMPDF_TEMP_DIR, "dompdf_img_"); |
---|
| 493 | imagepng($im, $filename); |
---|
| 494 | |
---|
| 495 | } else { |
---|
| 496 | $filename = DOMPDF_LIB_DIR . "/res/broken_image.png"; |
---|
| 497 | |
---|
| 498 | } |
---|
| 499 | |
---|
| 500 | restore_error_handler(); |
---|
| 501 | |
---|
| 502 | $this->_image_cache[] = $filename; |
---|
| 503 | |
---|
| 504 | return $filename; |
---|
| 505 | |
---|
| 506 | } |
---|
| 507 | |
---|
| 508 | function rectangle($x1, $y1, $w, $h, $color, $width, $style = array(), |
---|
| 509 | $blend = "Normal", $opacity = 1.0) { |
---|
| 510 | |
---|
| 511 | $this->_set_stroke_color($color); |
---|
| 512 | $this->_set_line_style($width, "square", "miter", $style); |
---|
| 513 | $this->_set_line_transparency($blend, $opacity); |
---|
| 514 | |
---|
| 515 | $this->_pdf->rectangle($x1, $this->y($y1) - $h, $w, $h); |
---|
| 516 | } |
---|
| 517 | |
---|
| 518 | //........................................................................ |
---|
| 519 | |
---|
| 520 | function filled_rectangle($x1, $y1, $w, $h, $color, $blend = "Normal", $opacity = 1.0) { |
---|
| 521 | |
---|
| 522 | $this->_set_fill_color($color); |
---|
| 523 | $this->_set_line_style(1, "square", "miter", array()); |
---|
| 524 | $this->_set_line_transparency($blend, $opacity); |
---|
| 525 | $this->_set_fill_transparency($blend, $opacity); |
---|
| 526 | |
---|
| 527 | $this->_pdf->filledRectangle($x1, $this->y($y1) - $h, $w, $h); |
---|
| 528 | } |
---|
| 529 | |
---|
| 530 | //........................................................................ |
---|
| 531 | |
---|
| 532 | function polygon($points, $color, $width = null, $style = array(), |
---|
| 533 | $fill = false, $blend = "Normal", $opacity = 1.0) { |
---|
| 534 | |
---|
| 535 | $this->_set_fill_color($color); |
---|
| 536 | $this->_set_stroke_color($color); |
---|
| 537 | |
---|
| 538 | $this->_set_line_transparency($blend, $opacity); |
---|
| 539 | $this->_set_fill_transparency($blend, $opacity); |
---|
| 540 | |
---|
| 541 | if ( !$fill && isset($width) ) |
---|
| 542 | $this->_set_line_style($width, "square", "miter", $style); |
---|
| 543 | |
---|
| 544 | // Adjust y values |
---|
| 545 | for ( $i = 1; $i < count($points); $i += 2) |
---|
| 546 | $points[$i] = $this->y($points[$i]); |
---|
| 547 | |
---|
| 548 | $this->_pdf->polygon($points, count($points) / 2, $fill); |
---|
| 549 | } |
---|
| 550 | |
---|
| 551 | //........................................................................ |
---|
| 552 | |
---|
| 553 | function circle($x, $y, $r1, $color, $width = null, $style = null, |
---|
| 554 | $fill = false, $blend = "Normal", $opacity = 1.0) { |
---|
| 555 | |
---|
| 556 | $this->_set_fill_color($color); |
---|
| 557 | $this->_set_stroke_color($color); |
---|
| 558 | |
---|
| 559 | $this->_set_line_transparency($blend, $opacity); |
---|
| 560 | $this->_set_fill_transparency($blend, $opacity); |
---|
| 561 | |
---|
| 562 | if ( !$fill && isset($width) ) |
---|
| 563 | $this->_set_line_style($width, "round", "round", $style); |
---|
| 564 | |
---|
| 565 | $this->_pdf->filledEllipse($x, $this->y($y), $r1, 0, 0, 8, 0, 360, 1, $fill); |
---|
| 566 | |
---|
| 567 | } |
---|
| 568 | |
---|
| 569 | //........................................................................ |
---|
| 570 | |
---|
| 571 | function image($img_url, $img_type, $x, $y, $w, $h) { |
---|
| 572 | |
---|
| 573 | $img_type = mb_strtolower($img_type); |
---|
| 574 | |
---|
| 575 | switch ($img_type) { |
---|
| 576 | case "jpeg": |
---|
| 577 | case "jpg": |
---|
| 578 | $this->_pdf->addJpegFromFile($img_url, $x, $this->y($y) - $h, $w, $h); |
---|
| 579 | break; |
---|
| 580 | |
---|
| 581 | case "png": |
---|
| 582 | $this->_pdf->addPngFromFile($img_url, $x, $this->y($y) - $h, $w, $h); |
---|
| 583 | break; |
---|
| 584 | |
---|
| 585 | case "gif": |
---|
| 586 | // Convert gifs to pngs |
---|
| 587 | $img_url = $this->_convert_gif_to_png($img_url); |
---|
| 588 | $this->_pdf->addPngFromFile($img_url, $x, $this->y($y) - $h, $w, $h); |
---|
| 589 | break; |
---|
| 590 | |
---|
| 591 | default: |
---|
| 592 | break; |
---|
| 593 | } |
---|
| 594 | |
---|
| 595 | return; |
---|
| 596 | } |
---|
| 597 | |
---|
| 598 | //........................................................................ |
---|
| 599 | |
---|
| 600 | function text($x, $y, $text, $font, $size, $color = array(0,0,0), |
---|
| 601 | $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) { |
---|
| 602 | |
---|
| 603 | list($r, $g, $b) = $color; |
---|
| 604 | $this->_pdf->setColor($r, $g, $b); |
---|
| 605 | |
---|
| 606 | $this->_set_line_transparency($blend, $opacity); |
---|
| 607 | $this->_set_fill_transparency($blend, $opacity); |
---|
| 608 | $font .= ".afm"; |
---|
| 609 | |
---|
| 610 | $this->_pdf->selectFont($font); |
---|
| 611 | $this->_pdf->addText($x, $this->y($y) - Font_Metrics::get_font_height($font, $size), $size, utf8_decode($text), $angle, $adjust); |
---|
| 612 | |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | //........................................................................ |
---|
| 616 | |
---|
| 617 | /** |
---|
| 618 | * Add a named destination (similar to <a name="foo">...</a> in html) |
---|
| 619 | * |
---|
| 620 | * @param string $anchorname The name of the named destination |
---|
| 621 | */ |
---|
| 622 | function add_named_dest($anchorname) { |
---|
| 623 | $this->_pdf->addDestination($anchorname,"Fit"); |
---|
| 624 | } |
---|
| 625 | |
---|
| 626 | //........................................................................ |
---|
| 627 | |
---|
| 628 | /** |
---|
| 629 | * Add a link to the pdf |
---|
| 630 | * |
---|
| 631 | * @param string $url The url to link to |
---|
| 632 | * @param float $x The x position of the link |
---|
| 633 | * @param float $y The y position of the link |
---|
| 634 | * @param float $width The width of the link |
---|
| 635 | * @param float $height The height of the link |
---|
| 636 | */ |
---|
| 637 | function add_link($url, $x, $y, $width, $height) { |
---|
| 638 | |
---|
| 639 | $y = $this->y($y) - $height; |
---|
| 640 | |
---|
| 641 | if ( strpos($url, '#') === 0 ) { |
---|
| 642 | // Local link |
---|
| 643 | $name = substr($url,1); |
---|
| 644 | if ( $name ) |
---|
| 645 | $this->_pdf->addInternalLink($name, $x, $y, $x + $width, $y + $height); |
---|
| 646 | |
---|
| 647 | } else { |
---|
| 648 | $this->_pdf->addLink(rawurldecode($url), $x, $y, $x + $width, $y + $height); |
---|
| 649 | } |
---|
| 650 | |
---|
| 651 | } |
---|
| 652 | |
---|
| 653 | //........................................................................ |
---|
| 654 | |
---|
| 655 | function get_text_width($text, $font, $size, $spacing = 0) { |
---|
| 656 | $this->_pdf->selectFont($font); |
---|
| 657 | return $this->_pdf->getTextWidth($size, utf8_decode($text), $spacing); |
---|
| 658 | } |
---|
| 659 | |
---|
| 660 | //........................................................................ |
---|
| 661 | |
---|
| 662 | function get_font_height($font, $size) { |
---|
| 663 | $this->_pdf->selectFont($font); |
---|
| 664 | return $this->_pdf->getFontHeight($size); |
---|
| 665 | } |
---|
| 666 | |
---|
| 667 | /** |
---|
| 668 | * Writes text at the specified x and y coordinates on every page |
---|
| 669 | * |
---|
| 670 | * The strings '{PAGE_NUM}' and '{PAGE_COUNT}' are automatically replaced |
---|
| 671 | * with their current values. |
---|
| 672 | * |
---|
| 673 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 674 | * |
---|
| 675 | * @param float $x |
---|
| 676 | * @param float $y |
---|
| 677 | * @param string $text the text to write |
---|
| 678 | * @param string $font the font file to use |
---|
| 679 | * @param float $size the font size, in points |
---|
| 680 | * @param array $color |
---|
| 681 | * @param float $adjust word spacing adjustment |
---|
| 682 | * @param float $angle angle to write the text at, measured CW starting from the x-axis |
---|
| 683 | */ |
---|
| 684 | function page_text($x, $y, $text, $font, $size, $color = array(0,0,0), |
---|
| 685 | $adjust = 0, $angle = 0, $blend = "Normal", $opacity = 1.0) { |
---|
| 686 | |
---|
| 687 | $this->_page_text[] = compact("x", "y", "text", "font", "size", "color", "adjust", "angle"); |
---|
| 688 | } |
---|
| 689 | |
---|
| 690 | //........................................................................ |
---|
| 691 | |
---|
| 692 | function new_page() { |
---|
| 693 | $this->_page_count++; |
---|
| 694 | |
---|
| 695 | $ret = $this->_pdf->newPage(); |
---|
| 696 | $this->_pages[] = $ret; |
---|
| 697 | return $ret; |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | //........................................................................ |
---|
| 701 | |
---|
| 702 | /** |
---|
| 703 | * Add text to each page after rendering is complete |
---|
| 704 | */ |
---|
| 705 | protected function _add_page_text() { |
---|
| 706 | |
---|
| 707 | if ( !count($this->_page_text) ) |
---|
| 708 | return; |
---|
| 709 | |
---|
| 710 | $page_number = 1; |
---|
| 711 | |
---|
| 712 | foreach ($this->_pages as $pid) { |
---|
| 713 | |
---|
| 714 | foreach ($this->_page_text as $pt) { |
---|
| 715 | extract($pt); |
---|
| 716 | |
---|
| 717 | $text = str_replace(array("{PAGE_NUM}","{PAGE_COUNT}"), |
---|
| 718 | array($page_number, $this->_page_count), $text); |
---|
| 719 | |
---|
| 720 | $this->reopen_object($pid); |
---|
| 721 | $this->text($x, $y, $text, $font, $size, $color, $adjust, $angle); |
---|
| 722 | $this->close_object(); |
---|
| 723 | } |
---|
| 724 | |
---|
| 725 | $page_number++; |
---|
| 726 | |
---|
| 727 | } |
---|
| 728 | } |
---|
| 729 | |
---|
| 730 | /** |
---|
| 731 | * Streams the PDF directly to the browser |
---|
| 732 | * |
---|
| 733 | * @param string $filename the name of the PDF file |
---|
| 734 | * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0 |
---|
| 735 | */ |
---|
| 736 | function stream($filename, $options = null) { |
---|
| 737 | // Add page text |
---|
| 738 | $this->_add_page_text(); |
---|
| 739 | |
---|
| 740 | $options["Content-Disposition"] = $filename; |
---|
| 741 | $this->_pdf->stream($options); |
---|
| 742 | } |
---|
| 743 | |
---|
| 744 | //........................................................................ |
---|
| 745 | |
---|
| 746 | /** |
---|
| 747 | * Returns the PDF as a string |
---|
| 748 | * |
---|
| 749 | * @return string |
---|
| 750 | */ |
---|
| 751 | function output($options = null) { |
---|
| 752 | // Add page text |
---|
| 753 | $this->_add_page_text(); |
---|
| 754 | |
---|
| 755 | if ( isset($options["compress"]) && $options["compress"] != 1 ) |
---|
| 756 | $debug = 1; |
---|
| 757 | else |
---|
| 758 | $debug = 0; |
---|
| 759 | |
---|
| 760 | return $this->_pdf->output($debug); |
---|
| 761 | |
---|
| 762 | } |
---|
| 763 | |
---|
| 764 | //........................................................................ |
---|
| 765 | |
---|
| 766 | /** |
---|
| 767 | * Returns logging messages generated by the Cpdf class |
---|
| 768 | * |
---|
| 769 | * @return string |
---|
| 770 | */ |
---|
| 771 | function get_messages() { return $this->_pdf->messages; } |
---|
| 772 | |
---|
| 773 | } |
---|
| 774 | |
---|
| 775 | ?> |
---|