Bonjour,
La classe n'est pas en ligne car pas terminée, mais pour vous montrer le principe, je vous la colle içi:
<?php
//inclusion de fpdf
class PDF extends FPDF
{
/**
* largeur d'une page au format portrait
*/
private $largeurPage=190;
/**
* Hauteur d'une cellule de tableau
*/
private $hauteurCell=6;
/**
* PDF::table
*
* créer un tableau de donnée dans un document pdf
*
* @access public
* @param array $header Les entêtes du tableau (cad le nom des colonnes)
* @param array $data Les données du tableau
* @param mixed $pourcentage taille en pourcentage du tableau
* Si un entier est passé, les colonnes
* on la même largeur et le tableau occupe
* x pourcent de la largeur de la page.
* Si un array de int est passé,
* c'est la largeur des colonnes qui est déterminée
* @return none
*/
public function table($header,$data,$pourcentage=100){
$nbCell=count($header);
if(is_array($pourcentage) && count($pourcentage)==$nbCell && array_sum($pourcentage)==100){
foreach($pourcentage as $value){
$largeurCell[]=$this->largeurPage*($value/100);
}
$largeurTab=$this->largeurPage;
} else {
$largeurCell=array_fill(0,$nbCell,($this->largeurPage*($pourcentage/100))/$nbCell);
$largeurTab=$this->largeurPage*($pourcentage/100);
}
//global parameter
$this->SetFont('Arial','',14);
//header parameter
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//header
$this->Cell($this->largeurPage,$this->hauteurCell);
$this->Ln();
for($i=0;$i<$nbCell;$i++)
$this->Cell($largeurCell[$i],7,utf8_decode($header[$i]),1,0,'C',1);
$this->Ln();
//content parameter
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
//content
$fill=0;
foreach($data as $row){
if(isset($row['separator']) && $row['separator']){
$this->Cell($largeurTab,1,'','TB');
} else {
$i=0;
foreach($row as $cell){
$this->Cell(
$largeurCell[$i++],
$this->hauteurCell,
$cell,
'LR',
0,
'C',
$fill
);
}
$fill=!$fill;
}
$this->Ln();
}
$this->Cell($largeurTab,1,'','T');
}
}
à utiliser comme suit:
<?php
$header=array('col1','col2');
$data=array(
array(1,2),
array(3,4),
array('separator'=>true),
array(5,6),
array(7,8)
);
$pdf= new PDF();
$pdf->table($header,$data,array(20,80));
$pdf->Output();
?>