[ PHPXref.com ] [ Generated: Sun Jul 20 17:19:34 2008 ] [ dompdf 0.5 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/include/ -> pdflib_adapter.cls.php (source)

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


[ Powered by PHPXref - Served by Debian GNU/Linux ]