FPDF MultiCell()
yirkkiller
-
yirkkiller -
yirkkiller -
Bonjour,
Je rencontre quelques problèmes sur fpdf.
J'ai un fichier CSV enregistré dans mon répertoire csv/importdateheure.csv.
Ce fichier contient le Nom, prénom, direction, etc... de mes collègues.
Je me suis aperçu qu'avec la fonction cell() seule, certains champs dépassaient sur les colonnes d'à côté.
Je me suis donc renseigné et ai trouvé la fonction multicell(). Seulement, tous les champs ne sont pas obligés de passer en multicell. J'ai donc trouvé la formule GetStringWidth() qui permet de calculer la longueur du champ. Avec un test, je suis capable maintenant de savoir si la ligne a besoin d'être en multicell ou non.
Ce test fonctionne, étant donné que quand je génère mon fichier PDF, seules les lignes qui dépassent ne marchent pas.
Ces lignes là sont mal organisées, toutes dans la colonne A (voir capture d'écran).
En débuggant avec ZendStudio, j'ai vu que j'obtiens "Undefined offset" 1 2 3 4 5 6 sur les lignes entre 40 et 64.
Voici mon code :
<?php
require('scripts/fpdf.php');
# Variable de test
$_GET['filename'] = "cvs/import1703201041.csv";
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 coloré
function FancyTable($header,$data)
{
//Couleurs, épaisseur du trait et police grasse
$this->SetFillColor(216,216,216);
$this->SetTextColor(255);
$this->SetDrawColor(0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//En-tête
$w=array(40,40,20,40,40,40,60);
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)
{
$long0 = $this->GetStringWidth($row[0]);
$long1 = $this->GetStringWidth($row[1]);
$long2 = $this->GetStringWidth($row[2]);
$long3 = $this->GetStringWidth($row[3]);
$long4 = $this->GetStringWidth($row[4]);
$long5 = $this->GetStringWidth($row[5]);
$long6 = $this->GetStringWidth($row[6]);
if (($long0 > $w[0]) or ($long1 > $w[1]) or ($long2 > $w[2]) or ($long3 > $w[3]) or ($long4 > $w[4]) or ($long5 > $w[5]) or ($long6 > $w[6]))
{
$this->MultiCell($w[0],6,$row[0],'LR',0,'C',$fill);
$this->MultiCell($w[1],6,$row[1],'LR',0,'C',$fill);
$this->MultiCell($w[2],6,$row[2],'LR',0,'C',$fill);
$this->MultiCell($w[3],6,$row[3],'LR',0,'C',$fill);
$this->MultiCell($w[4],6,$row[4],'LR',0,'C',$fill);
$this->MultiCell($w[5],6,$row[5],'LR',0,'C',$fill);
$this->MultiCell($w[6],6,$row[6],'LR',0,'C',$fill);
}
else
{
$this->Cell($w[0],6,$row[0],'LR',0,'C',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'C',$fill);
$this->Cell($w[2],6,$row[2],'LR',0,'C',$fill);
$this->Cell($w[3],6,$row[3],'LR',0,'C',$fill);
$this->Cell($w[4],6,$row[4],'LR',0,'C',$fill);
$this->Cell($w[5],6,$row[5],'LR',0,'C',$fill);
$this->Cell($w[6],6,$row[6],'LR',0,'C',$fill);
}
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF('L');
//Titres des colonnes
$header=array('Nom','Prénom','Filiale','Direction','Service','Fonction','Mail');
//Chargement des données
$filename = $_GET['filename'];
$filename = substr($filename,4);
$file = "cvs/".$filename;
$data=$pdf->LoadData($file);
$pdf->SetFont('Arial','',8);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>
Voici le résultat :
<a href="http://imagik.fr/view-rl/225920">Ici</a>
Vous pouvez voir que tant que rien ne dépasse tout va bien, et dès qu'on arrive sur une ligne où un champ dépasse c'est le bordel, ensuite tout revient à la normale.
J'ai remarqué aussi qu'il sautait des lignes ? Pourquoi ?
Pourriez-vous m'aider svp ?
Merci beaucoup à tous
Je rencontre quelques problèmes sur fpdf.
J'ai un fichier CSV enregistré dans mon répertoire csv/importdateheure.csv.
Ce fichier contient le Nom, prénom, direction, etc... de mes collègues.
Je me suis aperçu qu'avec la fonction cell() seule, certains champs dépassaient sur les colonnes d'à côté.
Je me suis donc renseigné et ai trouvé la fonction multicell(). Seulement, tous les champs ne sont pas obligés de passer en multicell. J'ai donc trouvé la formule GetStringWidth() qui permet de calculer la longueur du champ. Avec un test, je suis capable maintenant de savoir si la ligne a besoin d'être en multicell ou non.
Ce test fonctionne, étant donné que quand je génère mon fichier PDF, seules les lignes qui dépassent ne marchent pas.
Ces lignes là sont mal organisées, toutes dans la colonne A (voir capture d'écran).
En débuggant avec ZendStudio, j'ai vu que j'obtiens "Undefined offset" 1 2 3 4 5 6 sur les lignes entre 40 et 64.
Voici mon code :
<?php
require('scripts/fpdf.php');
# Variable de test
$_GET['filename'] = "cvs/import1703201041.csv";
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 coloré
function FancyTable($header,$data)
{
//Couleurs, épaisseur du trait et police grasse
$this->SetFillColor(216,216,216);
$this->SetTextColor(255);
$this->SetDrawColor(0);
$this->SetLineWidth(.3);
$this->SetFont('','B');
//En-tête
$w=array(40,40,20,40,40,40,60);
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)
{
$long0 = $this->GetStringWidth($row[0]);
$long1 = $this->GetStringWidth($row[1]);
$long2 = $this->GetStringWidth($row[2]);
$long3 = $this->GetStringWidth($row[3]);
$long4 = $this->GetStringWidth($row[4]);
$long5 = $this->GetStringWidth($row[5]);
$long6 = $this->GetStringWidth($row[6]);
if (($long0 > $w[0]) or ($long1 > $w[1]) or ($long2 > $w[2]) or ($long3 > $w[3]) or ($long4 > $w[4]) or ($long5 > $w[5]) or ($long6 > $w[6]))
{
$this->MultiCell($w[0],6,$row[0],'LR',0,'C',$fill);
$this->MultiCell($w[1],6,$row[1],'LR',0,'C',$fill);
$this->MultiCell($w[2],6,$row[2],'LR',0,'C',$fill);
$this->MultiCell($w[3],6,$row[3],'LR',0,'C',$fill);
$this->MultiCell($w[4],6,$row[4],'LR',0,'C',$fill);
$this->MultiCell($w[5],6,$row[5],'LR',0,'C',$fill);
$this->MultiCell($w[6],6,$row[6],'LR',0,'C',$fill);
}
else
{
$this->Cell($w[0],6,$row[0],'LR',0,'C',$fill);
$this->Cell($w[1],6,$row[1],'LR',0,'C',$fill);
$this->Cell($w[2],6,$row[2],'LR',0,'C',$fill);
$this->Cell($w[3],6,$row[3],'LR',0,'C',$fill);
$this->Cell($w[4],6,$row[4],'LR',0,'C',$fill);
$this->Cell($w[5],6,$row[5],'LR',0,'C',$fill);
$this->Cell($w[6],6,$row[6],'LR',0,'C',$fill);
}
$this->Ln();
$fill=!$fill;
}
$this->Cell(array_sum($w),0,'','T');
}
}
$pdf=new PDF('L');
//Titres des colonnes
$header=array('Nom','Prénom','Filiale','Direction','Service','Fonction','Mail');
//Chargement des données
$filename = $_GET['filename'];
$filename = substr($filename,4);
$file = "cvs/".$filename;
$data=$pdf->LoadData($file);
$pdf->SetFont('Arial','',8);
$pdf->AddPage();
$pdf->FancyTable($header,$data);
$pdf->Output();
?>
Voici le résultat :
<a href="http://imagik.fr/view-rl/225920">Ici</a>
Vous pouvez voir que tant que rien ne dépasse tout va bien, et dès qu'on arrive sur une ligne où un champ dépasse c'est le bordel, ensuite tout revient à la normale.
J'ai remarqué aussi qu'il sautait des lignes ? Pourquoi ?
Pourriez-vous m'aider svp ?
Merci beaucoup à tous