von der Born
 
Startseite

Über mich
Referenzen
Veröffentlichungen
Kontakt
Impressum

Projekte
Software
PHP Snippets
PHP Counter

Interessen
Forexhandel
Meerschweinchen
OS X Programmierung
Kultautos
Metalldetektor


Besucher Statistik
» 2 Online
» 30 Heute
» 311 Woche
» 1585 Monat
» 14586 Jahr
» 209211 Gesamt
Rekord: 219 (08.11.2010)

Expended tables with FPDF

FPDF is a free PHP Library to create PDF documents in PHP. The functions Cell and MultiCell will help to create tables. But this is always a lot of work. Under FPDF Addons you will find some basic solutions for creating tables. I took the solution from Olivier and made a lot of changes. The new solution will be interested for other programmers who like to create reports like invoices. The solution based on arrays. You will have an array for each Cell. Several Cells we be a column. A function will read this array and create your needed table. You do not need to use the Cell or MultiCell functions yourself for creating tables. The function is working with MultiCell, because it allows several lines in one Cell.

Functions
  • Support of several lines inside a cell
  • Different colors for border, text and background
  • Size for border and possibility to hide parts of the border (top, bottom, left, right)
  • Support bold, italic and underline
  • Different number of rows in the columns

The coming example will create this table:


PHP Code for create this table
<?
// create pdf
$pdf=new MYPDF('P','mm','A4');
$pdf->AliasNbPages();
$pdf->SetMargins($pdf->left$pdf->top$pdf->right); 
$pdf->AddPage();
   
// create table
$columns = array();      
   
// header col
$col = array();
$col[] = array('text' => 'Datum''width' => '20''height' => '5''align' => 'C''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '135,206,250''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Text''width' => '125''height' => '5''align' => 'C''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '135,206,250''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Soll''width' => '15''height' => '5''align' => 'C''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '135,206,250''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Haben''width' => '15''height' => '5''align' => 'C''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '135,206,250''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Saldo''width' => '15''height' => '5''align' => 'C''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '135,206,250''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');   
$columns[] = $col;
   
// data col
$col = array();
$col[] = array('text' => '01.12.2010''width' => '20''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Rechnungs Nr 123456789''width' => '125''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => '120.50''width' => '15''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '12''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => '''width' => '15''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '255,255,255''textcolor' => '0,0,255''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => '120.50S''width' => '15''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');       
$columns[] = $col;
 
// data col
$col = array();
$col[] = array('text' => '15.12.2010''width' => '20''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Zahlung: 123456789''width' => '125''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => '''width' => '15''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => '120.50''width' => '15''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => '0.00H''width' => '15''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');       
$columns[] = $col;

$col = array();
$col[] = array('text' => 'Ist der Text zu lang, ist das kein Problem''width' => '50''height' => '5''align' => 'R''font_name' => 'Arial''font_size' => '12''font_style' => '''fillcolor' => '0,0,255''textcolor' => '0,255,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Auch mit mehreren Farben ist es kein Problem''width' => '50''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => 'B''fillcolor' => '255,255,0''textcolor' => '0,255,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'So ist das Bauen einer Tabelle einfach nur einfach. MuliCell macht es einfach. Okay das ist nun lang genug''width' => '50''height' => '5''align' => 'C''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,255,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Erstellen von Rechnungen sind kein Problem mehr''width' => '40''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,0,255''textcolor' => '0,255,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$columns[] = $col;

$col = array();
$col[] = array('text' => 'Einfach nur mal eine Zeile ohne Rahmen''width' => '190''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,255,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'TB');
$columns[] = $col;

$col = array();
$col[] = array('text' => 'Einfach nur mal eine Zeile in der Tabelle''width' => '80''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,0,0''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$col[] = array('text' => 'Gerne auch mit einer Spalte mehr''width' => '110''height' => '5''align' => 'L''font_name' => 'Arial''font_size' => '8''font_style' => '''fillcolor' => '255,255,255''textcolor' => '0,0,0''drawcolor' => '0,0,0''linewidth' => '0.4''linearea' => 'LTBR');
$columns[] = $col;

// Draw Table   
$pdf->WriteTable($columns);
   
// Show PDF   
$pdf->Output();
?>

When you create an invoice or an list, you will use loops for filling the arrays. This here is only a simple example.

Documentation

The source code is self explaining. Severl cells will be a column. Several columns will be used to create the table with help of new function CreateTable. You have to fill the complete Array for each Cell. Colors are in RGB as CSV. Linearea keeps the information which borders to show. Just play with the exmaple.

Changes on FPDF Class
<?
// own pdf structure

class MYPDF extends FPDF
{
   
// Margins
   
var $left 10;
   var 
$right 10;
   var 
$top 10;
   var 
$bottom 10;
         
   
// Create Table
   
function WriteTable($tcolums)
   {
      
// go through all colums
      
for ($i 0$i sizeof($tcolums); $i++)
      {
         
$current_col $tcolums[$i];
         
$height 0;
         
         
// get max height of current col
         
$nb=0;
         for(
$b 0$b sizeof($current_col); $b++)
         {
            
// set style
            
$this->SetFont($current_col[$b]['font_name'], $current_col[$b]['font_style'], $current_col[$b]['font_size']);
            
$color explode(","$current_col[$b]['fillcolor']);
            
$this->SetFillColor($color[0], $color[1], $color[2]);
            
$color explode(","$current_col[$b]['textcolor']);
            
$this->SetTextColor($color[0], $color[1], $color[2]);            
            
$color explode(","$current_col[$b]['drawcolor']);            
            
$this->SetDrawColor($color[0], $color[1], $color[2]);
            
$this->SetLineWidth($current_col[$b]['linewidth']);
                        
            
$nb max($nb$this->NbLines($current_col[$b]['width'], $current_col[$b]['text']));            
            
$height $current_col[$b]['height'];
         }  
         
$h=$height*$nb;
         
         
         
// Issue a page break first if needed
         
$this->CheckPageBreak($h);
         
         
// Draw the cells of the row
         
for($b 0$b sizeof($current_col); $b++)
         {
            
$w $current_col[$b]['width'];
            
$a $current_col[$b]['align'];
            
            
// Save the current position
            
$x=$this->GetX();
            
$y=$this->GetY();
            
            
// set style
            
$this->SetFont($current_col[$b]['font_name'], $current_col[$b]['font_style'], $current_col[$b]['font_size']);
            
$color explode(","$current_col[$b]['fillcolor']);
            
$this->SetFillColor($color[0], $color[1], $color[2]);
            
$color explode(","$current_col[$b]['textcolor']);
            
$this->SetTextColor($color[0], $color[1], $color[2]);            
            
$color explode(","$current_col[$b]['drawcolor']);            
            
$this->SetDrawColor($color[0], $color[1], $color[2]);
            
$this->SetLineWidth($current_col[$b]['linewidth']);
            
            
$color explode(","$current_col[$b]['fillcolor']);            
            
$this->SetDrawColor($color[0], $color[1], $color[2]);
            
            
            
// Draw Cell Background
            
$this->Rect($x$y$w$h'FD');
            
            
$color explode(","$current_col[$b]['drawcolor']);            
            
$this->SetDrawColor($color[0], $color[1], $color[2]);
            
            
// Draw Cell Border
            
if (substr_count($current_col[$b]['linearea'], "T") > 0)
            {
               
$this->Line($x$y$x+$w$y);
            }            
            
            if (
substr_count($current_col[$b]['linearea'], "B") > 0)
            {
               
$this->Line($x$y+$h$x+$w$y+$h);
            }            
            
            if (
substr_count($current_col[$b]['linearea'], "L") > 0)
            {
               
$this->Line($x$y$x$y+$h);
            }
                        
            if (
substr_count($current_col[$b]['linearea'], "R") > 0)
            {
               
$this->Line($x+$w$y$x+$w$y+$h);
            }
            
            
            
// Print the text
            
$this->MultiCell($w$current_col[$b]['height'], $current_col[$b]['text'], 0$a0);
            
            
// Put the position to the right of the cell
            
$this->SetXY($x+$w$y);         
         }
         
         
// Go to the next line
         
$this->Ln($h);          
      }                  
   }

   
   
// If the height h would cause an overflow, add a new page immediately
   
function CheckPageBreak($h)
   {
      if(
$this->GetY()+$h>$this->PageBreakTrigger)
         
$this->AddPage($this->CurOrientation);
   }


   
// Computes the number of lines a MultiCell of width w will take
   
function NbLines($w$txt)
   {
      
$cw=&$this->CurrentFont['cw'];
      if(
$w==0)
         
$w=$this->w-$this->rMargin-$this->x;
      
$wmax=($w-2*$this->cMargin)*1000/$this->FontSize;
      
$s=str_replace("\r"''$txt);
      
$nb=strlen($s);
      if(
$nb>and $s[$nb-1]=="\n")
         
$nb--;
      
$sep=-1;
      
$i=0;
      
$j=0;
      
$l=0;
      
$nl=1;
      while(
$i<$nb)
      {
         
$c=$s[$i];
         if(
$c=="\n")
         {
            
$i++;
            
$sep=-1;
            
$j=$i;
            
$l=0;
            
$nl++;
            continue;
         }
         if(
$c==' ')
            
$sep=$i;
         
$l+=$cw[$c];
         if(
$l>$wmax)
         {
            if(
$sep==-1)
            {
               if(
$i==$j)
                  
$i++;
            }
            else
               
$i=$sep+1;
            
$sep=-1;
            
$j=$i;
            
$l=0;
            
$nl++;
         }
         else
            
$i++;
      }
      return 
$nl;
   }
        
}
?>


Finish! You are not allowed too copy my text, but you are welcome, to link to my page. The sourcecode is freeware, you can use it, change it or dou what ever you want. I hope this function will help you.



Sparen Sie mit Ihren Tintenpatronen und Druckerpatronen und verwenden Sie meinen PHP Counter

© 2007 by Philipp von der Born