- <?php
- /**
- * Page manager extension with support to Pdf output
- * Require:
- * R&OS pdf class (http://www.ros.co.nz/pdf)
- * sptpl_clsPageManager.php
- *
- * @copyright sptpl_clsPagePdf.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
- * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
- * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsPagePdf.php,v 2.5 2005/03/17 12:46:48 Darvin Exp $
- */
- /*
- * +-------------------------------------------------------------------------+
- * | Sptpl |
- * +-------------------------------------------------------------------------+
- * | Copyright (c) 2003-2005 Andrioli Darvin |
- * | Email <darvin (inside) andrioli (dot) com> |
- * | Web http://www.andrioli.com/en/sptpl.html |
- * | Download http://www.phpclasses.org/browse.html/package/1326.html |
- * | |
- * +-------------------------------------------------------------------------+
- * | This library is free software; you can redistribute it and/or modify |
- * | it under the terms of the GNU Lesser General Public License as |
- * | published by the Free Software Foundation; either version 2 of the |
- * | License, or (at your option) any later version. |
- * | |
- * | This library is distributed in the hope that it will be useful, but |
- * | WITHOUT ANY WARRANTY; without even the implied warranty of |
- * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
- * | Lesser General Public License for more details. |
- * | |
- * | You should have received a copy of the GNU Lesser General Public |
- * | License along with this library; if not, write to the Free Software |
- * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
- * +-------------------------------------------------------------------------+
- */
-
- class CPagePdf extends CPageMgr {
-
- /**
- * Class Pdf instance
- * @var object Cpdf
- */
- var $clsCpdf;
-
- var $OutFile;
- /**
- * The array holds the handlers returnt by Aprint's CreateFont
- * @var array
- * @see _DownloadFont()
- */
- var $FontsId;
- /**
- * Class constructor. Call the parent class (CPageMgr) constructor
- * @param object CDataStorage &$DataMgr Reference to the DataStorage istance
- * @access public
- */
- function CPagePdf(&$DataMgr)
- {
- parent::CPageMgr($DataMgr);
- // set the default margins and page size.
- $this->tblFormatIso=array( 'A0' => array( 'length' => 3370.39, 'width' => 2383.94),
- 'A1' => array( 'length' => 2383.94, 'width' => 1683.78),
- 'A2' => array( 'length' => 1683.78, 'width' => 1190.55),
- 'A3' => array( 'length' => 1190.55, 'width' => 841.89),
- 'A4' => array( 'length' => 841.89, 'width' => 595.28),
- 'A5' => array( 'length' => 595.28, 'width' => 419.53),
- );
-
- $this->PageFormat='A4';
- $this->Margins=array('top' =>20,
- 'bottom' =>20,
- 'left' =>10,
- 'right' =>10);
- // $this->FontsId=array();
- }
-
- /**
- * Create one default font named STANDARD
- * @access private
- */
- function _CreateDefaultFont()
- {
- $q="?"; // It makes happy my editor!!
- $XmlStr="<".$q."xml version='1.0' ".$q.">
- <font id='STANDARD' face='Helvetica.afm' size='12' />
- ";
- $tmpRoot=new CXml2Array();
- $tmpRoot->LoadFromString($XmlStr);
- $this->NewFont($tmpRoot);
- }
-
- /**
- * Load margins setting from the configuration file
- * Default values are set by the class constructor.
- * Extend the base class
- * @param object CXml2Array
- * @access private
- *
- */
- function _SetMargins($Cfg)
- {
- // The parent class parse the configuration and load
- // the information, now using that information I set the
- parent::_SetMargins($Cfg);
- // $this->clsCpdf->SetMargin($this->Margins['top'],$this->Margins['bottom'],$this->Margins['left'],$this->Margins['right']);
- }
-
-
- /**
- * Check whether the margins are placed inside or outside the page
- * No check is made by this module. I haven't information about paper size.
- * The Aprint module will made the checks.
- */
- function CheckMargins()
- {
- }
-
- /**
- * Function executd at the end of the xml parsing. I run the final configuration
- * checks and complete the configration data
- * @access public
- */
- function EndParseXml()
- {
- $this->PageSize=$this->tblFormatIso[$this->PageFormat];
- $this->clsCpdf=new Cpdf(array(0,0,$this->PageSize['width'],$this->PageSize['length']));
- parent::EndParseXml();
- }
-
- /**
- * Create an istance of the CFont class based on configuration
- * file.
- * @param object CXml2Array Node with font configuration
- * @access public
- */
- function NewFont($node)
- {
- $tmpFont=new CPdfFont($node);
- if(array_key_exists($tmpFont->GetId(),$this->Fonts))
- trigger_error('Font '.$tmpFont->GetId().' already exist',E_USER_ERROR);
- $this->Fonts[$tmpFont->GetId()]=$tmpFont;
- }
-
- /**
- * Start to printout the report. OPen the output file,
- * initialize some variable.
- * @parameter string $OutFile output printer name
- * @access public
- */
- function BeginReport($OutFile)
- {
- $this->OutFile=$OutFile;
- // $this->CurrentLine=MAXPAGEROWS+1;
- $this->CurrentPage=0;
- $this->_OpenPage();
- $this->_StartReport();
- }
-
- /**
- * Perform the requested job to end the report.
- * Print the end report text, close the current page and the output file
- * @access public
- */
- function CloseReport()
- {
- $this->_EndReport();
- $this->_ComposeRow($this->ColumnSet);
- $this->CurrentLine=$this->PageSize['length']-$this->Margins['bottom'];
- $this->_ClosePage();
- $out=$this->clsCpdf->output();
- $fp=fopen($this->OutFile,'wb');
- fwrite($fp,$out);
- fclose($fp);
- }
-
- /**
- * Begin a new page. Close the previous, if any,
- * and printout the 'openpage' text if specified
- *
- * @access public
- */
- function NewPage($FromComposeRow=FALSE)
- {
- // If this function is not called from composerow, but as page skip
- // in the middle of the page, before change the page, I close the
- // current text
- if(!$FromComposeRow)
- $this->_ComposeRow($this->ColumnSet);
-
- if($this->CurrentPage)
- {
- $this->CurrentLine=$this->PageSize['length']-$this->Margins['bottom'];
- $this->_ClosePage();
- $this->clsCpdf->newPage();
- }
- // print "<br>".$this->CurrentPage;
- $this->_OpenPage();
- }
-
- /**
- * Printout the closepage text (if any)
- * @access private
- */
- function _ClosePage()
- {
- if(array_key_exists('closepage',$this->Report))
- {
- $txt=$this->Report['closepage']->WriteOut($this);
- //$this->clsAprint->TextFooter(trim($txt));
- }
- }
-
- /**
- * Now each column has its own text for the current row. This function
- * merge all texts from the columns and fill the PageData array.
- * Note a column may span over one or more row
- * @param array column set to print
- * @param bool the row to print is the footer? If so don't skip to the new page
- * @return bool return true if the function writes almost 1 row
- * @access private
- */
- function _ComposeRow($ColSet,$PageFooter=FALSE)
- {
- $nColumn=count($ColSet);
- $RowWritten=FALSE;
- /*
- if($PageFooter) {
- $GLOBALS['dbg']->pray($this);
- $GLOBALS['dbg']->Backtrace();
- die('prova');
- }
- */
- // $GLOBALS['dbg']->pray($ColSet);
- // Checl each row to know the max height
- $maxHeight=0;
- for($i=0;$i<$nColumn;$i++)
- {
- assert('is_object($ColSet[$i])');
- $maxHeight=($ColSet[$i]->GetHeight()>$maxHeight)?$ColSet[$i]->GetHeight():$maxHeight;
- }
-
- if($this->CurrentLine+$maxHeight>($this->PageSize['length']-$this->Margins['bottom'])&&!$PageFooter)
- $this->NewPage(TRUE);
- $conta=0;
- // die('boh');
- $NewLinePos=0;
- for($i=0;$i<$nColumn;$i++)
- {
- // I've got one columns, check where it starts
- $posY=$this->CurrentLine;
- while($ColSet[$i]->HasAnotherText())
- {
- assert('$conta<100');
- $conta++;
- $LeftPos=$ColSet[$i]->GetLeftTextPos();
- $Text=$ColSet[$i]->GetText();
- $FontToUse=$ColSet[$i]->GetFontId();
- if($this->Fonts[$FontToUse]->Bold)
- {
- $Text='<b>'.$Text.'</b>';
- }
- if($this->Fonts[$FontToUse]->Italic)
- $Text='<i>'.$Text.'</i>';
- if(!file_exists(PDFFONTDIR.$this->Fonts[$FontToUse]->Face))
- trigger_error('Font file:'.PDFFONTDIR.$this->Fonts[$FontToUse]->Face.' does not exists',E_USER_ERROR);
- $this->clsCpdf->selectFont(PDFFONTDIR.$this->Fonts[$FontToUse]->Face);
- assert('$LeftPos<$this->PageSize["width"]'); // X pos inside the page width
- assert('$posY<$this->PageSize["length"]'); // Y pos inside the page length
- $FontHeight=$this->clsCpdf->getFontHeight($this->Fonts[$FontToUse]->Size);
- $this->clsCpdf->addText($LeftPos,abs($posY-$this->PageSize["length"])-$FontHeight,$this->Fonts[$FontToUse]->Size,$Text);
- $posY+=$FontHeight;;
- }
- $NewLinePos=($NewLinePos>$posY)?$NewLinePos:$posY;
- }
- $this->CurrentLine=$NewLinePos;
- // Clear all used text
- for($i=0;$i<$nColumn;$i++)
- $ColSet[$i]->ClearText();
- return($RowWritten);
- }
-
- }
- ?>