[2000] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
| 4 | * |
---|
| 5 | * File: $RCSfile$ |
---|
| 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: tcpdf_adapter.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
| 41 | |
---|
| 42 | require_once(DOMPDF_LIB_DIR . '/tcpdf/tcpdf.php'); |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * TCPDF PDF Rendering interface |
---|
| 46 | * |
---|
| 47 | * TCPDF_Adapter provides a simple, stateless interface to TCPDF. |
---|
| 48 | * |
---|
| 49 | * Unless otherwise mentioned, all dimensions are in points (1/72 in). |
---|
| 50 | * The coordinate origin is in the top left corner and y values |
---|
| 51 | * increase downwards. |
---|
| 52 | * |
---|
| 53 | * See {@link http://tcpdf.sourceforge.net} for more information on |
---|
| 54 | * the underlying TCPDF class. |
---|
| 55 | * |
---|
| 56 | * @package dompdf |
---|
| 57 | */ |
---|
| 58 | class TCPDF_Adapter implements Canvas { |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Dimensions of paper sizes in points |
---|
| 62 | * |
---|
| 63 | * @var array; |
---|
| 64 | */ |
---|
| 65 | static public $PAPER_SIZES = array(); // Set to |
---|
| 66 | // CPDF_Adapter::$PAPER_SIZES below. |
---|
| 67 | |
---|
| 68 | |
---|
| 69 | /** |
---|
| 70 | * Instance of the TCPDF class |
---|
| 71 | * |
---|
| 72 | * @var TCPDF |
---|
| 73 | */ |
---|
| 74 | private $_pdf; |
---|
| 75 | |
---|
| 76 | /** |
---|
| 77 | * PDF width in points |
---|
| 78 | * |
---|
| 79 | * @var float |
---|
| 80 | */ |
---|
| 81 | private $_width; |
---|
| 82 | |
---|
| 83 | /** |
---|
| 84 | * PDF height in points |
---|
| 85 | * |
---|
| 86 | * @var float |
---|
| 87 | */ |
---|
| 88 | private $_height; |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Last fill colour used |
---|
| 92 | * |
---|
| 93 | * @var array |
---|
| 94 | */ |
---|
| 95 | private $_last_fill_color; |
---|
| 96 | |
---|
| 97 | /** |
---|
| 98 | * Last stroke colour used |
---|
| 99 | * |
---|
| 100 | * @var array |
---|
| 101 | */ |
---|
| 102 | private $_last_stroke_color; |
---|
| 103 | |
---|
| 104 | /** |
---|
| 105 | * Last line width used |
---|
| 106 | * |
---|
| 107 | * @var float |
---|
| 108 | */ |
---|
| 109 | private $_last_line_width; |
---|
| 110 | |
---|
| 111 | /** |
---|
| 112 | * Total number of pages |
---|
| 113 | * |
---|
| 114 | * @var int |
---|
| 115 | */ |
---|
| 116 | private $_page_count; |
---|
| 117 | |
---|
| 118 | /** |
---|
| 119 | * Text to display on every page |
---|
| 120 | * |
---|
| 121 | * @var array |
---|
| 122 | */ |
---|
| 123 | private $_page_text; |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Array of pages for accessing after initial rendering is complete |
---|
| 127 | * |
---|
| 128 | * @var array |
---|
| 129 | */ |
---|
| 130 | private $_pages; |
---|
| 131 | |
---|
| 132 | /** |
---|
| 133 | * Class constructor |
---|
| 134 | * |
---|
| 135 | * @param mixed $paper The size of paper to use either a string (see {@link CPDF_Adapter::$PAPER_SIZES}) or |
---|
| 136 | * an array(xmin,ymin,xmax,ymax) |
---|
| 137 | * @param string $orientation The orientation of the document (either 'landscape' or 'portrait') |
---|
| 138 | */ |
---|
| 139 | function __construct($paper = "letter", $orientation = "portrait") { |
---|
| 140 | |
---|
| 141 | if ( is_array($paper) ) |
---|
| 142 | $size = $paper; |
---|
| 143 | else if ( isset(self::$PAPER_SIZES[mb_strtolower($paper)]) ) |
---|
| 144 | $size = self::$PAPER_SIZE[$paper]; |
---|
| 145 | else |
---|
| 146 | $size = self::$PAPER_SIZE["letter"]; |
---|
| 147 | |
---|
| 148 | if ( mb_strtolower($orientation) == "landscape" ) { |
---|
| 149 | $a = $size[3]; |
---|
| 150 | $size[3] = $size[2]; |
---|
| 151 | $size[2] = $a; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | $this->_width = $size[2] - $size[0]; |
---|
| 155 | $this->_height = $size[3] - $size[1]; |
---|
| 156 | |
---|
| 157 | $this->_pdf = new TCPDF("P", "pt", array($this->_width, $this->_height)); |
---|
| 158 | $this->_pdf->Setcreator("DOMPDF Converter"); |
---|
| 159 | |
---|
| 160 | $this->_pdf->AddPage(); |
---|
| 161 | |
---|
| 162 | $this->_page_number = $this->_page_count = 1; |
---|
| 163 | $this->_page_text = array(); |
---|
| 164 | |
---|
| 165 | $this->_last_fill_color = |
---|
| 166 | $this->_last_stroke_color = |
---|
| 167 | $this->_last_line_width = null; |
---|
| 168 | |
---|
| 169 | } |
---|
| 170 | |
---|
| 171 | /** |
---|
| 172 | * Remaps y coords from 4th to 1st quadrant |
---|
| 173 | * |
---|
| 174 | * @param float $y |
---|
| 175 | * @return float |
---|
| 176 | */ |
---|
| 177 | protected function y($y) { return $this->_height - $y; } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Sets the stroke colour |
---|
| 181 | * |
---|
| 182 | * @param array $color |
---|
| 183 | */ |
---|
| 184 | protected function _set_stroke_colour($colour) { |
---|
| 185 | $colour[0] = round(255 * $colour[0]); |
---|
| 186 | $colour[1] = round(255 * $colour[1]); |
---|
| 187 | $colour[2] = round(255 * $colour[2]); |
---|
| 188 | |
---|
| 189 | if ( is_null($this->_last_stroke_color) || $color != $this->_last_stroke_color ) { |
---|
| 190 | $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]); |
---|
| 191 | $this->_last_stroke_color = $color; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | /** |
---|
| 197 | * Sets the fill colour |
---|
| 198 | * |
---|
| 199 | * @param array $color |
---|
| 200 | */ |
---|
| 201 | protected function _set_fill_colour($colour) { |
---|
| 202 | $colour[0] = round(255 * $colour[0]); |
---|
| 203 | $colour[1] = round(255 * $colour[1]); |
---|
| 204 | $colour[2] = round(255 * $colour[2]); |
---|
| 205 | |
---|
| 206 | if ( is_null($this->_last_fill_color) || $color != $this->_last_fill_color ) { |
---|
| 207 | $this->_pdf->SetDrawColor($color[0],$color[1],$color[2]); |
---|
| 208 | $this->_last_fill_color = $color; |
---|
| 209 | } |
---|
| 210 | |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | /** |
---|
| 214 | * Return the TCPDF instance |
---|
| 215 | * |
---|
| 216 | * @return TCPDF |
---|
| 217 | */ |
---|
| 218 | function get_tcpdf() { return $this->_pdf; } |
---|
| 219 | |
---|
| 220 | /** |
---|
| 221 | * Returns the current page number |
---|
| 222 | * |
---|
| 223 | * @return int |
---|
| 224 | */ |
---|
| 225 | function get_page_number() { |
---|
| 226 | return $this->_page_number; |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * Returns the total number of pages |
---|
| 231 | * |
---|
| 232 | * @return int |
---|
| 233 | */ |
---|
| 234 | function get_page_count() { |
---|
| 235 | return $this->_page_count; |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Sets the total number of pages |
---|
| 240 | * |
---|
| 241 | * @param int $count |
---|
| 242 | */ |
---|
| 243 | function set_page_count($count) { |
---|
| 244 | $this->_page_count = (int)$count; |
---|
| 245 | } |
---|
| 246 | |
---|
| 247 | /** |
---|
| 248 | * Draws a line from x1,y1 to x2,y2 |
---|
| 249 | * |
---|
| 250 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 251 | * See {@link Cpdf::setLineStyle()} for a description of the format of the |
---|
| 252 | * $style parameter (aka dash). |
---|
| 253 | * |
---|
| 254 | * @param float $x1 |
---|
| 255 | * @param float $y1 |
---|
| 256 | * @param float $x2 |
---|
| 257 | * @param float $y2 |
---|
| 258 | * @param array $color |
---|
| 259 | * @param float $width |
---|
| 260 | * @param array $style |
---|
| 261 | */ |
---|
| 262 | function line($x1, $y1, $x2, $y2, $color, $width, $style = null) { |
---|
| 263 | |
---|
| 264 | if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) { |
---|
| 265 | $this->_pdf->SetLineWidth($width); |
---|
| 266 | $this->_last_line_width = $width; |
---|
| 267 | } |
---|
| 268 | |
---|
| 269 | $this->_set_stroke_colour($color); |
---|
| 270 | |
---|
| 271 | // FIXME: ugh, need to handle different styles here |
---|
| 272 | $this->_pdf->line($x1, $y1, $x2, $y2); |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | /** |
---|
| 276 | * Draws a rectangle at x1,y1 with width w and height h |
---|
| 277 | * |
---|
| 278 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 279 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
---|
| 280 | * parameter (aka dash) |
---|
| 281 | * |
---|
| 282 | * @param float $x1 |
---|
| 283 | * @param float $y1 |
---|
| 284 | * @param float $w |
---|
| 285 | * @param float $h |
---|
| 286 | * @param array $color |
---|
| 287 | * @param float $width |
---|
| 288 | * @param array $style |
---|
| 289 | */ |
---|
| 290 | function rectangle($x1, $y1, $w, $h, $color, $width, $style = null) { |
---|
| 291 | |
---|
| 292 | if ( is_null($this->_last_line_width) || $width != $this->_last_line_width ) { |
---|
| 293 | $this->_pdf->SetLineWidth($width); |
---|
| 294 | $this->_last_line_width = $width; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | $this->_set_stroke_colour($color); |
---|
| 298 | |
---|
| 299 | // FIXME: ugh, need to handle styles here |
---|
| 300 | $this->_pdf->rect($x1, $y1, $w, $h); |
---|
| 301 | |
---|
| 302 | } |
---|
| 303 | |
---|
| 304 | /** |
---|
| 305 | * Draws a filled rectangle at x1,y1 with width w and height h |
---|
| 306 | * |
---|
| 307 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 308 | * |
---|
| 309 | * @param float $x1 |
---|
| 310 | * @param float $y1 |
---|
| 311 | * @param float $w |
---|
| 312 | * @param float $h |
---|
| 313 | * @param array $color |
---|
| 314 | */ |
---|
| 315 | function filled_rectangle($x1, $y1, $w, $h, $color) { |
---|
| 316 | |
---|
| 317 | $this->_set_fill_colour($color); |
---|
| 318 | |
---|
| 319 | // FIXME: ugh, need to handle styles here |
---|
| 320 | $this->_pdf->rect($x1, $y1, $w, $h, "F"); |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /** |
---|
| 324 | * Draws a polygon |
---|
| 325 | * |
---|
| 326 | * The polygon is formed by joining all the points stored in the $points |
---|
| 327 | * array. $points has the following structure: |
---|
| 328 | * <code> |
---|
| 329 | * array(0 => x1, |
---|
| 330 | * 1 => y1, |
---|
| 331 | * 2 => x2, |
---|
| 332 | * 3 => y2, |
---|
| 333 | * ... |
---|
| 334 | * ); |
---|
| 335 | * </code> |
---|
| 336 | * |
---|
| 337 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 338 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
---|
| 339 | * parameter (aka dash) |
---|
| 340 | * |
---|
| 341 | * @param array $points |
---|
| 342 | * @param array $color |
---|
| 343 | * @param float $width |
---|
| 344 | * @param array $style |
---|
| 345 | * @param bool $fill Fills the polygon if true |
---|
| 346 | */ |
---|
| 347 | function polygon($points, $color, $width = null, $style = null, $fill = false) { |
---|
| 348 | // FIXME: FPDF sucks |
---|
| 349 | } |
---|
| 350 | |
---|
| 351 | /** |
---|
| 352 | * Draws a circle at $x,$y with radius $r |
---|
| 353 | * |
---|
| 354 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 355 | * See {@link Cpdf::setLineStyle()} for a description of the $style |
---|
| 356 | * parameter (aka dash) |
---|
| 357 | * |
---|
| 358 | * @param float $x |
---|
| 359 | * @param float $y |
---|
| 360 | * @param float $r |
---|
| 361 | * @param array $color |
---|
| 362 | * @param float $width |
---|
| 363 | * @param array $style |
---|
| 364 | * @param bool $fill Fills the circle if true |
---|
| 365 | */ |
---|
| 366 | function circle($x, $y, $r, $color, $width = null, $style = null, $fill = false); |
---|
| 367 | |
---|
| 368 | /** |
---|
| 369 | * Add an image to the pdf. |
---|
| 370 | * |
---|
| 371 | * The image is placed at the specified x and y coordinates with the |
---|
| 372 | * given width and height. |
---|
| 373 | * |
---|
| 374 | * @param string $img_url the path to the image |
---|
| 375 | * @param string $img_type the type (e.g. extension) of the image |
---|
| 376 | * @param float $x x position |
---|
| 377 | * @param float $y y position |
---|
| 378 | * @param int $w width (in pixels) |
---|
| 379 | * @param int $h height (in pixels) |
---|
| 380 | */ |
---|
| 381 | function image($img_url, $img_type, $x, $y, $w, $h); |
---|
| 382 | |
---|
| 383 | /** |
---|
| 384 | * Writes text at the specified x and y coordinates |
---|
| 385 | * |
---|
| 386 | * See {@link Style::munge_colour()} for the format of the colour array. |
---|
| 387 | * |
---|
| 388 | * @param float $x |
---|
| 389 | * @param float $y |
---|
| 390 | * @param string $text the text to write |
---|
| 391 | * @param string $font the font file to use |
---|
| 392 | * @param float $size the font size, in points |
---|
| 393 | * @param array $color |
---|
| 394 | * @param float $adjust word spacing adjustment |
---|
| 395 | */ |
---|
| 396 | function text($x, $y, $text, $font, $size, $color = array(0,0,0), $adjust = 0); |
---|
| 397 | |
---|
| 398 | /** |
---|
| 399 | * Add a named destination (similar to <a name="foo">...</a> in html) |
---|
| 400 | * |
---|
| 401 | * @param string $anchorname The name of the named destination |
---|
| 402 | */ |
---|
| 403 | function add_named_dest($anchorname); |
---|
| 404 | |
---|
| 405 | /** |
---|
| 406 | * Add a link to the pdf |
---|
| 407 | * |
---|
| 408 | * @param string $url The url to link to |
---|
| 409 | * @param float $x The x position of the link |
---|
| 410 | * @param float $y The y position of the link |
---|
| 411 | * @param float $width The width of the link |
---|
| 412 | * @param float $height The height of the link |
---|
| 413 | */ |
---|
| 414 | function add_link($url, $x, $y, $width, $height); |
---|
| 415 | |
---|
| 416 | /** |
---|
| 417 | * Calculates text size, in points |
---|
| 418 | * |
---|
| 419 | * @param string $text the text to be sized |
---|
| 420 | * @param string $font the desired font |
---|
| 421 | * @param float $size the desired font size |
---|
| 422 | * @param float $spacing word spacing, if any |
---|
| 423 | * @return float |
---|
| 424 | */ |
---|
| 425 | function get_text_width($text, $font, $size, $spacing = 0); |
---|
| 426 | |
---|
| 427 | /** |
---|
| 428 | * Calculates font height, in points |
---|
| 429 | * |
---|
| 430 | * @param string $font |
---|
| 431 | * @param float $size |
---|
| 432 | * @return float |
---|
| 433 | */ |
---|
| 434 | function get_font_height($font, $size); |
---|
| 435 | |
---|
| 436 | |
---|
| 437 | /** |
---|
| 438 | * Starts a new page |
---|
| 439 | * |
---|
| 440 | * Subsequent drawing operations will appear on the new page. |
---|
| 441 | */ |
---|
| 442 | function new_page(); |
---|
| 443 | |
---|
| 444 | /** |
---|
| 445 | * Streams the PDF directly to the browser |
---|
| 446 | * |
---|
| 447 | * @param string $filename the name of the PDF file |
---|
| 448 | * @param array $options associative array, 'Attachment' => 0 or 1, 'compress' => 1 or 0 |
---|
| 449 | */ |
---|
| 450 | function stream($filename, $options = null); |
---|
| 451 | |
---|
| 452 | /** |
---|
| 453 | * Returns the PDF as a string |
---|
| 454 | * |
---|
| 455 | * @param array $options associative array: 'compress' => 1 or 0 |
---|
| 456 | * @return string |
---|
| 457 | */ |
---|
| 458 | function output($options = null); |
---|
| 459 | |
---|
| 460 | } |
---|
| 461 | |
---|
| 462 | // Workaround for idiotic limitation on statics... |
---|
| 463 | PDFLib_Adapter::$PAPER_SIZES = CPDF_Adapter::$PAPER_SIZES; |
---|
| 464 | ?> |
---|