Chilo

pdftable.inc

Aug 30th, 2012
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 21.35 KB | None | 0 0
  1. <?
  2. /**
  3.  * Title: Print a HTML Table to PDF file
  4.  * Class: PDFTable
  5.  * Author: vietcom (vncommando at yahoo dot com)
  6.  * Version: 1.3
  7.  *
  8.  * Update to 1.1 (2-Nov-2004):
  9.  *      - Support long table in many page. Repeatable for some rows on each page.
  10.  *        Limitation: The height of each row can't great than the page height-the height of repeat title
  11.  * Update to 1.2 (18 Nov 2004):
  12.  *      - Fix some errors
  13.  * Edited: 25 Feb 2005
  14.  *      - In function _tableWriteRow(): correct $this-> after each row
  15.  * Edited: 24 Apr 2005
  16.  *      - Replace form 'foreach($array as $value)' to form 'foreach($array as $index=>$value)'
  17.  * Update to 1.3: 19 Dec 2006
  18.  *      Thanks to Vincent Tomi (a czech), he improved my class from May-2005
  19.  *      - Support many tables (but not inside other table)
  20.  *      - Support font/size/style for each cell
  21.  *      - Border width can be changed
  22.  * Update to 1.4: 26 March 2007
  23.  *      - Fix error cellwidth is not exactly when use font/size/style
  24.  *      - Increase height of line 1 user unit
  25.  */
  26. require_once('fpdf.inc.php');
  27. require_once('color.inc.php');
  28. require_once('htmlparser.inc.php');
  29.  
  30. class PDFTable extends FPDF{
  31. var $left;          //Toa do le trai cua trang
  32. var $right;         //Toa do le phai cua trang
  33. var $top;           //Toa do le tren cua trang
  34. var $bottom;        //Toa do le duoi cua trang
  35. var $width;         //Width of writable zone of page
  36. var $height;        //Height of writable zone of page
  37. var $defaultFontFamily ;
  38. var $defaultFontStyle;
  39. var $defaultFontSize;
  40.  
  41. function PDFTable($orientation='P',$unit='mm',$format=array(400,455)){
  42.     parent::FPDF($orientation,$unit,$format);
  43.     $this->SetMargins(0,0,0,0);
  44.     $this->SetAuthor('Pham Minh Dung');
  45.     $this->_makePageSize();
  46. }
  47.  
  48. function SetMargins($left,$top,$right=-1){
  49.     parent::SetMargins($left, $top, $right);
  50.  
  51.     $this->_makePageSize();
  52. }
  53.  
  54. function SetLeftMargin($margin){
  55.     parent::SetLeftMargin($margin);
  56.     $this->_makePageSize();
  57. }
  58.  
  59. function SetRightMargin($margin){
  60.     parent::SetRightMargin($margin);
  61.     $this->_makePageSize();
  62. }
  63.  
  64. function Header(){
  65.     parent::Header();
  66.     $this->SetY(($this->h*-1));
  67.     //$this->image('images/logo.png',$this->lMargin,2,70,10);
  68.     $this->SetY(($this->h*-1));
  69.     $this->SetFont('Arial','B',10);
  70.     $this->SetTextColor(0);
  71.     //$this->Cell(0,10,'Sistem Informasi Perkiraan ',0,1,'R');
  72.     $this->SetY(($this->h*-1)+5);
  73.     //$this->Cell(0,10,'CV. Surabaya Intelectual Club ',0,1,'R');
  74.  
  75.     //$this->SetFillColor(0,0,0);
  76.     //$this->Cell($this->w-$this->rMargin-$this->lMargin,0.2,'',1,0,'L',1);
  77.  
  78.     //Return Font to normal
  79.     //$this->SetFont('Arial','',11);
  80.     //$this->SetX($this->lMargin);
  81.     //$this->SetY(($this->h*-1)+$this->tMargin);
  82.  
  83.     $this->_makePageSize();
  84. }
  85. function _makePageSize(){
  86.     if ($this->CurOrientation=='P'){
  87.         $this->left     = $this->lMargin;
  88.         $this->right    = $this->fw - $this->rMargin;
  89.         $this->top      = $this->tMargin;
  90.         $this->bottom   = $this->fh - $this->bMargin;
  91.         $this->width    = $this->right - $this->left;
  92.         $this->height   = $this->bottom - $this->tMargin;
  93.     }else{
  94.         $this->left     = $this->tMargin;
  95.         $this->right    = $this->fh - $this->bMargin;
  96.         $this->top      = $this->rMargin;
  97.         $this->bottom   = $this->fw - $this->rMargin;
  98.         $this->width    = $this->right - $this->left;
  99.         $this->height   = $this->bottom - $this->lMargin;
  100.     }
  101. }
  102.  
  103. function Footer()
  104. {
  105. //! @return void
  106. //! @desc The footer is printed in every page!
  107.     //Position at 1.0 cm from bottom
  108.     parent::Footer();
  109.     $this->SetY(-10);
  110.     //Copyright //especial para esta versão
  111.     $this->SetFont('Arial','B',9);
  112.     $this->SetTextColor(0);
  113.     //Arial italic 9
  114.     $this->SetFont('Arial','I',9);
  115.     //Page number
  116.     //$this->SetFillColor(0,0,0);
  117.     //$this->Cell($this->w-$this->rMargin-$this->lMargin,0.2,'',1,0,'L',1);
  118.     //$this->Cell(0,10,$this->PageNo(),0,0,'R');
  119.     //Return Font to normal
  120.     //$this->SetFont('Arial','',11);
  121. }
  122.  
  123. /**
  124.  * @return int
  125.  * Tra ve chieu cao cua 1 dong theo font hien hanh
  126.  */
  127. function getLineHeight($fontSize = 0){
  128.     if ($fontSize == 0) $fontSize = $this->FontSizePt;
  129.     return ($fontSize/$this->k)+1;
  130. }
  131.  
  132. /**
  133.  * @return int
  134.  * @desc Tinh so dong cua $txt khi hien thi trong cell co width la $w
  135.  */
  136. function countLine($w,$txt){
  137.     //Computes the number of lines a MultiCell of width w will take
  138.     $cw=&$this->CurrentFont['cw'];
  139.     if($w==0)
  140.         $w=$this->w-$this->rMargin-$this->x;
  141.     $wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
  142.     $s=str_replace("\r",'',$txt);
  143.     $nb=strlen($s);
  144.     if($nb>0 and $s[$nb-1]=="\n")
  145.         $nb--;
  146.     $sep=-1;
  147.     $i=$j=$l=0;
  148.     $nl=1;
  149.     while($i<$nb){
  150.         $c=$s[$i];
  151.         if($c=="\n"){
  152.             $i++;
  153.             $sep=-1;
  154.             $j=$i;
  155.             $l=0;
  156.             $nl++;
  157.             continue;
  158.         }
  159.         if($c==' ')
  160.             $sep=$i;
  161.         $l+=$cw[$c];
  162.         if($l>$wmax){
  163.             if($sep==-1){
  164.                 if($i==$j)
  165.                     $i++;
  166.             }
  167.             else
  168.                 $i=$sep+1;
  169.             $sep=-1;
  170.             $j=$i;
  171.             $l=0;
  172.             $nl++;
  173.         }
  174.         else
  175.             $i++;
  176.     }
  177.     return $nl;
  178. }
  179.  
  180. /**
  181.  * @return array
  182.  * @desc Parse a string in html and return array of attribute of table
  183.  */
  184. function _tableParser(&$html){
  185.     $align = array('left'=>'L','center'=>'C','right'=>'R','top'=>'T','middle'=>'M','bottom'=>'B');
  186.     $t = new TreeHTML(new HTMLParser($html), 0);
  187.     $row    = $col  = -1;
  188.     $table['nc'] = $table['nr'] = 0;
  189.     $table['repeat'] = array();
  190.     $cell   = array();
  191.     foreach ($t->name as $i=>$element){
  192.         if ($t->type[$i] != NODE_TYPE_ELEMENT && $t->type[$i] != NODE_TYPE_TEXT) continue;
  193.         switch ($element){
  194.             case 'table':
  195.                 $a  = &$t->attribute[$i];
  196.                 if (isset($a['width']))     $table['w'] = intval($a['width']);
  197.                 if (isset($a['height']))    $table['h'] = intval($a['height']);
  198.                 if (isset($a['align']))     $table['a'] = $align[strtolower($a['align'])];
  199.                 $table['border'] = (isset($a['border']))?   $a['border']: 0;
  200.                 if (isset($a['bgcolor']))   $table['bgcolor'][-1]   = $a['bgcolor'];
  201.                 break;
  202.             case 'tr':
  203.                 $row++;
  204.                 $table['nr']++;
  205.                 $col = -1;
  206.                 $a  = &$t->attribute[$i];
  207.                 if (isset($a['bgcolor']))   $table['bgcolor'][$row] = $a['bgcolor'];
  208.                 if (isset($a['repeat']))    $table['repeat'][]      = $row;
  209.                 break;
  210.             case 'td':
  211.                 $col++;while (isset($cell[$row][$col])) $col++;
  212.                 //Update number column
  213.                 if ($table['nc'] < $col+1)      $table['nc']        = $col+1;
  214.                 $cell[$row][$col] = array();
  215.                 $c = &$cell[$row][$col];
  216.                 $a  = &$t->attribute[$i];
  217.                 $c['text'] = array();
  218.                 $c['s'] = 2;
  219.                 if (isset($a['width']))     $c['w']     = intval($a['width']);
  220.                 if (isset($a['height']))    $c['h']     = intval($a['height']);
  221.                 if (isset($a['align']))     $c['a']     = $align[strtolower($a['align'])];
  222.                 if (isset($a['valign']))    $c['va']    = $align[strtolower($a['valign'])];
  223.                 if (isset($a['border']))    $c['border']    = $a['border'];
  224.                     else                    $c['border']    = $table['border'];
  225.                 if (isset($a['bgcolor']))   $c['bgcolor']   = $a['bgcolor'];
  226.                 $cs = $rs = 1;
  227.                 if (isset($a['colspan']) && $a['colspan']>1)    $cs = $c['colspan'] = intval($a['colspan']);
  228.                 if (isset($a['rowspan']) && $a['rowspan']>1)    $rs = $c['rowspan'] = intval($a['rowspan']);
  229.                 if (isset($a['size']))      $c['fontSize']      = $a['size'];
  230.                 if (isset($a['family']))    $c['fontFamily']    = $a['family'];
  231.                 if (isset($a['style']))     $c['fontStyle']     = $a['style'];
  232.                 if (isset($a['color']))     $c['color']         = $a['color'];
  233.                 //Chiem dung vi tri de danh cho cell span
  234.                 for ($k=$row;$k<$row+$rs;$k++) for($l=$col;$l<$col+$cs;$l++){
  235.                     if ($k-$row || $l-$col)
  236.                         $cell[$k][$l] = 0;
  237.                 }
  238.                 if (isset($a['nowrap']))    $c['nowrap']= 1;
  239.                 break;
  240.             case 'Text':
  241.                 $this->_setTextAndSize($c,$this->_html2text($t->value[$i]));
  242.                 break;
  243.             case 'img':
  244.                 $a  = &$t->attribute[$i];
  245.                 if (isset($a['src'])){
  246.                     $this->_setImage($c,$a);
  247.                 }
  248.                 break;
  249.             case 'br':
  250.                 $c = &$cell[$row][$col];
  251.                 $cn = isset($c['font'])?count($c['font'])-1:0;
  252.                 $c['font'][$cn]['line'][] = 'br';
  253.                 break;
  254.         }
  255.     }
  256.     $table['cells'] = $cell;
  257.     $table['wc']    = array_pad(array(),$table['nc'],array('miw'=>0,'maw'=>0));
  258.     $table['hr']    = array_pad(array(),$table['nr'],0);
  259.     return $table;
  260. }
  261.  
  262. function _setTextAndSize(&$c, $text){
  263.     $c['text'][] = $text;
  264.  
  265.     $this->_setFontText($c);
  266.     $width =  $this->GetStringWidth($text)+3;
  267.     if (!isset($c['s']) || $c['s'] < $width) $c['s'] = $width;
  268. }
  269.  
  270. private function _setImage(&$c, &$a){
  271.     $path = $a['src'];
  272.     if (!is_file($path)){
  273.         $this->Error('Image is not exists: '.$path);
  274.     }else{
  275.         list($u,$d) = $this->_getResolution($path);
  276.         $c['img'] = $path;
  277.         list($c['w'],$c['h'],) = getimagesize($path);
  278.         if (isset($a['width'])) $c['w'] = $a['width'];
  279.         if (isset($a['height'])) $c['h'] = $a['height'];
  280.         $scale = 1;
  281.         if ($u==1) $scale = 25.4/$d;
  282.         elseif ($u==2) $scale = 10/$d;
  283.         $c['w'] = intval($c['w']*$scale);
  284.         $c['h'] = intval($c['h']*$scale);
  285.     }
  286. }
  287. private function _getResolution($path)
  288. {
  289.     $pos=strrpos($path,'.');
  290.     if(!$pos)
  291.         $this->Error('Image file has no extension and no type was specified: '.$path);
  292.     $type=substr($path,$pos+1);
  293.     $type=strtolower($type);
  294.     if($type=='jpeg')
  295.         $type='jpg';
  296.     if ($type!='jpg')
  297.         $this->Error('Unsupported image type: '.$path);
  298.     $f = fopen($path,'r');
  299.     fseek($f,13,SEEK_SET);
  300.     $info = fread($f,3);
  301.     fclose($f);
  302.     $iUnit = ord($info{0});
  303.     $iX = ord($info{1}) * 256 + ord($info{2});
  304.     return array($iUnit, $iX);
  305. }
  306.  
  307. function _html2text($text){
  308.     $text = str_replace('&nbsp;',' ',$text);
  309.     $text = str_replace('&lt;','<',$text);
  310.     return $text;
  311. }
  312.  
  313. //table     Array of (w, h, bc, nr, wc, hr, cells)
  314. //w         Width of table
  315. //h         Height of table
  316. //bc        Number column
  317. //nr        Number row
  318. //hr        List of height of each row
  319. //wc        List of width of each column
  320. //cells     List of cells of each rows, cells[i][j] is a cell in table
  321. function _tableColumnWidth(&$table){
  322.     $cs = &$table['cells'];
  323.     $mw = $this->getStringWidth('W');
  324.     $nc = $table['nc'];
  325.     $nr = $table['nr'];
  326.     $listspan = array();
  327.     //Xac dinh do rong cua cac cell va cac cot tuong ung
  328.     for ($j=0;$j<$nc;$j++){
  329.         $wc = &$table['wc'][$j];
  330.         for ($i=0;$i<$nr;$i++){
  331.             if (isset($cs[$i][$j]) && $cs[$i][$j]){
  332.                 $c   = &$cs[$i][$j];
  333.                 $miw = $mw;
  334.                 $c['maw']   = $c['s'];
  335.                 if (isset($c['nowrap']))            $miw = $c['maw'];
  336.                 if (isset($c['w'])){
  337.                     if ($miw<$c['w'])   $c['miw'] = $c['w'];
  338.                     if ($miw>$c['w'])   $c['miw'] = $c['w']   = $miw;
  339.                     if (!isset($wc['w'])) $wc['w'] = 1;
  340.                 }else{
  341.                     $c['miw'] = $miw;
  342.                 }
  343.                 if ($c['maw']  < $c['miw'])         $c['maw']  = $c['miw'];
  344.                 if (!isset($c['colspan'])){
  345.                     if ($wc['miw'] < $c['miw'])     $wc['miw']  = $c['miw'];
  346.                     if ($wc['maw'] < $c['maw'])     $wc['maw']  = $c['maw'];
  347.                 }else $listspan[] = array($i,$j);
  348.             }
  349.         }
  350.     }
  351.     //Xac dinh su anh huong cua cac cell colspan len cac cot va nguoc lai
  352.     $wc = &$table['wc'];
  353.     foreach ($listspan as $_z=>$span){
  354.         list($i,$j) = $span;
  355.         $c = &$cs[$i][$j];
  356.         $lc = $j + $c['colspan'];
  357.         if ($lc > $nc) $lc = $nc;
  358.  
  359.         $wis = $wisa = 0;
  360.         $was = $wasa = 0;
  361.         $list = array();
  362.         for($k=$j;$k<$lc;$k++){
  363.             $wis += $wc[$k]['miw'];
  364.             $was += $wc[$k]['maw'];
  365.             if (!isset($c['w'])){
  366.                 $list[] = $k;
  367.                 $wisa += $wc[$k]['miw'];
  368.                 $wasa += $wc[$k]['maw'];
  369.             }
  370.         }
  371.         if ($c['miw'] > $wis){
  372.             if (!$wis){//Cac cot chua co kich thuoc => chia deu
  373.                 for($k=$j;$k<$lc;$k++) $wc[$k]['miw'] = $c['miw']/$c['colspan'];
  374.             }elseif (!count($list)){//Khong co cot nao co kich thuoc auto => chia deu phan du cho tat ca
  375.                 $wi = $c['miw'] - $wis;
  376.                 for($k=$j;$k<$lc;$k++)
  377.                     $wc[$k]['miw'] += ($wc[$k]['miw']/$wis)*$wi;
  378.             }else{//Co mot so cot co kich thuoc auto => chia deu phan du cho cac cot auto
  379.                 $wi = $c['miw'] - $wis;
  380.                 foreach ($list as $_z2=>$k)
  381.                     $wc[$k]['miw'] += ($wc[$k]['miw']/$wisa)*$wi;
  382.             }
  383.         }
  384.         if ($c['maw'] > $was){
  385.             if (!$wis){//Cac cot chua co kich thuoc => chia deu
  386.                 for($k=$j;$k<$lc;$k++) $wc[$k]['maw'] = $c['maw']/$c['colspan'];
  387.             }elseif (!count($list)){//Khong co cot nao co kich thuoc auto => chia deu phan du cho tat ca
  388.                 $wi = $c['maw'] - $was;
  389.                 for($k=$j;$k<$lc;$k++)
  390.                     $wc[$k]['maw'] += ($wc[$k]['maw']/$was)*$wi;
  391.             }else{//Co mot so cot co kich thuoc auto => chia deu phan du cho cac cot auto
  392.                 $wi = $c['maw'] - $was;
  393.                 foreach ($list as $_z2=>$k)
  394.                     $wc[$k]['maw'] += ($wc[$k]['maw']/$wasa)*$wi;
  395.             }
  396.         }
  397.     }
  398. }
  399.  
  400. /**
  401.  * @desc Xac dinh chieu rong cua table
  402.  */
  403. function _tableWidth(&$table){
  404.     $wc = &$table['wc'];
  405.     $nc = $table['nc'];
  406.     $a = 0;
  407.     for ($i=0;$i<$nc;$i++){
  408.         $a += isset($wc[$i]['w']) ? $wc[$i]['miw'] : $wc[$i]['maw'];
  409.     }
  410.     if ($a > $this->width) $table['w'] = $this->width;
  411.  
  412.     if (isset($table['w'])){
  413.         $wis = $wisa = 0;
  414.         $list = array();
  415.         for ($i=0;$i<$nc;$i++){
  416.             $wis += $wc[$i]['miw'];
  417.             if (!isset($wc[$i]['w'])){ $list[] = $i;$wisa += $wc[$i]['miw'];}
  418.         }
  419.         if ($table['w'] > $wis){
  420.             if (!count($list)){//Khong co cot nao co kich thuoc auto => chia deu phan du cho tat ca
  421.                 //$wi = $table['w'] - $wis;
  422.                 $wi = ($table['w'] - $wis)/$nc;
  423.                 for($k=0;$k<$nc;$k++)
  424.                     //$wc[$k]['miw'] += ($wc[$k]['miw']/$wis)*$wi;
  425.                     $wc[$k]['miw'] += $wi;
  426.             }else{//Co mot so cot co kich thuoc auto => chia deu phan du cho cac cot auto
  427.                 //$wi = $table['w'] - $wis;
  428.                 $wi = ($table['w'] - $wis)/count($list);
  429.                 foreach ($list as $_z2=>$k)
  430.                     //$wc[$k]['miw'] += ($wc[$k]['miw']/$wisa)*$wi;
  431.                     $wc[$k]['miw'] += $wi;
  432.             }
  433.         }
  434.         for ($i=0;$i<$nc;$i++){
  435.             $a = $wc[$i]['miw'];
  436.             unset($wc[$i]);
  437.             $wc[$i] = $a;
  438.         }
  439.     }else{
  440.         $table['w'] = $a;
  441.         for ($i=0;$i<$nc;$i++){
  442.             $a = isset($wc[$i]['w']) ? $wc[$i]['miw'] : $wc[$i]['maw'];
  443.             unset($wc[$i]);
  444.             $wc[$i] = $a;
  445.         }
  446.     }
  447.     $table['w'] = array_sum($wc);
  448. }
  449.  
  450. function _tableHeight(&$table){
  451.     $cs = &$table['cells'];
  452.     $nc = $table['nc'];
  453.     $nr = $table['nr'];
  454.     $listspan = array();
  455.     for ($i=0;$i<$nr;$i++){
  456.         $hr = &$table['hr'][$i];
  457.         for ($j=0;$j<$nc;$j++){
  458.             if (isset($cs[$i][$j]) && $cs[$i][$j]){
  459.                 $c = &$cs[$i][$j];
  460.                 list($x,$cw) = $this->_tableGetWCell($table, $i,$j);
  461.  
  462.                 $fontSize = (isset($c['fontSize']) && ($c['fontSize'] >0))? $c['fontSize']: 0;
  463.  
  464.                 $ch = $this->countLine($cw, implode("\n", $c['text'])) * $this->getLineHeight($fontSize);
  465.                 $c['ch'] = $ch;
  466.  
  467.                 if (isset($c['h']) && $c['h'] > $ch) $ch = $c['h'];
  468.  
  469.                 if (isset($c['rowspan'])) $listspan[] = array($i,$j);
  470.                 elseif ($hr < $ch)              $hr         = $ch;
  471.                 $c['mih'] = $ch;
  472.             }
  473.         }
  474.     }
  475.     $hr = &$table['hr'];
  476.     foreach ($listspan as $_z=>$span){
  477.         list($i,$j) = $span;
  478.         $c = &$cs[$i][$j];
  479.         $lr = $i + $c['rowspan'];
  480.         if ($lr > $nr) $lr = $nr;
  481.         $hs = $hsa = 0;
  482.         $list = array();
  483.         for($k=$i;$k<$lr;$k++){
  484.             $hs += $hr[$k];
  485.             if (!isset($c['h'])){
  486.                 $list[] = $k;
  487.                 $hsa += $hr[$k];
  488.             }
  489.         }
  490.         if ($c['mih'] > $hs){
  491.             if (!$hs){//Cac dong chua co kich thuoc => chia deu
  492.                 for($k=$i;$k<$lr;$k++) $hr[$k] = $c['mih']/$c['rowspan'];
  493.             }elseif (!count($list)){//Khong co dong nao co kich thuoc auto => chia deu phan du cho tat ca
  494.                 $hi = $c['mih'] - $hs;
  495.                 for($k=$i;$k<$lr;$k++)
  496.                     $hr[$k] += ($hr[$k]/$hs)*$hi;
  497.             }else{//Co mot so dong co kich thuoc auto => chia deu phan du cho cac dong auto
  498.                 $hi = $c['mih'] - $hsa;
  499.                 foreach ($list as $_z2=>$k)
  500.                     $hr[$k] += ($hr[$k]/$hsa)*$hi;
  501.             }
  502.         }
  503.     }
  504.     $table['repeatH'] = 0;
  505.     if (count($table['repeat'])){
  506.         foreach ($table['repeat'] as $_z=>$i) $table['repeatH'] += $hr[$i];
  507.     }else $table['repeat'] = 0;
  508. }
  509.  
  510. /**
  511.  * @desc Xac dinh toa do va do rong cua mot cell
  512.  */
  513. function _tableGetWCell(&$table, $i,$j){
  514.     $c = &$table['cells'][$i][$j];
  515.     if ($c){
  516.         if (isset($c['x0'])) return array($c['x0'], $c['w0']);
  517.         $x = 0;
  518.         $wc = &$table['wc'];
  519.         for ($k=0;$k<$j;$k++) $x += $wc[$k];
  520.         $w = $wc[$j];
  521.         if (isset($c['colspan'])){
  522.             for ($k=$j+$c['colspan']-1;$k>$j;$k--)
  523.                 $w += $wc[$k];
  524.         }
  525.         $c['x0'] = $x;
  526.         $c['w0'] = $w;
  527.         return array($x, $w);
  528.     }
  529.     return array(0,0);
  530. }
  531.  
  532. function _tableGetHCell(&$table, $i,$j){
  533.     $c = &$table['cells'][$i][$j];
  534.     if ($c){
  535.         if (isset($c['h0'])) return $c['h0'];
  536.         $hr = &$table['hr'];
  537.         $h = $hr[$i];
  538.         if (isset($c['rowspan'])){
  539.             for ($k=$i+$c['rowspan']-1;$k>$i;$k--)
  540.                 $h += $hr[$k];
  541.         }
  542.         $c['h0'] = $h;
  543.         return $h;
  544.     }
  545.     return 0;
  546. }
  547.  
  548. function _tableRect($x, $y, $w, $h, $type=1){
  549.     if (strlen($type)==4)
  550.     {
  551.         $x2 = $x + $w; $y2 = $y + $h;
  552.         if (intval($type{0})) $this->Line($x , $y , $x2, $y );
  553.         if (intval($type{1})) $this->Line($x2, $y , $x2, $y2);
  554.         if (intval($type{2})) $this->Line($x , $y2, $x2, $y2);
  555.         if (intval($type{3})) $this->Line($x , $y , $x , $y2);
  556.     }
  557.     elseif ((int)$type===1)
  558.         $this->Rect($x, $y, $w, $h);
  559.     elseif ((int)$type>1 && (int)$type<11)
  560.     {
  561.         $width = $this->LineWidth;
  562.         $this->SetLineWidth($type * $this->LineWidth);
  563.         $this->Rect($x, $y, $w, $h);
  564.         $this->SetLineWidth($width);
  565.     }
  566. }
  567.  
  568. function _tableDrawBorder(&$table){
  569.     //When fill a cell, it overwrite the border of prevous cell, then I have to draw border at the end
  570.     foreach ($table['listborder'] as $_z=>$c){
  571.         list($x,$y,$w,$h,$s) = $c;
  572.         $this->_tableRect($x,$y,$w,$h,$s);
  573.     }
  574.  
  575.     $table['listborder'] = array();
  576. }
  577. function _tableWriteRow(&$table,$i,$x0){
  578.     $maxh = 0;
  579.     for ($j=0;$j<$table['nc'];$j++){
  580.         $h = $this->_tableGetHCell($table, $i, $j);
  581.         if ($maxh < $h) $maxh = $h;
  582.     }
  583.     if ($table['lasty']+$maxh>$this->bottom && $table['multipage']){
  584.         if ($maxh+$table['repeatH'] > $this->height){
  585.             $msg = 'Height of this row is greater than page height!';
  586.             $h = $this->countLine(0,$msg) * $this->getLineHeight();
  587.             $this->SetFillColor(255,0,0);
  588.             $this->Rect($this->x, $this->y=$table['lasty'], $table['w'], $h, 'F');
  589.             $this->MultiCell($table['w'],$this->getLineHeight(),$msg);
  590.             $table['lasty'] += $h;
  591.             return ;
  592.         }
  593.         $this->_tableDrawBorder($table);
  594.         $this->AddPage($this->CurOrientation);
  595.  
  596.         $table['lasty'] = $this->y;
  597.         if ($table['repeat']){
  598.             foreach ($table['repeat'] as $_z=>$r){
  599.                 if ($r==$i) continue;
  600.                 $this->_tableWriteRow($table,$r,$x0);
  601.             }
  602.         }
  603.     }
  604.     $y = $table['lasty'];
  605.     for ($j=0;$j<$table['nc'];$j++){
  606.         if (isset($table['cells'][$i][$j]) && $table['cells'][$i][$j]){
  607.             $c = &$table['cells'][$i][$j];
  608.             list($x,$w) = $this->_tableGetWCell($table, $i, $j);
  609.             $h = $this->_tableGetHCell($table, $i, $j);
  610.             $x += $x0;
  611.             //Align
  612.             $this->x = $x; $this->y = $y;
  613.             $align = isset($c['a'])? $c['a'] : 'L';
  614.             //Vertical align
  615.             $verticalAlign = (!isset($c['va']))? 'T': $c['va'];
  616.             $verticalAlign = (strpos('TMB', $verticalAlign)=== false)? 'T': $verticalAlign;
  617.  
  618.             if ($verticalAlign == 'M')     $this->y += ($c['mih']>$c['ch'])? ($h-$c['ch'])/2: ($h-$c['mih'])/2;
  619.             elseif ($verticalAlign == 'B') $this->y += ($c['mih']>$c['ch'])? $h-$c['ch']: $h-$c['mih'];
  620.             //Fill
  621.             $fill = isset($c['bgcolor']) ? $c['bgcolor']
  622.                 : (isset($table['bgcolor'][$i]) ? $table['bgcolor'][$i]
  623.                 : (isset($table['bgcolor'][-1]) ? $table['bgcolor'][-1] : 0));
  624.             if ($fill){
  625.                 $color = Color::HEX2RGB($fill);
  626.                 $this->SetFillColor($color[0],$color[1],$color[2]);
  627.                 $this->Rect($x, $y, $w, $h, 'F');
  628.             };
  629.             //Content
  630.             $f = $this->_setFontText($c);
  631.  
  632.             if (isset($c['color']))
  633.             {
  634.                 $color = Color::HEX2RGB($c['color']);
  635.                 $this->SetTextColor($color[0],$color[1],$color[2]);
  636.             }else unset($color);
  637.  
  638.             $this->MultiCell($w,$this->getLineHeight($f),implode("\n",$c['text']),0,$align);
  639.  
  640.             if (isset($color))
  641.                 $this->SetTextColor(0);
  642.  
  643.             //Border
  644.             if (isset($c['border'])){
  645.                 $table['listborder'][] = array($x,$y,$w,$h,$c['border']);
  646.             }elseif (isset($table['border']) && $table['border'])
  647.                 $table['listborder'][] = array($x,$y,$w,$h,$table['border']);
  648.         }
  649.     }
  650.     $table['lasty'] += $table['hr'][$i];
  651.     $this->y = $table['lasty'];
  652. }
  653. function _setFontText(&$c){
  654.     //$count = 0;
  655.  
  656.     if (isset($c['fontSize']) && ($c['fontSize'] >0)){
  657.         $fontSize   = $c['fontSize'];
  658.         //$count++;
  659.     }else $fontSize   = $this->defaultFontSize;
  660.     if (isset($c['fontFamily'])){
  661.         $fontFamily = $c['fontFamily'];
  662.         //$count++;
  663.     }else $fontFamily = $this->defaultFontFamily;
  664.     if (isset($c['fontStyle'])){
  665.           $STYLE     = explode(",", $c['fontStyle']);
  666.           $fontStyle = '';
  667.  
  668.           foreach($STYLE AS $si=>$style)  $fontStyle .= strtoupper(substr(trim($style), 0, 1));
  669.          // $count++;
  670.     }else $fontStyle  = $this->defaultFontStyle;
  671.  
  672.    $this->SetFont($fontFamily, $fontStyle, $fontSize);
  673.    return $fontSize;
  674. }
  675. function _tableWrite(&$table){
  676.     //if ($table['w']>$this->width+5)
  677.     //debug($this->CurOrientation,$table['w'],$this->width);
  678.     if ($this->CurOrientation == 'P' && $table['w']>$this->width+5) $this->AddPage('L');
  679.     $x0 = $this->x;
  680.     $y0 = $this->y;
  681.     if (isset($table['a'])){
  682.         if ($table['a']=='C'){
  683.             $x0 += (($this->right-$x0) - $table['w'])/2;
  684.         }elseif ($table['a']=='R'){
  685.             $x0 = $this->right - $table['w'];
  686.         }
  687.     }
  688.     $table['lasty'] = $y0;
  689.     $table['listborder'] = array();
  690.     for ($i=0;$i<$table['nr'];$i++) $this->_tableWriteRow($table, $i, $x0);
  691.     $this->_tableDrawBorder($table);
  692.     //echo "<pre>";print_r($table);
  693. }
  694.  
  695. function htmltable(&$html,$multipage=1){
  696.     $a = $this->AutoPageBreak;
  697.     $this->SetAutoPageBreak(0,$this->bMargin);
  698.     $HTML = explode("<table", $html);
  699.     foreach ($HTML as $i=>$table)
  700.     {
  701.         if (strlen($table)<6) continue;
  702.         $table = '<table' . $table;
  703.         $table = $this->_tableParser($table);
  704.         $table['multipage'] = $multipage;
  705.         $this->_tableColumnWidth($table);
  706.         $this->_tableWidth($table);
  707.         $this->_tableHeight($table);
  708.         $this->_tableWrite($table);
  709.     }
  710.     $this->SetAutoPageBreak($a,$this->bMargin);
  711. }
  712. }//PDFTable
  713. ?>
Advertisement
Add Comment
Please, Sign In to add comment