[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/PDF/ -> PDFGenerator.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  include_once dirname(__FILE__) . '/TCPDF.php';
  11  include_once dirname(__FILE__) . '/Frame.php';
  12  
  13  class Vtiger_PDF_Generator {
  14      private $headerViewer = false, $footerViewer = false, $contentViewer = false, $pagerViewer = false;
  15      private $headerFrame, $footerFrame, $contentFrame;
  16      
  17      private $pdf;
  18      
  19      private $isFirstPage = false;
  20      private $isLastPage = false;
  21      private $totalWidth = 0;
  22      private $totalHeight = 0;
  23      
  24  	function __construct() {
  25          $this->pdf = new Vtiger_PDF_TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT);
  26  
  27          $this->pdf->setPrintHeader(false);
  28          $this->pdf->setPrintFooter(false);
  29      }
  30  
  31  	function setHeaderViewer($viewer) {
  32          $this->headerViewer = $viewer;
  33      }
  34  	function setFooterViewer($viewer) {
  35          $this->footerViewer = $viewer;
  36      }
  37  	function setContentViewer($viewer) {
  38          $this->contentViewer = $viewer;
  39      }
  40  	function setPagerViewer($viewer) {
  41          $this->pagerViewer = $viewer;
  42      }
  43  
  44  	function getHeaderFrame() {
  45          return $this->headerFrame;
  46      }
  47  	function getFooterFrame() {
  48          return $this->footerFrame;
  49      }    
  50  	function getContentFrame() {
  51          return $this->contentFrame;
  52      }
  53  
  54  	function getPDF() {
  55          return $this->pdf;
  56      }
  57      
  58  	function onLastPage() {
  59          return $this->isLastPage;
  60      }
  61      
  62  	function onFirstPage() {
  63          return $this->isFirstPage;
  64      }
  65      
  66  	function getTotalWidth() {
  67          return $this->totalWidth;
  68      }
  69      
  70  	function getTotalHeight() {
  71          return $this->totalHeight;
  72      }
  73      
  74  	function createLastPage() {
  75          // Detect if there is a last page already.
  76          if($this->isLastPage) return false;
  77          // Check if the last page is required for adding footer
  78          if(!$this->footerViewer || !$this->footerViewer->onLastPage()) return false;
  79          
  80          $pdf = $this->pdf;
  81          
  82          // Create a new page
  83          $pdf->AddPage();
  84          
  85          $this->isFirstPage = false;
  86          $this->isLastPage = true;
  87          
  88          $margins = $pdf->getMargins();
  89          $totalHeightFooter = $this->footerViewer? $this->footerViewer->totalHeight($this) : 0;
  90  
  91          if ($totalHeightFooter) {
  92              $this->footerFrame = new Vtiger_PDF_Frame();
  93              $this->footerFrame->x = $pdf->GetX();
  94              $this->footerFrame->y = $margins['top'];
  95              $this->footerFrame->h = $totalHeightFooter;
  96              $this->footerFrame->w = $this->totalWidth;
  97              
  98              $this->footerViewer->initDisplay($this);
  99              $this->footerViewer->display($this);
 100          }
 101          if($this->pagerViewer) {
 102              $this->pagerViewer->display($this);
 103          }
 104          return true;
 105      }
 106      
 107  	function createPage($isLastPage = false) {
 108          $pdf = $this->pdf;
 109          
 110          // Create a new page
 111          $pdf->AddPage();
 112          
 113          if($isLastPage) {
 114              $this->isFirstPage = false;
 115              $this->isLastPage = true;
 116          } else {
 117              if($pdf->getPage() > 1) $this->isFirstPage = false;
 118              else $this->isFirstPage = true;
 119          }
 120      
 121          $margins = $pdf->getMargins();
 122          
 123          $this->totalWidth  = $pdf->getPageWidth()-$margins['left']-$margins['right'];
 124          
 125          $this->totalHeight = $totalHeight = $pdf->getPageHeight() - $margins['top'] - $margins['bottom'];
 126          $totalHeightHeader = $this->headerViewer? $this->headerViewer->totalHeight($this) : 0;
 127          $totalHeightFooter = $this->footerViewer? $this->footerViewer->totalHeight($this) : 0;
 128  
 129          $totalHeightContent= $this->contentViewer->totalHeight($this);
 130          if ($totalHeightContent === 0) $totalHeightContent = $totalHeight - $totalHeightHeader - $totalHeightFooter;
 131  
 132          if ($totalHeightHeader) {
 133              $this->headerFrame = new Vtiger_PDF_Frame();
 134              $this->headerFrame->x = $pdf->GetX();
 135              $this->headerFrame->y = $pdf->GetY();
 136              $this->headerFrame->h = $totalHeightHeader;
 137              $this->headerFrame->w = $this->totalWidth;
 138  
 139              $this->headerViewer->initDisplay($this);
 140              $this->headerViewer->display($this);
 141          }
 142          
 143          // ContentViewer
 144          $this->contentFrame = new Vtiger_PDF_Frame();
 145          $this->contentFrame->x = $pdf->GetX();    
 146                  $this->contentFrame->y = $pdf->GetY()+10.0;
 147          
 148          $this->contentFrame->h = $totalHeightContent;
 149          $this->contentFrame->w = $this->totalWidth;
 150  
 151          $this->contentViewer->initDisplay($this);
 152          
 153          if ($totalHeightFooter) {
 154              $this->footerFrame = new Vtiger_PDF_Frame();
 155              $this->footerFrame->x = $pdf->GetX();
 156              $this->footerFrame->y = $totalHeight+$margins['top']-$totalHeightFooter;
 157              $this->footerFrame->h = $totalHeightFooter;
 158              $this->footerFrame->w = $this->totalWidth;
 159  
 160              $this->footerViewer->initDisplay($this);
 161              $this->footerViewer->display($this);
 162          }
 163          
 164          if($this->pagerViewer) {
 165              $this->pagerViewer->display($this);
 166          }
 167      }
 168      
 169  	function generate($name, $outputMode='D') {
 170          $this->contentViewer->display($this);        
 171          $this->pdf->Output($name, $outputMode);
 172      }
 173  
 174  	function getImageSize($file) {
 175          // get image dimensions
 176          $imsize = @getimagesize($file);
 177          if ($imsize === FALSE) {
 178              // encode spaces on filename
 179              $file = str_replace(' ', '%20', $file);
 180              $imsize = @getimagesize($file);
 181              if ($imsize === FALSE) {
 182                  //TODO handle error better.
 183                  //values here are consistent with one that should be max size of logo.
 184                  return array(60, 30, null, null);
 185              }
 186          }
 187          return $imsize;
 188      }
 189  
 190  }
 191  
 192  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1