Location: PHPKode > projects > Php Pdf Factory > Pdf/Document.php
<?

require_once "Etc/Styles.php";
require_once "Etc/Geometry.php";

class PdfDocument {
  var $objects;
  var $currentNode;  //copy od PdfObject->currentNode;
  /**
  * Object containing Color, Stroke Color and line style
  * and stack for them
  *
  * @type resource
  * @access protected
  */
  var $styles;
  /**
  * Object containing geometry routines
  *
  * @type resource
  * @access protected
  */
  var $geometry;
  /**
  * intialize a new document
  * if this is called on an existing document results may be unpredictable, but the existing document would be lost at minimum
  * this function is called automatically by the constructor function
  *
  * @access public
  */
  function PdfDocument (&$objects,$pageSize){

    $this->styles = &new PdfStyles($objects);
    $this->geometry = &new PdfGeometry($objects);

    $this->objects = &$objects;
    $this->objects->createObject('PdfObjectCatalog');

    $this->objects->currentNode->mediaBox($pageSize);
    $this->objects->currentNode->addChild();


    $this->objects->createObject('PdfObjectProcset');

    $this->objects->createObject('PdfObjectInfo');

  }
  /**
  * this will be called at a new page to return the state to what it was on the 
  * end of the previous page, before the stack was closed down
  * This is to get around not being able to have open 'q' across pages
  *
  */
  function saveState(){
    $this->styles->save();
  }

  /**
  * restore a previously saved state
  */
  function restoreState(){
    $this->styles->restore();
  }

  /**
  * functions which can be called to adjust or add to the document
  */

  /**
  * add a link in the document to an external URL
  */
  function addLink($url,$x0,$y0,$x1,$y1){
    $info = array('type'=>'link','url'=>$url,'rect'=>array($x0,$y0,$x1,$y1));
    $this->objects->createObject('PdfObjectAnnotation',$info);
  }

  /**
  * add a link in the document to an internal destination (ie. within the document)
  */
  function addInternalLink($label,$x0,$y0,$x1,$y1){
    $info = array('type'=>'ilink','label'=>$label,'rect'=>array($x0,$y0,$x1,$y1));
    $this->objects->createObject('PdfObjectAnnotation',$info);
  }
  /**
  * create a labelled destination within the document
  */
  function addDestination($label,$style,$a=0,$b=0,$c=0){
    // associates the given label with the destination, it is done this way so that a destination can be specified after
    // it has been linked to
    // styles are the same as the 'openHere' function
    $d = &$this->objects->createObject('PdfObjectDestination',  array('page'=>$this->objects->currentPage->getId(),
                                                                      'type'=>$style,
                                                                      'p1'=>$a,
                                                                      'p2'=>$b,
                                                                      'p3'=>$c));
    // store the label->idf relationship, note that this means that labels can be used only once
    $this->objects->destinations[$label]=$d->getId();
  }
  /**
  * specify where the document should open when it first starts
  */
  function openHere($style,$a=0,$b=0,$c=0){
    // this function will open the document at a specified page, in a specified style
    // the values for style, and the required paramters are:
    // 'XYZ'  left, top, zoom
    // 'Fit'
    // 'FitH' top
    // 'FitV' left
    // 'FitR' left,bottom,right
    // 'FitB'
    // 'FitBH' top
    // 'FitBV' left
    $d = &$this->objects->createObject('PdfObjectDestination',array('page'=>$this->objects->currentPage->getId(),
                                                                    'type'=>$style,
                                                                    'p1'=>$a,
                                                                    'p2'=>$b,
                                                                    'p3'=>$c));
    $this->objects->catalogId->openHere($d->getId());
  }
  /**
  *  Adds new PageTree to the desired position and sets it as currentNode
  *
  *  @param int target page number (not the page's object id!)
  *  @param insert before or after the target page
  *  @param mixed init options
  *  @access public
  */
  function addPageTree ($id=false,$after=true,$options=array()) {
    if (false === $id || !isset($this->objects->pages[$id])) {
//      $options['parent'] = &$this->objects->currentNode;
      $this->objects->currentNode->addChildPages($this->objects->pages[sizeof($this->objects->pages)-1],$after,$options);
    } else {
      $p = &$this->objects->getById($this->objects->pages[$id]);
      $r = &$p->getParent();
//      $options['parent'] = &$r;
      $r->addChildPages($this->objects->pages[$id],$after,$options);
    }
  }
  /**
  *  Adds new page to the current PageTree
  *
  *  @access public
  */
  function newPage($insert=false,$id=0,$pos='after') {
    // if there is a state saved, then go up the stack closing them
    // then on the new page, re-open them with the right setings
    $this->styles->restoreAll();
    if ($insert){
      // the id from the ezPdf class is the id of the contents of the page, not the page object itself
      // query that object to find the parent
      $rid = &$this->objects->getById($id);
      $p = &$this->objects->getById($rid->onPage());
      if (!$this->currentNode->hasChild($p->getId())) {
        $this->objects->currentNode = &$p->getParent();
      }
      $this->objects->currentNode->addChild($p->getId(),$pos=='after');
    } else {
      $this->objects->currentNode->addChild();
    }
    $this->styles->setAll();
    $this->styles->reload();
    // the call to the o_page object set currentContents to the present page, so this can be returned as the page id
    return  $this->objects->currentContents->getId();
  }
  /**
  * add content to the documents info object
  */
  function addInfo($label,$value=0) {
    // this will only work if the label is one of the valid ones.
    // modify this so that arrays can be passed as well.
    // if $label is an array then assume that it is key=>value pairs
    // else assume that they are both scalar, anything else will probably error
    if (is_array($label)){
      foreach ($label as $l=>$v){
        $this->objects->infoObject->add(array($l=>$v));
      }
    } else {
      $this->objects->infoObject->add(array($label=>$value));
    }
  }
  /**
  * set the viewer preferences of the document, it is up to the browser to obey these.
  */
  function setPreferences($label,$value=0){
    // this will only work if the label is one of the valid ones.
    if (is_array($label)){
      foreach ($label as $l=>$v){
        $this->objects->catalogId->viewerPreferences(array($l=>$v));
      }
    } else {
      $this->objects->catalogId->viewerPreferences(array($label=>$value));
    }
  }
}
?>
Return current item: Php Pdf Factory