|
Invité de passage
Développeur informatique Inscription : janvier 2012 Messages : 1 Détails du profil  Informations personnelles : Sexe :  Localisation : France Informations professionnelles :
Activité : Développeur informatique Informations forums :
Inscription : janvier 2012 Messages : 1 Points : 0 Points : 0
|
Problème d'affichage PDF avec FPDF (classe utilisable en PHP)
Bonjour à tous !
Je me tourne vers vous aujourd’hui dans le but de trouver des réponses à un problème que je rencontre.
J'essaye de sortir un fichier PDF depuis une base de donnée MYSQL grâce a fpdf, pour cela , j'utilise 2 scripts que j'ai directement utilisé sur fpdf.org.
Le problème est que dans une des colonne de ma table , il se peut que le texte soit coloré ou souligné ou en gras. Dans la table , ceci est retranscrit avec les balises correspondantes (par ex : <b>le texte </b>). Et donc afin de récupérer le texte en gras dans mon fichier PDF, j'utilise un script qui me permet d'écrire directement du HTML dans mon PDF. Le problème est que la colonne qui contient le texte en gras ne veut pas s'afficher correctement dans ma table PDF.
Je pense que vous comprendrez mieux avec le code :
Le script me permettant d'afficher ma table :
Code :
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
|
<?php
class PDF_MC_Table
{
var $widths;
var $aligns;
function SetWidths($w)
{
//Tableau des largeurs de colonnes
$this->widths=$w;
}
function SetAligns($a)
{
//Tableau des alignements de colonnes
$this->aligns=$a;
}
function CheckPageBreak($h)
{
//Si la hauteur h provoque un débordement, saut de page manuel
if($this->GetY()+$h>$this->PageBreakTrigger)
$this->AddPage($this->CurOrientation);
}
function NbLines($w,$txt)
{
//Calcule le nombre de lignes qu'occupe un MultiCell de largeur w
$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>0 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;
}
function Row($data)
{
//Calcule la hauteur de la ligne
$nb=0;
for($i=0;$i<count($data);$i++)
{
$nb=max($nb,$this->NbLines($this->widths[$i],$data[$i]));
}
$h=5*$nb;
//Effectue un saut de page si nécessaire
$this->CheckPageBreak($h);
//Dessine les cellules
for($i=0;$i<count($data);$i++)
{
$w=$this->widths[$i];
$a=isset($this->aligns[$i]) ? $this->aligns[$i] : 'C';
//Sauve la position courante
$x=$this->GetX();
$y=$this->GetY();
//Dessine le cadre
$this->Rect($x,$y,$w,$h);
//Imprime le texte
$this->MultiCell($w,5,$data[$i],0,$a);
//Repositionne à droite
$this->SetXY($x+$w,$y);
}
//Va à la ligne
$this->Ln($h);
}
}
?> |
Le script qui me permet de modifier le code HTML pour que PDF prenne en compte le gras :
Code :
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
| <?php
require_once('mc_table.php');
// fonction hex2dec
// retourne un tableau associatif (clés : R,V,B) à
// partir d'un code html de couleur hexa (ex : #3FE5AA)
function hex2dec($couleur = "#000000"){
$R = substr($couleur, 1, 2);
$rouge = hexdec($R);
$V = substr($couleur, 3, 2);
$vert = hexdec($V);
$B = substr($couleur, 5, 2);
$bleu = hexdec($B);
$tbl_couleur = array();
$tbl_couleur['R']=$rouge;
$tbl_couleur['V']=$vert;
$tbl_couleur['B']=$bleu;
return $tbl_couleur;
}
//conversion pixel -> millimètre en 72 dpi
function px2mm($px){
return $px*25.4/72;
}
function txtentities($html){
$trans = get_html_translation_table(HTML_ENTITIES);
$trans = array_flip($trans);
return strtr($html, $trans);
}
////////////////////////////////////
class PDF_HTML extends PDF_MC_Table
{
//variables du parseur html
var $B;
var $I;
var $U;
var $HREF;
var $fontList;
var $issetfont;
var $issetcolor;
function PDF_HTML($orientation='P', $unit='mm', $format='A4')
{
//Appel au constructeur parent
$this->FPDF($orientation,$unit,$format);
//Initialisation
$this->B=0;
$this->I=0;
$this->U=0;
$this->HREF='';
$this->fontlist=array('arial', 'times', 'courier', 'helvetica', 'symbol');
$this->issetfont=false;
$this->issetcolor=false;
}
function WriteHTML($html)
{
//Parseur HTML
$html=strip_tags($html,"<b><u><i><a><img><p><br><strong><em><font><tr><blockquote>"); //supprime tous les tags sauf ceux reconnus
$html=str_replace("\n",' ',$html); //remplace retour à la ligne par un espace
$a=preg_split('/<(.*)>/U',$html,-1,PREG_SPLIT_DELIM_CAPTURE); //éclate la chaîne avec les balises
foreach($a as $i=>$e)
{
if($i%2==0)
{
//Texte
if($this->HREF)
$this->PutLink($this->HREF,$e);
else
$this->Write(5,stripslashes(txtentities($e)));
}
else
{
//Balise
if($e[0]=='/')
$this->CloseTag(strtoupper(substr($e,1)));
else
{
//Extraction des attributs
$a2=explode(' ',$e);
$tag=strtoupper(array_shift($a2));
$attr=array();
foreach($a2 as $v)
{
if(preg_match('/([^=]*)=["\']?([^"\']*)/',$v,$a3))
$attr[strtoupper($a3[1])]=$a3[2];
}
$this->OpenTag($tag,$attr);
}
}
}
}
function OpenTag($tag, $attr)
{
//Balise ouvrante
switch($tag){
case 'STRONG':
$this->SetStyle('B',true);
break;
case 'EM':
$this->SetStyle('I',true);
break;
case 'B':
case 'I':
case 'U':
$this->SetStyle($tag,true);
break;
case 'A':
$this->HREF=$attr['HREF'];
break;
case 'IMG':
if(isset($attr['SRC']) && (isset($attr['WIDTH']) || isset($attr['HEIGHT']))) {
if(!isset($attr['WIDTH']))
$attr['WIDTH'] = 0;
if(!isset($attr['HEIGHT']))
$attr['HEIGHT'] = 0;
$this->Image($attr['SRC'], $this->GetX(), $this->GetY(), px2mm($attr['WIDTH']), px2mm($attr['HEIGHT']));
}
break;
case 'TR':
case 'BLOCKQUOTE':
case 'BR':
$this->Ln(5);
break;
case 'P':
$this->Ln(10);
break;
case 'FONT':
if (isset($attr['COLOR']) && $attr['COLOR']!='') {
$coul=hex2dec($attr['COLOR']);
$this->SetTextColor($coul['R'],$coul['V'],$coul['B']);
$this->issetcolor=true;
}
if (isset($attr['FACE']) && in_array(strtolower($attr['FACE']), $this->fontlist)) {
$this->SetFont(strtolower($attr['FACE']));
$this->issetfont=true;
}
break;
}
}
function CloseTag($tag)
{
//Balise fermante
if($tag=='STRONG')
$tag='B';
if($tag=='EM')
$tag='I';
if($tag=='B' || $tag=='I' || $tag=='U')
$this->SetStyle($tag,false);
if($tag=='A')
$this->HREF='';
if($tag=='FONT'){
if ($this->issetcolor==true) {
$this->SetTextColor(0);
}
if ($this->issetfont) {
$this->SetFont('arial');
$this->issetfont=false;
}
}
}
function SetStyle($tag, $enable)
{
//Modifie le style et sélectionne la police correspondante
$this->$tag+=($enable ? 1 : -1);
$style='';
foreach(array('B','I','U') as $s)
{
if($this->$s>0)
$style.=$s;
}
$this->SetFont('',$style);
}
function PutLink($URL, $txt)
{
//Place un hyperlien
$this->SetTextColor(0,0,255);
$this->SetStyle('U',true);
$this->Write(5,$txt,$URL);
$this->SetStyle('U',false);
$this->SetTextColor(0);
}
}//fin classe
?> |
Et la page php me permettant de construire le fichier PDF :
Code :
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
|
<?php
session_start();
require("connexion.php");
require("phpToPDF/fpdf.php");
mysql_connect("127.0.0.1","root","");
mysql_select_db("intranet");
$req = mysql_query("select * from main_courante_message where idMainCourante =".$_SESSION['id']);
$pdf=new FPDF('L');
$pdf->AddPage();
$pdf->SetFont('Arial','',14);
$pdf->SetWidths(array(15,29,30,30,40,120,21));
$pdf->SetFillColor(255,79,83);
$pdf->Cell('15','10','Heure','1','0','C','true');
$pdf->Cell('29','10','Date','1','0','C','true');
$pdf->Cell('30','10','Redacteur','1','0','C','true');
$pdf->Cell('30','10','Cellule','1','0','C','true');
$pdf->Cell('40','10','Type de message','1','0','C','true');
$pdf->Cell('120','10','Objet','1','0','C','true');
$pdf->Cell('21','10','Transmis','1','1','C','true');
$pdf->SetFillColor(196,196,189);
while($row = mysql_fetch_array($req))
{
$texte = $pdf->WriteHTML($row[6]);
$pdf->Row(array($row[1],$row[2],$row[3],$row[4],$row[5],$texte,$row[7]));
}
$pdf->Output();
?> |
et je vous rajoute une image pour vous montrez le résultat obtenu :
http://www.hostingpics.net/viewer.php?id=515147img.jpg
Ce que je veux c'est que le texte : "effefefefefeefe" se position dans la colonne qui a pour titre : "Objet".
En espérant avoir une réponse je vous remercie d'avance.
Cordialement
|