Source for file pdfexport.php

Documentation is available at pdfexport.php

  1. <?php
  2. /**
  3. * Construct a PDF from the array representation
  4. *
  5. @author    Stuart Prescott
  6. @copyright  Copyright Stuart Prescott
  7. @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  8. @version    $Id$
  9. @package    Bumblebee
  10. @subpackage Export
  11. *
  12. *  path (bumblebee root)/inc/export/pdfexport.php
  13. */
  14.  
  15. /** Load ancillary functions */
  16. require_once 'inc/typeinfo.php';
  17.  
  18. require_once 'inc/bb/configreader.php';
  19.  
  20. /** constants for defining export formatting and codes */
  21. require_once 'inc/exportcodes.php';
  22.  
  23. /** config and class libraries for PDF export */
  24. require_once 'inc/export/pdfexportconfig.php';
  25.  
  26. define('PDF_FONT''Arial');
  27.  
  28. /**
  29. * Construct a PDF from the array representation
  30. *
  31. @package    Bumblebee
  32. @subpackage Export
  33. */
  34. class PDFExport {
  35.   var $ea;
  36.   var $pdf;
  37.   var $export;
  38.   var $filename = '/tmp/test.pdf';
  39.   var $writeToFile = false;
  40.  
  41.   var $orientation = 'L';
  42.   var $size = 'A4';
  43.   var $pageWidth   = 297;   // mm
  44.     var $pageHeight  = 210;   // mm
  45.     var $leftMargin  = 15;    // mm
  46.     var $rightMargin = 15;    // mm
  47.     var $topMargin   = 15;    // mm
  48.     var $bottomMargin15;    // mm
  49.  
  50.   
  51.   var $minAutoMargin = 4;   // mm added to auto calc'd column widths
  52.     var $tableHeaderAlignment = 'L';
  53.   var $rowLines = '';  // use 'T' for lines between rows
  54.     var $headerLines = 'TB';    // Top and Bottom lines on the header rows
  55.  
  56.   
  57.  
  58.   var $normalLineHeight = 5;
  59.   var $headerLineHeight = 6;
  60.   var $footerLineHeight = 4;
  61.   var $doubleLineWidth  = 0.2;
  62.   var $singleLineWidth  = 0.3;
  63.   var $sectionHeaderLineHeight = 8;
  64.   var $singleCellTopMargin    = 1;
  65.  
  66.   var $normalFillColor = array(224,235,255);
  67.   var $normalDrawColor = array(  0,  0,  0);
  68.   var $normalTextColor = array(  0,  0,  0);
  69.   var $normalFont      = array(PDF_FONT,'',12);
  70.  
  71.   var $sectionHeaderFillColor = array(255,255,255);
  72.   var $sectionHeaderDrawColor = array(  0,  0,  0);
  73.   var $sectionHeaderTextColor = array(  0,  0,  0);
  74.   var $sectionHeaderFont      = array(PDF_FONT,'B',14);
  75.  
  76.   var $tableHeaderFillColor = array(  0,  0,128);
  77.   var $tableHeaderDrawColor = array(  0,  0,  0);
  78.   var $tableHeaderTextColor = array(255,255,255);
  79.   var $tableHeaderFont      = array(PDF_FONT,'B',12);
  80.  
  81.   var $tableFooterFillColor = array(  0,  0,128);
  82.   var $tableFooterDrawColor = array(  0,  0,  0);
  83.   var $tableFooterTextColor = array(255,255,255);
  84.   var $tableFooterFont      = array(PDF_FONT,'',9);
  85.  
  86.   var $tableTotalFillColor = array(224,235,255);
  87.   var $tableTotalDrawColor = array(  0,  0,  0);
  88.   var $tableTotalTextColor = array(  0,  0,  0);
  89.   var $tableTotalFont      = array(PDF_FONT,'',12);
  90.  
  91.  
  92.  
  93.   var $cols = array()//=array(50,20,20,20,20,20,20,20);   //column widths
  94.     var $colStart = array();
  95.   var $tableRow = 0;
  96.  
  97.   var $useBigTable = true;
  98.  
  99.   var $DEBUG = 0;
  100.  
  101.   function PDFExport(&$exportArray{
  102.     $this->ea = $exportArray;
  103.     $this->_readConfig();
  104.   }
  105.  
  106.   function makePDFBuffer({
  107.     $this->render();
  108.     $this->export = $this->Output();
  109.   }
  110.  
  111.   function render({
  112.     $this->pdf = new TabularPDF($this->orientation'mm'$this->size);
  113.     $this->_setMetaInfo();
  114.     $this->_setPageMargins();
  115.     $this->_setTableAttributes();
  116.     $this->pdf->AliasNbPages();
  117.     #$this->pdf->AddPage();  //should we always include a page to start with?
  118.     $this->_parseArray();
  119.   }
  120.  
  121.   function Output({
  122.     if ($this->writeToFile{
  123.       return $this->pdf->Output($this->filename'F');
  124.     else {
  125.       return $this->pdf->Output('''S');
  126.     }
  127.   }
  128.  
  129.   function _setMetaInfo({
  130.     $metaData $this->ea->export['metadata'];
  131.     $this->pdf->SetCreator($metaData['creator']);
  132.     $this->pdf->SetAuthor($metaData['author']);
  133.     $this->pdf->SetKeywords($metaData['keywords']);
  134.     $this->pdf->SetSubject($metaData['subject']);
  135.     $this->pdf->SetTitle($metaData['title']);
  136.     $this->pdf->title $metaData['title'];
  137.   }
  138.  
  139.   function _readConfig({
  140.     $conf ConfigReader::getInstance();
  141.     $simplevars array(
  142.       'orientation''size''pageWidth''pageHeight',
  143.       'leftMargin''rightMargin''topMargin''bottomMargin',
  144.       'minAutoMargin''tableHeaderAlignment''rowLines''headerLines',
  145.       'normalLineHeight''headerLineHeight''footerLineHeight''sectionHeaderLineHeight',
  146.       'doubleLineWidth''singleLineWidth''singleCellTopMargin'
  147.       );
  148.     $cxvars array(
  149.       'normalFillColor''normalDrawColor''normalTextColor''normalFont',
  150.       'sectionHeaderFillColor''sectionHeaderDrawColor''sectionHeaderTextColor',  'sectionHeaderFont',
  151.       'tableHeaderFillColor''tableHeaderDrawColor''tableHeaderTextColor''tableHeaderFont',
  152.       'tableFooterFillColor''tableFooterDrawColor''tableFooterTextColor''tableFooterFont',
  153.       'tableTotalFillColor''tableTotalDrawColor''tableTotalTextColor''tableTotalFont'
  154.       );
  155.     foreach ($simplevars as $v{
  156.       $this->$v $conf->value('pdfexport'$v);
  157.     }
  158.     foreach ($cxvars as $v{
  159.       $val $conf->value('pdfexport'$v);
  160.       $this->$v $this->_confSplit($val);
  161.     }
  162.  
  163.     $fonts $conf->value('pdfexport''fontSubstitutions');
  164.     $fontList preg_split('@[,;\n\r]@'$fonts);
  165.     $fontTable array();
  166.     foreach ($fontList as $font{
  167.       $line explode('='$font);
  168.       if (count($line== 2{
  169.         $fontTable["@$line[0]@i"$line[1];
  170.       }
  171.     }
  172.     $this->fontSubstitutionTable = $fontTable;
  173.   }
  174.  
  175.   function _confSplit($c{
  176.     return explode(','$c);
  177.   }
  178.  
  179.   function _setPageMargins({
  180.     $this->pdf->SetMargins($this->leftMargin$this->topMargin$this->rightMargin);
  181.     $this->pdf->SetAutoPageBreak(true$this->bottomMargin);
  182.     $vars array('pageWidth''pageHeight''leftMargin''rightMargin''topMargin''bottomMargin''minAutoMargin''tableHeaderAlignment''rowLines''headerLines');
  183.     foreach ($vars as $v{
  184.       $this->pdf->$v $this->$v;
  185.     }
  186.   }
  187.  
  188.   function _setTableAttributes({
  189.     $vars array(
  190.       'normalLineHeight''headerLineHeight''footerLineHeight''sectionHeaderLineHeight',
  191.       'doubleLineWidth''singleLineWidth''singleCellTopMargin',
  192.       'normalFillColor''normalDrawColor''normalTextColor''normalFont',
  193.       'sectionHeaderFillColor''sectionHeaderDrawColor''sectionHeaderTextColor',  'sectionHeaderFont',
  194.       'tableHeaderFillColor''tableHeaderDrawColor''tableHeaderTextColor''tableHeaderFont',
  195.       'tableFooterFillColor''tableFooterDrawColor''tableFooterTextColor''tableFooterFont',
  196.       'tableTotalFillColor''tableTotalDrawColor''tableTotalTextColor''tableTotalFont',
  197.       'fontSubstitutionTable'
  198.       );
  199.     foreach ($vars as $v{
  200.       $this->pdf->$v $this->$v;
  201.     }
  202.   }
  203.  
  204.   /**
  205.   * calculate column widths
  206.   *
  207.   * @todo //TODO: make sure PDF columns don't go over right side of page
  208.   */
  209.   function _calcColWidths($widths$entry{
  210.     if ($this->useBigTable && count($this->cols)) return;
  211.     $this->log('Calculating column widths');
  212.     //preDump($widths);
  213.     //preDump($entry);
  214.     $sum 0;
  215.     $this->cols = array();
  216.     for ($col 0$col<count($widths)$col++{
  217.       if ($widths[$col== '*'{
  218.         $this->cols[$col$this->_getColWidth($col$entry);
  219.       else {
  220.         $sum += $widths[$col];
  221.       }
  222.     }
  223.     $taken array_sum($this->cols);
  224.     //if ($taken > ($this->pageWidth-$this->leftMargin-$this->rightMargin)) {
  225.       ///FIXME: must not go over page right
  226.     for ($col 0$col<count($widths)$col++{
  227.       if (isset($this->cols[$col])) {
  228.         $this->cols[$col$widths[$col]
  229.                   / $sum*($this->pageWidth-$this->leftMargin-$this->rightMargin-$taken);
  230.       }
  231.     }
  232.     $this->pdf->cols $this->cols;
  233.   }
  234.  
  235.   /**
  236.   * Calculate the width of an actual column
  237.   *
  238.   * @todo //TODO: calculate width of header from actual header data (bold) rather than 1.1 * non-bold
  239.   */
  240.   function _getColWidth($col$entry{
  241.     //why are we doing lots of calls into the pdf here? is it bad encapsulation?
  242.     //We have to have a font chosen within FPDF to perform these length calculations
  243.     $this->pdf->_setTableFont();
  244.     $ea =$this->ea->export;
  245.     $i=0;
  246.     $width 0;
  247.     for ($key=$entry;
  248.         $key<count($ea)-&& ($this->useBigTable || $ea[$key]['type'!= EXPORT_REPORT_TABLE_END);
  249.         $key++{
  250.       //$this->log('key='.$key);
  251.       if (is_array($ea[$key]['data'])) // protect against headers
  252.         $newWidth $this->pdf->GetStringWidth($ea[$key]['data'][$col]['value']);
  253.         if ($ea[$key]['type'== EXPORT_REPORT_TABLE_HEADER)
  254.           $newWidth *= 1.1;     //FIXME: we should do this calculation properly!
  255.         //echo "VAL=".$ea[$key]['data'][$col]['value'].", WIDTH=$newWidth/$width.<br/>";
  256.         $width max($width$newWidth);
  257.       }
  258.     }
  259.     //echo "WIDTH=$width.<br/>";
  260.     return $width $this->minAutoMargin;
  261.   }
  262.  
  263.   function _getColWidthRand($col{
  264.     //FIXME: is this random thing good enough? what about fitting in the header?
  265.     //why are we doing lots of calls into the pdf here? is it bad encapsulation?
  266.     //We have to have a font chosen within FPDF to perform these length calculations
  267.     $this->pdf->_setTableFont();
  268.     $ea =$this->ea->export;
  269.     $i=0;
  270.     $width 0$header 0;
  271.     while ($rows<10 || $header<1{
  272.       $key array_rand($ea1);
  273.       //echo $key;
  274.       if (is_numeric($key&& $ea[$key]['type'== EXPORT_REPORT_TABLE_ROW{
  275.         $rows++;
  276.         $newWidth $this->pdf->GetStringWidth($ea[$key]['data'][$col]['value']);
  277.         //echo "VAL=".$ea[$key]['data'][$col]['value'].", WIDTH=$newWidth/$width.<br/>";
  278.         $width max($width$newWidth);
  279.      elseif (is_numeric($key&& $ea[$key]['type'== EXPORT_REPORT_TABLE_HEADER{
  280.         $header++;
  281.         $newWidth 1.1*$this->pdf->GetStringWidth($ea[$key]['data'][$col]['value']);
  282.         //echo "VAL=".$ea[$key]['data'][$col]['value'].", WIDTH=$newWidth/$width.<br/>";
  283.         $width max($width$newWidth);
  284.      }
  285.     }
  286.     //echo "WIDTH=$width.<br/>";
  287.     return $width $this->minAutoMargin;
  288.   }
  289.  
  290.   function _parseArray({
  291.     //$this->log('Making HTML representation of data');
  292.     $ea =$this->ea->export;
  293.     $metaData $ea['metadata'];
  294.     unset($ea['metadata']);
  295.     $ea_length count($ea);
  296.     $this->log('Found '.$ea_length.' rows in this ExportArray');
  297.     for ($i=0$i<$ea_length$i++{
  298.       #echo $i.': '.$ea[$i]['type'].'<br/>';
  299.       #preDump($ea[$i]);
  300.       switch ($ea[$i]['type']{
  301.         case EXPORT_REPORT_START:
  302.           $this->pdf->reportStart();
  303.           break;
  304.         case EXPORT_REPORT_END:
  305.           $this->pdf->reportEnd();
  306.           //$i = count($ea);
  307.           break;
  308.         case EXPORT_REPORT_HEADER:
  309.           $this->pdf->reportHeader($ea[$i]['data']);
  310.           break;
  311.         case EXPORT_REPORT_SECTION_HEADER:
  312.           $this->_calcColWidths($ea[$i]['metadata']['colwidths']$i);
  313.           $this->pdf->sectionHeader($ea[$i]['data']);
  314.           break;
  315.         case EXPORT_REPORT_TABLE_START:
  316.           $this->tableRow=0;
  317.           $this->pdf->tableStart();
  318.           break;
  319.         case EXPORT_REPORT_TABLE_END:
  320.           $this->pdf->tableEnd();
  321.           break;
  322.         case EXPORT_REPORT_TABLE_HEADER:
  323.           $this->pdf->tableHeader($this->_formatRow($ea[$i]['data']true));
  324.           break;
  325.         case EXPORT_REPORT_TABLE_TOTAL:
  326.           $this->pdf->tableTotal($this->_formatRow($ea[$i]['data']false'TT'));
  327.           break;
  328.         case EXPORT_REPORT_TABLE_FOOTER:
  329.           $this->pdf->tableFooter($this->_formatRow($ea[$i]['data']));
  330.           break;
  331.         case EXPORT_REPORT_TABLE_ROW:
  332.           $this->pdf->tableRow($this->_formatRow($ea[$i]['data']));
  333.           $this->tableRow++;
  334.           break;
  335.       }
  336.       # unset($ea[$i]);
  337.       /// FIXME: free memory as we are using it up
  338.     }
  339.   }
  340.  
  341.   function _formatRow($row$isHeader=false$border=NULL{
  342.     $rowpdf array();
  343.     for ($j=0$j<count($row)$j++{
  344.       $rowpdf[$this->_formatCell($row[$j]$j$isHeader$border);
  345.     }
  346.     return $rowpdf;
  347.   }
  348.  
  349.   function _formatCell($d$col$isHeader$setborder{
  350.     $val $d['value'];
  351.     if ($isHeader{
  352.       switch($d['format'EXPORT_HTML_ALIGN_MASK{
  353.         case EXPORT_HTML_LEFT:
  354.           $align='L';
  355.           break;
  356.         case EXPORT_HTML_RIGHT:
  357.           $align='R';
  358.           break;
  359.         case EXPORT_HTML_CENTRE:
  360.         default:
  361.           $align='C';
  362.       }
  363.       $fill $this->tableRow % 2;
  364.       $border $this->rowLines;
  365.     else {
  366.       $align $this->tableHeaderAlignment;
  367.       $fill 1;
  368.       $border $this->headerLines;
  369.     }
  370.     if (isset($setborder)) {
  371.       $border $setborder;
  372.     }
  373.     return array('align'=>$align'value'=>$val'fill'=>$fill'border'=>$border);
  374.   }
  375.  
  376.   function log ($string$prio=10{
  377.     if ($prio <= $this->DEBUG{
  378.       echo $string."<br />\n";
  379.     }
  380.   }
  381.  
  382. }  //  class PDFExport
  383.  
  384.  
  385. /**
  386. * PDF class that extends FPDF by putting the logo in the top corner
  387. *
  388. @package    Bumblebee
  389. @subpackage Export
  390. */
  391. class BrandedPDF extends TCPDF {
  392.   var $title;
  393.   var $pageWidth   = 297;   // mm
  394.     var $pageHeight  = 210;   // mm
  395.     var $leftMargin  = 15;    // mm
  396.     var $rightMargin = 15;    // mm
  397.     var $topMargin   = 15;    // mm
  398.     var $bottomMargin15;    // mm
  399.  
  400.   
  401.  
  402.   var $DEBUG = 0;
  403.  
  404.   function BrandedPDF($orientation$measure$format{
  405.     parent::TCPDF($orientation$measure$format);
  406.     $this->title = T_('Bumblebee Report');
  407.   }
  408.  
  409.   function Header({
  410.     //Logo
  411.     $this->Image('theme/export/logo.png',10,8,33);
  412.     //Arial bold 15
  413.     $this->SetFont(PDF_FONT,'B',15);
  414.     //Move to the right
  415.     $this->Cell(40);
  416.     //Title
  417.     $this->Cell(200,30,$this->title,0,0,'C');
  418.     //Line break
  419.     $this->Ln(30);
  420.   }
  421.  
  422.   //Page footer
  423.     function Footer({
  424.     //Position at 1.5 cm from bottom
  425.     $this->SetY(-15);
  426.     //Arial italic 8
  427.     $this->SetFont(PDF_FONT,'I',8);
  428.     //Page number
  429.     $this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
  430.   }
  431.  
  432.   function SetFillColor($r=0$g=0$b=0{
  433.     if (is_array($r)) {
  434.       return parent::SetFillColor($r[0]$r[1]$r[2]);
  435.     else {
  436.       return parent::SetFillColor($r$g$b);
  437.     }
  438.   }
  439.  
  440.   function SetDrawColor($r=0$g=0$b=0{
  441.     if (is_array($r)) {
  442.       return parent::SetDrawColor($r[0]$r[1]$r[2]);
  443.     else {
  444.       return parent::SetDrawColor($r$g$b);
  445.     }
  446.   }
  447.  
  448.   function SetTextColor($r=0$g=0$b=0{
  449.     if (is_array($r)) {
  450.       return parent::SetTextColor($r[0]$r[1]$r[2]);
  451.     else {
  452.       return parent::SetTextColor($r$g$b);
  453.     }
  454.   }
  455.  
  456.   function SetFont($font=0$style=0$size=0{
  457.     if (is_array($font)) {
  458.       return parent::SetFont($this->_substituteFont($font[0])$font[1]$font[2]);
  459.     else {
  460.       return parent::SetFont($this->_substituteFont($font)$style$size);
  461.     }
  462.   }
  463.  
  464.   function _substituteFont($fontName{
  465.                         array_values($this->fontSubstitutionTable),
  466.                         $fontName);
  467.   }
  468.  
  469.   function log ($string$prio=10{
  470.     if ($prio <= $this->DEBUG{
  471.       echo $string."<br />\n";
  472.     }
  473.   }
  474.  
  475.   // overload the error function to give us some better output
  476.     function Error($msg{
  477.     echo '<div class="error"><b>'.T_('Error generating PDF output:').'</b><br/> '.$msg
  478.         .'<br/><br/>'.T_('Sorry things didn\'t work out for you.').'</div>';
  479.     if ($this->DEBUG{
  480.       preDump(debug_backtrace());
  481.     }
  482.     die(T_('Exiting with error'));
  483.   }
  484.  
  485. // class BrandedPDF
  486.  
  487.  
  488. /**
  489. * PDF class that extends BrandedPDF by providing functions for representing row data.
  490. *
  491. * Table-managing code adapted from Olivier's FPDF examples:
  492. *      http://www.fpdf.org/en/script/script3.php
  493. *
  494. @package    Bumblebee
  495. @subpackage Export
  496. */
  497. class TabularPDF extends BrandedPDF {
  498.  
  499.  
  500.  
  501.   var $lineHeight;
  502.   var $normalLineHeight = 5;
  503.   var $headerLineHeight = 6;
  504.   var $footerLineHeight = 4;
  505.   var $doubleLineWidth  = 0.2;
  506.   var $singleLineWidth  = 0.3;
  507.   var $sectionHeaderLineHeight = 8;
  508.   var $cellTopMargin;
  509.   var $singleCellTopMargin    = 1;
  510.  
  511.   var $normalFillColor = array(224,235,255);
  512.   var $normalDrawColor = array(  0,  0,  0);
  513.   var $normalTextColor = array(  0,  0,  0);
  514.   var $normalFont      = array(PDF_FONT,'',12);
  515.  
  516.   var $sectionHeaderFillColor = array(255,255,255);
  517.   var $sectionHeaderDrawColor = array(  0,  0,  0);
  518.   var $sectionHeaderTextColor = array(  0,  0,  0);
  519.   var $sectionHeaderFont      = array(PDF_FONT,'B',14);
  520.  
  521.   var $tableHeaderFillColor = array(  0,  0,128);
  522.   var $tableHeaderDrawColor = array(  0,  0,  0);
  523.   var $tableHeaderTextColor = array(255,255,255);
  524.   var $tableHeaderFont      = array(PDF_FONT,'B',12);
  525.  
  526.   var $tableFooterFillColor = array(  0,  0,128);
  527.   var $tableFooterDrawColor = array(  0,  0,  0);
  528.   var $tableFooterTextColor = array(255,255,255);
  529.   var $tableFooterFont      = array(PDF_FONT,'',9);
  530.  
  531.   var $tableTotalFillColor = array(224,235,255);
  532.   var $tableTotalDrawColor = array(  0,  0,  0);
  533.   var $tableTotalTextColor = array(  0,  0,  0);
  534.   var $tableTotalFont      = array(PDF_FONT,'',12);
  535.  
  536.   function TabularPDF($orientation$measure$format{
  537.     parent::BrandedPDF($orientation$measure$format);
  538.     $this->continuedHeader = ' '.T_('(continued)');
  539.   }
  540.  
  541.  
  542.   function _setTableFont({
  543.     $this->SetFillColor($this->normalFillColor);
  544.     $this->SetDrawColor($this->normalDrawColor);
  545.     $this->SetLineWidth($this->singleLineWidth);
  546.     $this->SetTextColor($this->normalTextColor);
  547.     $this->SetFont($this->normalFont);
  548.     $this->lineHeight = $this->normalLineHeight;
  549.     $this->cellTopMargin = $this->singleCellTopMargin;
  550.   }
  551.  
  552.   function _setSectionHeaderFont({
  553.     $this->SetFillColor($this->sectionHeaderFillColor);
  554.     $this->SetDrawColor($this->sectionHeaderDrawColor);
  555.     $this->SetLineWidth($this->singleLineWidth);
  556.     $this->SetTextColor($this->sectionHeaderTextColor);
  557.     $this->SetFont($this->sectionHeaderFont);
  558.     $this->lineHeight = $this->normalLineHeight;
  559.     $this->cellTopMargin = $this->singleCellTopMargin;
  560.   }
  561.  
  562.   function _setTableHeaderFont({
  563.     $this->SetFillColor($this->tableHeaderFillColor);
  564.     $this->SetDrawColor($this->tableHeaderDrawColor);
  565.     $this->SetLineWidth($this->singleLineWidth);
  566.     $this->SetTextColor($this->tableHeaderTextColor);
  567.     $this->SetFont($this->tableHeaderFont);
  568.     $this->lineHeight = $this->headerLineHeight;
  569.     $this->cellTopMargin = $this->singleCellTopMargin;
  570.   }
  571.  
  572.   function _setTableFooterFont({
  573.     $this->SetFillColor($this->tableFooterFillColor);
  574.     $this->SetDrawColor($this->tableFooterDrawColor);
  575.     $this->SetLineWidth($this->singleLineWidth);
  576.     $this->SetTextColor($this->tableFooterTextColor);
  577.     $this->SetFont($this->tableFooterFont);
  578.     $this->lineHeight = $this->footerLineHeight;
  579.     $this->cellTopMargin = $this->singleCellTopMargin;
  580.   }
  581.  
  582.   function _setTableTotalFont({
  583.     $this->SetFillColor($this->tableTotalFillColor);
  584.     $this->SetDrawColor($this->tableTotalDrawColor);
  585.     $this->SetLineWidth($this->doubleLineWidth);
  586.     $this->SetTextColor($this->tableTotalTextColor);
  587.     $this->SetFont($this->tableTotalFont);
  588.     $this->lineHeight = $this->normalLineHeight//+ 4*$this->doubleLineWidth;
  589.     $this->cellTopMargin = $this->singleCellTopMargin + 4*$this->doubleLineWidth;
  590.   }
  591.  
  592.   function sectionHeader($header$skipAddPage=false{
  593.     if (!$skipAddPage{
  594.       $this->AddPage();                  // FIXME: doesn't add a page if no section header??
  595.       $this->_last_sectionHeader = $header;
  596.     }
  597.     //Colors, line width and bold font
  598.     $this->_setSectionHeaderFont();
  599.     $this->_row(array(array('value'=>$header,'border'=>'','align'=>'L''fill'=>true'fullWidth'=>1)));
  600.     $this->_setTableFont();
  601.     $this->lineHeight = $this->sectionHeaderLineHeight;
  602. }
  603.  
  604.   function repeatSectionHeader({
  605.     $this->sectionHeader($this->_last_sectionHeader.$this->continuedHeadertrue);
  606.   }
  607.  
  608.   function repeatTableHeader({
  609.     $this->tableHeader($this->_last_tableHeader);
  610.   }
  611.  
  612.   function reportStart({
  613.   }
  614.  
  615.   function reportHeader({
  616.   }
  617.  
  618.   function reportEnd({
  619.   }
  620.  
  621.   function tableStart({
  622.   }
  623.  
  624.   function tableHeader($data{
  625.     $this->_setTableHeaderFont();
  626.     //$data[0]['fullWidth'] = 1;
  627.     $this->_row($data);
  628.     $this->_last_tableHeader = $data;
  629.     $this->_setTableFont();
  630.   }
  631.  
  632.   function tableFooter($data{
  633.     $this->_setTableFooterFont();
  634.     $this->_row($data);
  635.     $this->_setTableFont();
  636.   }
  637.  
  638.   function tableTotal($data{
  639.     $this->_setTableTotalFont();
  640.     $this->_row($data);
  641.     $this->_setTableFont();
  642.   }
  643.  
  644.   function tableEnd({
  645.     $this->_preventNewPage = true;
  646.     $currHeight $this->lineHeight;
  647.     $this->lineHeight = 0.1;
  648.     $this->_row(array(array('value'=>'','border'=>'T','fill'=>false,'fullWidth'=>0)));
  649.     $this->lineHeight = $currHeight;
  650.     $this->_preventNewPage = false;
  651.   }
  652.  
  653.   function tableRow($data{
  654.     $this->_row($data);
  655.   }
  656.  
  657.   function _row($data{
  658.     $widths array();
  659.     if (count($data== && isset($data[0]['fullWidth']&& $data[0]['fullWidth'== 1{
  660.       $widths[0$this->pageWidth - $this->leftMargin - $this->rightMargin;
  661.     elseif (count($data== 1{
  662.       $widths[0array_sum($this->cols);
  663.     else {
  664.       $widths $this->cols;
  665.     }
  666.     //Calculate the height of the row
  667.     $nb=0;
  668.     for($i=0$i<count($data)$i++)
  669.         $nb=max($nb$this->NbLines($widths[$i]$data[$i]['value']));
  670.     $rowHeight $this->lineHeight*$nb $this->cellTopMargin;
  671.     //Issue a page break first if needed
  672.     $this->CheckPageBreak($rowHeight);
  673.     $y0bg  $this->GetY();
  674.     $y0txt $y0bg $this->cellTopMargin;
  675.     //Draw the cells of the row
  676.     $this->SetY($y0txt);
  677.     for($i=0$i<count($data)$i++{
  678.       $align=isset($data[$i]['align']$data[$i]['align''L';
  679.       //Save the current position
  680.       $x $this->GetX();
  681.       //$y = $this->GetY();
  682.       //Draw the background of the cell if appropriate
  683.       if ($data[$i]['fill'])
  684.         $this->Rect($x$y0bg$widths[$i]$rowHeight+$this->cellTopMargin'F');
  685.       //Draw the borders requested
  686.       if ($data[$i]['border']{
  687.         if (strpos($data[$i]['border']'B'!== false{
  688.           //echo 'B';
  689.           $this->line($x,             $y0txt+$rowHeight$x+$widths[$i]$y0txt+$rowHeight);
  690.         }
  691.         if (strpos($data[$i]['border']'T'!== false{
  692.           //echo 'T';
  693.           $this->line($x,             $y0bg,            $x+$widths[$i]$y0bg);
  694.         }
  695.         if (strpos($data[$i]['border']'TT'!== false{
  696.           //double line on the top of the cell
  697.           $dy=$this->doubleLineWidth*3//mm
  698.           $this->line($x,             $y0bg+$dy,        $x+$widths[$i]$y0bg+$dy);
  699.         }
  700.         if (strpos($data[$i]['border']'L'!== false{
  701.           //echo 'L';
  702.           $this->line($x,             $y0bg,            $x,             $y0txt+$rowHeight);
  703.         }
  704.         if (strpos($data[$i]['border']'R'!== false{
  705.           //echo 'R';
  706.           $this->line($x+$widths[$i]$y0bg,            $x+$widths[$i]$y0txt+$rowHeight);
  707.         }
  708.       }
  709.       //Print the text
  710.       $this->MultiCell($widths[$i]$this->lineHeight$data[$i]['value']''$align0);
  711.       //Put the position to the right of the cell
  712.       $this->SetXY($x+$widths[$i],$y0txt);
  713.     }
  714.     //Go to the next line
  715.     $this->Ln($rowHeight);
  716.   }
  717.  
  718.   function CheckPageBreak($h{
  719.     //If the height h would cause an overflow, add a new page immediately
  720.     if($this->_preventNewPage && $this->GetY()+$h>$this->PageBreakTrigger{
  721.       $this->tableEnd();
  722.       $this->AddPage($this->CurOrientation);
  723.       $this->repeatSectionHeader();
  724.       $this->repeatTableHeader();
  725.     }
  726.   }
  727.  
  728.   function AcceptPageBreak({
  729.     return $this->_preventNewPage;
  730.   }
  731.  
  732.   function NbLines($w,$txt{
  733.     //Computes the number of lines a MultiCell of width w will take
  734.     $cw=&$this->CurrentFont['cw'];
  735.     if($w==0)
  736.         $w=$this->w-$this->rMargin-$this->x;
  737.     $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  738.     $s=str_replace("\r",'',$txt);
  739.     $nb=strlen($s);
  740.     if($nb>and $s[$nb-1]=="\n")
  741.         $nb--;
  742.     $sep=-1;
  743.     $i=0;
  744.     $j=0;
  745.     $l=0;
  746.     $nl=1;
  747.     while($i<$nb)  {
  748.         $c=$s[$i];
  749.         if($c=="\n"{
  750.             $i++;
  751.             $sep=-1;
  752.             $j=$i;
  753.             $l=0;
  754.             $nl++;
  755.             continue;
  756.         }
  757.         if($c==' ')
  758.             $sep=$i;
  759.         if(isset($cw[ord($c)])) {
  760.           $l+=$cw[ord($c)];
  761.         else {
  762.           /// FIXME: Making up a width... probably should warn the user
  763.           $l+=$cw[ord('a')];
  764.         }
  765.         if($l>$wmax{
  766.             if($sep==-1)
  767.             {
  768.                 if($i==$j)
  769.                     $i++;
  770.             }
  771.             else
  772.                 $i=$sep+1;
  773.             $sep=-1;
  774.             $j=$i;
  775.             $l=0;
  776.             $nl++;
  777.         }
  778.         else
  779.             $i++;
  780.     }
  781.     return $nl;
  782.   }
  783.  
  784.  
  785. // class TabularPDF
  786.  
  787. ?>

Documentation generated on Tue, 06 Mar 2007 10:01:52 +0000 by phpDocumentor 1.3.0