1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
| <?php
//define('FPDF_FONTPATH','/font/');
require('fpdf.php');
$chaine = '1, designation1, 100,2, designation2, 200,3, designation3, 350.50,4, designation4, 400.25';
class Load_String
{
private $separateur = ',';
private $split_string = 3;
private $import_string = '';
private $pos_add;
private $tab_result = array();
private $tab_total = array();
public function __construct($import_string,$separateur,$split,$pos_add)
{
//récupération des valeurs passées en paramètre lors de l'initialisation de la classe
$this->import_string = isset($import_string) && trim($import_string != '') ? $import_string : $this->import_string;
$this->separateur = isset($separateur) ? $separateur : $this->separateur;
$this->split_string = !empty($split) ? $split : $this->split_string;
// Si $pos_add n'est pas défini on suppose que le prix est en dernier ($pos_add = $split)
$this->pos_add = !empty($pos_add) ? $pos_add : $this->split_string;
}
public function Tab_result ()
{
$tab_chaine = explode($this->separateur,$this->import_string);
//nettoyage
$tab_chaine = array_map('trim',$tab_chaine); //Supprime les espaces vides
$tab_chaine = array_filter($tab_chaine); //Supprime les éléments vides
if (count($tab_chaine) % $this->split_string == 0)// Si le nombre d'éléments du tableau est un multiple de $split
{
$this->tab_result = array_chunk($tab_chaine,$this->split_string);//fait des tableaux de $split = 3 éléments
return $this->tab_result;
}
else return false;
}
public function Total ()
{
// Utilise la fonction "Test_key" pour récupérer dans "$this->tab_total" les éléments à additionner
array_walk_recursive($this->tab_result,array('Load_String','Test_key'));
return array_sum($this->tab_total);
}
private function Test_key ($item, $key)
{
if($key == $this->pos_add - 1) $this->tab_total[] = floatval($item);
}
}
$separateur = ','; // séparateur dans la chaine
$segment = 3;// longueur des segments à récupérer dans la chaine
$pos_add = 3;// position des éléments à additionner dans les segments
//Initialisation de la classe en passant les paramètres
$data = new Load_String($chaine,$separateur,$segment,$pos_add);
/*Utilisation de la fonction Tab_result() pour récupérer le tableau
- renvoie le tableau
- ou renvoie un tableau vide si la chaine est vide
- ou renvoie false s'il manque un élément dans la chaine.*/
$tab_data = $data->Tab_result();
// Utilisation de la fonction Total() pour récupérer le total
$total = $data->Total();
class PDF extends FPDF
{
//Chargement des données
function LoadData($file)
{
//Lecture des lignes du fichier
$lines=file($file);
$data=array();
foreach($lines as $line)
$data[]=explode(';',chop($line));
return $data;
}
//Tableau simple
function BasicTable($header,$data,$total)
{
//En-tête
foreach($header as $col)
$this->Cell(40,7,$col,1);
$this->Ln();
//Données
foreach($data as $row)
{
foreach($row as $col)
$this->Cell(40,6,$col,1);
$this->Ln();
}
// Ajout de la ligne total
$this->Cell(40,8,'',1);
$this->Cell(40,8,'Total',1);
$this->Cell(40,8,$total,1);
$this->Ln();
}
//Tableau amélioré
function ImprovedTable($header,$data,$total)
{
//Largeurs des colonnes
$w=array(30,60,30);
//En-tête
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C');
$this->Ln();
//Données
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR');
$this->Cell($w[1],6,$row[1],'LR');
$this->Cell($w[2],6,number_format(floatval($row[2]),2,',',' '),'LR',0,'R');
$this->Ln();
}
// Ajout de la ligne total
$this->Cell(30,8,'',1,0);
$this->Cell(60,8,'Total',1,0);
$this->Cell(30,8,$total,1,0,'R');
$this->Ln();
//Trait de terminaison
$this->Cell(array_sum($w),0,'','T');
}
//Tableau coloré
function FancyTable($header,$data,$total)
{
//Couleurs, épaisseur du trait et police grasse
$this->SetFillColor(255,0,0);
$this->SetTextColor(255);
$this->SetDrawColor(128,0,0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//En-tête
$w=array(30,60,30);
for($i=0;$i<count($header);$i++)
$this->Cell($w[$i],7,$header[$i],1,0,'C',1);
$this->Ln();
//Restauration des couleurs et de la police
$this->SetFillColor(224,235,255);
$this->SetTextColor(0);
$this->SetFont('');
//Données
$fill=false;
foreach($data as $row)
{
$this->Cell($w[0],6,$row[0],'LR',0,'C',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
$this->Cell($w[2],6,number_format(floatval($row[2]),2,',',' '),'LR',0,'C',$fill);
$this->Ln();
$fill=!$fill;
}
// Ajout de la ligne total avec une couleur spécifique
$this->SetFillColor(194,250,254);
$this->SetTextColor(0);
$this->SetFont('');
$this->Cell(30,8,'',1,0,'C',true);
$this->Cell(60,8,'Total',1,0,'C',true);
$this->Cell(30,8,$total,1,0,'C',true);
$this->Ln();
$this->Cell(array_sum($w),0,'','T');
}
}
if (is_array($tab_data))
{
if (count($tab_data) > 0)
{
$pdf=new PDF();
//Titres des colonnes
$header=array('Quantité','Désignation','prix');
//Chargement des données
$data=$tab_data;
$pdf->SetFont('Arial','',12);
$pdf->AddPage();
$pdf->BasicTable($header,$data,$total);
$pdf->AddPage();
$pdf->ImprovedTable($header,$data,$total);
$pdf->AddPage();
$pdf->FancyTable($header,$data,$total);
$pdf->Output();
}
else echo 'La chaine est vide';
}
else echo 'Il manque un élément dans la chaine';
?> |
Partager