phpDocumentor SpoolTemplate
[ Back ] [ class tree: SpoolTemplate ] [ index: SpoolTemplate ] [ all elements ]

Source for file sptpl_clsPagePdf.php

Documentation is available at sptpl_clsPagePdf.php

  1. <?php
  2. /**
  3. * Page manager extension with support to Pdf output
  4. * Require:
  5. * R&OS pdf class (http://www.ros.co.nz/pdf)
  6. * sptpl_clsPageManager.php
  7. *
  8. * @copyright sptpl_clsPagePdf.php is part of Sptpl project {@link http://www.andrioli.com/en/sptpl.html} and it is LGPL
  9. * @author Andrioli Darvin <darvin (inside) andrioli (dot) com>
  10. * @version $Header: d:\cvs/classistd/sptpl/sptpl_clsPagePdf.php,v 2.5 2005/03/17 12:46:48 Darvin Exp $
  11. */
  12. /*
  13. * +-------------------------------------------------------------------------+
  14. * | Sptpl |
  15. * +-------------------------------------------------------------------------+
  16. * | Copyright (c) 2003-2005 Andrioli Darvin |
  17. * | Email <darvin (inside) andrioli (dot) com> |
  18. * | Web http://www.andrioli.com/en/sptpl.html |
  19. * | Download http://www.phpclasses.org/browse.html/package/1326.html |
  20. * | |
  21. * +-------------------------------------------------------------------------+
  22. * | This library is free software; you can redistribute it and/or modify |
  23. * | it under the terms of the GNU Lesser General Public License as |
  24. * | published by the Free Software Foundation; either version 2 of the |
  25. * | License, or (at your option) any later version. |
  26. * | |
  27. * | This library is distributed in the hope that it will be useful, but |
  28. * | WITHOUT ANY WARRANTY; without even the implied warranty of |
  29. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
  30. * | Lesser General Public License for more details. |
  31. * | |
  32. * | You should have received a copy of the GNU Lesser General Public |
  33. * | License along with this library; if not, write to the Free Software |
  34. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  35. * +-------------------------------------------------------------------------+
  36. */
  37.  
  38. class CPagePdf extends CPageMgr {
  39.  
  40. /**
  41. * Class Pdf instance
  42. * @var object Cpdf
  43. */
  44. var $clsCpdf;
  45.  
  46. var $OutFile;
  47. /**
  48. * The array holds the handlers returnt by Aprint's CreateFont
  49. * @var array
  50. * @see _DownloadFont()
  51. */
  52. var $FontsId;
  53. /**
  54. * Class constructor. Call the parent class (CPageMgr) constructor
  55. * @param object CDataStorage &$DataMgr Reference to the DataStorage istance
  56. * @access public
  57. */
  58. function CPagePdf(&$DataMgr)
  59. {
  60. parent::CPageMgr($DataMgr);
  61. // set the default margins and page size.
  62. $this->tblFormatIso=array( 'A0' => array( 'length' => 3370.39, 'width' => 2383.94),
  63. 'A1' => array( 'length' => 2383.94, 'width' => 1683.78),
  64. 'A2' => array( 'length' => 1683.78, 'width' => 1190.55),
  65. 'A3' => array( 'length' => 1190.55, 'width' => 841.89),
  66. 'A4' => array( 'length' => 841.89, 'width' => 595.28),
  67. 'A5' => array( 'length' => 595.28, 'width' => 419.53),
  68. );
  69.  
  70. $this->PageFormat='A4';
  71. $this->Margins=array('top' =>20,
  72. 'bottom' =>20,
  73. 'left' =>10,
  74. 'right' =>10);
  75. // $this->FontsId=array();
  76. }
  77.  
  78. /**
  79. * Create one default font named STANDARD
  80. * @access private
  81. */
  82. function _CreateDefaultFont()
  83. {
  84. $q="?"; // It makes happy my editor!!
  85. $XmlStr="<".$q."xml version='1.0' ".$q.">
  86. <font id='STANDARD' face='Helvetica.afm' size='12' />
  87. ";
  88. $tmpRoot=new CXml2Array();
  89. $tmpRoot->LoadFromString($XmlStr);
  90. $this->NewFont($tmpRoot);
  91. }
  92.  
  93. /**
  94. * Load margins setting from the configuration file
  95. * Default values are set by the class constructor.
  96. * Extend the base class
  97. * @param object CXml2Array
  98. * @access private
  99. *
  100. */
  101. function _SetMargins($Cfg)
  102. {
  103. // The parent class parse the configuration and load
  104. // the information, now using that information I set the
  105. parent::_SetMargins($Cfg);
  106. // $this->clsCpdf->SetMargin($this->Margins['top'],$this->Margins['bottom'],$this->Margins['left'],$this->Margins['right']);
  107. }
  108.  
  109.  
  110. /**
  111. * Check whether the margins are placed inside or outside the page
  112. * No check is made by this module. I haven't information about paper size.
  113. * The Aprint module will made the checks.
  114. */
  115. function CheckMargins()
  116. {
  117. }
  118.  
  119. /**
  120. * Function executd at the end of the xml parsing. I run the final configuration
  121. * checks and complete the configration data
  122. * @access public
  123. */
  124. function EndParseXml()
  125. {
  126. $this->PageSize=$this->tblFormatIso[$this->PageFormat];
  127. $this->clsCpdf=new Cpdf(array(0,0,$this->PageSize['width'],$this->PageSize['length']));
  128. parent::EndParseXml();
  129. }
  130.  
  131. /**
  132. * Create an istance of the CFont class based on configuration
  133. * file.
  134. * @param object CXml2Array Node with font configuration
  135. * @access public
  136. */
  137. function NewFont($node)
  138. {
  139. $tmpFont=new CPdfFont($node);
  140. if(array_key_exists($tmpFont->GetId(),$this->Fonts))
  141. trigger_error('Font '.$tmpFont->GetId().' already exist',E_USER_ERROR);
  142. $this->Fonts[$tmpFont->GetId()]=$tmpFont;
  143. }
  144.  
  145. /**
  146. * Start to printout the report. OPen the output file,
  147. * initialize some variable.
  148. * @parameter string $OutFile output printer name
  149. * @access public
  150. */
  151. function BeginReport($OutFile)
  152. {
  153. $this->OutFile=$OutFile;
  154. // $this->CurrentLine=MAXPAGEROWS+1;
  155. $this->CurrentPage=0;
  156. $this->_OpenPage();
  157. $this->_StartReport();
  158. }
  159.  
  160. /**
  161. * Perform the requested job to end the report.
  162. * Print the end report text, close the current page and the output file
  163. * @access public
  164. */
  165. function CloseReport()
  166. {
  167. $this->_EndReport();
  168. $this->_ComposeRow($this->ColumnSet);
  169. $this->CurrentLine=$this->PageSize['length']-$this->Margins['bottom'];
  170. $this->_ClosePage();
  171. $out=$this->clsCpdf->output();
  172. $fp=fopen($this->OutFile,'wb');
  173. fwrite($fp,$out);
  174. fclose($fp);
  175. }
  176.  
  177. /**
  178. * Begin a new page. Close the previous, if any,
  179. * and printout the 'openpage' text if specified
  180. *
  181. * @access public
  182. */
  183. function NewPage($FromComposeRow=FALSE)
  184. {
  185. // If this function is not called from composerow, but as page skip
  186. // in the middle of the page, before change the page, I close the
  187. // current text
  188. if(!$FromComposeRow)
  189. $this->_ComposeRow($this->ColumnSet);
  190.  
  191. if($this->CurrentPage)
  192. {
  193. $this->CurrentLine=$this->PageSize['length']-$this->Margins['bottom'];
  194. $this->_ClosePage();
  195. $this->clsCpdf->newPage();
  196. }
  197. // print "<br>".$this->CurrentPage;
  198. $this->_OpenPage();
  199. }
  200.  
  201. /**
  202. * Printout the closepage text (if any)
  203. * @access private
  204. */
  205. function _ClosePage()
  206. {
  207. if(array_key_exists('closepage',$this->Report))
  208. {
  209. $txt=$this->Report['closepage']->WriteOut($this);
  210. //$this->clsAprint->TextFooter(trim($txt));
  211. }
  212. }
  213.  
  214. /**
  215. * Now each column has its own text for the current row. This function
  216. * merge all texts from the columns and fill the PageData array.
  217. * Note a column may span over one or more row
  218. * @param array column set to print
  219. * @param bool the row to print is the footer? If so don't skip to the new page
  220. * @return bool return true if the function writes almost 1 row
  221. * @access private
  222. */
  223. function _ComposeRow($ColSet,$PageFooter=FALSE)
  224. {
  225. $nColumn=count($ColSet);
  226. $RowWritten=FALSE;
  227. /*
  228. if($PageFooter) {
  229. $GLOBALS['dbg']->pray($this);
  230. $GLOBALS['dbg']->Backtrace();
  231. die('prova');
  232. }
  233. */
  234. // $GLOBALS['dbg']->pray($ColSet);
  235. // Checl each row to know the max height
  236. $maxHeight=0;
  237. for($i=0;$i<$nColumn;$i++)
  238. {
  239. assert('is_object($ColSet[$i])');
  240. $maxHeight=($ColSet[$i]->GetHeight()>$maxHeight)?$ColSet[$i]->GetHeight():$maxHeight;
  241. }
  242. if($this->CurrentLine+$maxHeight>($this->PageSize['length']-$this->Margins['bottom'])&&!$PageFooter)
  243. $this->NewPage(TRUE);
  244. $conta=0;
  245. // die('boh');
  246. $NewLinePos=0;
  247. for($i=0;$i<$nColumn;$i++)
  248. {
  249. // I've got one columns, check where it starts
  250. $posY=$this->CurrentLine;
  251. while($ColSet[$i]->HasAnotherText())
  252. {
  253. assert('$conta<100');
  254. $conta++;
  255. $LeftPos=$ColSet[$i]->GetLeftTextPos();
  256. $Text=$ColSet[$i]->GetText();
  257. $FontToUse=$ColSet[$i]->GetFontId();
  258. if($this->Fonts[$FontToUse]->Bold)
  259. {
  260. $Text='<b>'.$Text.'</b>';
  261. }
  262. if($this->Fonts[$FontToUse]->Italic)
  263. $Text='<i>'.$Text.'</i>';
  264. if(!file_exists(PDFFONTDIR.$this->Fonts[$FontToUse]->Face))
  265. trigger_error('Font file:'.PDFFONTDIR.$this->Fonts[$FontToUse]->Face.' does not exists',E_USER_ERROR);
  266. $this->clsCpdf->selectFont(PDFFONTDIR.$this->Fonts[$FontToUse]->Face);
  267. assert('$LeftPos<$this->PageSize["width"]'); // X pos inside the page width
  268. assert('$posY<$this->PageSize["length"]'); // Y pos inside the page length
  269. $FontHeight=$this->clsCpdf->getFontHeight($this->Fonts[$FontToUse]->Size);
  270. $this->clsCpdf->addText($LeftPos,abs($posY-$this->PageSize["length"])-$FontHeight,$this->Fonts[$FontToUse]->Size,$Text);
  271. $posY+=$FontHeight;;
  272. }
  273. $NewLinePos=($NewLinePos>$posY)?$NewLinePos:$posY;
  274. }
  275. $this->CurrentLine=$NewLinePos;
  276. // Clear all used text
  277. for($i=0;$i<$nColumn;$i++)
  278. $ColSet[$i]->ClearText();
  279. return($RowWritten);
  280. }
  281.  
  282. }
  283. ?>

Documentation generated on Mon, 28 Mar 2005 15:13:19 +0200 by phpDocumentor 1.3.0RC3