IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Bibliothèques et frameworks PHP Discussion :

[FPDF] Tableau avec FPDF


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut [FPDF] Tableau avec FPDF
    Bonjour,

    J'essaye d'utiliser FPDF pour construire un tableau de résultats dans un pdf...
    Je me suis donc reporté au site http://fpdf.org/.

    J'ai donc utilisé le script:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    <?php
    require('../fpdf/fpdf.php');
     
    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('\n',chop($line));
        return $data;
    }
    //Tableau coloré
    function FancyTable($header,$data)
    {
        //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(40,35,45,40);
        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=0;
        foreach($data as $row)
        {
            $this->Cell($w[0],6,$row[0],'LR',0,'L',$fill);
            $this->Cell($w[1],6,$row[1],'LR',0,'L',$fill);
            $this->Cell($w[2],6,number_format($row[2],0,',',' '),'LR',0,'R',$fill);
            $this->Cell($w[3],6,number_format($row[3],0,',',' '),'LR',0,'R',$fill);
            $this->Ln();
            $fill=!$fill;
        }
        $this->Cell(array_sum($w),0,'','T');
    }
    }
    ?>
    Puis:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    /////PDF creation///////
    require_once ("pdf_results.php");
    $pdf=new PDF();
    //Titres des colonnes
    $header=array('rec_id','text_result','value','result_value','name','info1','info2','info3','info4','info5','info6','reference','comments');
    //Chargement des données
    $data=$pdf->LoadData('../table.txt');
    $pdf->SetFont('Arial','',10);
    $pdf->AddPage();
    $pdf->FancyTable($header,$data);
    $file_pdf='../data/tmp.pdf';
    $pdf->Output($file_pdf);
    J'ai deux soucis:
    1 les entêtes de colonnes ne sont pas tous affichés et je n'ai pas autant de colonnes que d'entêtes.
    2 sous quelle forme doivent être les informations dans le .txt? J'utilise \t et \n et j'obtiens n'importe quoi dans le pdf....

    Merci d'avance pour votre aide...

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    Petite précision: à la base mes résultats sont sous la forme d'un tableau html... Si je pouvais me passer du fichier .txt comme intermédiaire çà m'arrangerai

  3. #3
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    j'ai modifié ta class pdf et ajouter une fonction mysql2pdf

    cela evite de passer par le fichier txt
    juste une probleme il manque la premiere ligne quand on choisit d'afficher les entetes (header = true)
    je me suis inspiré d'une fonction mysql2csv d'ou les commentaires en anglais

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    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('\n',chop($line));
        return $data;
    }
    function Mysql2pdf($bd,$requete)
    {
     
    	//   MySQL2CSV util
    	//   like every other '2' util, converts some data to other data.
    	//   In this case, we're taking a query and building a pdf export.
     
    	// Configuration
    	// CSV stuff
    	// $outfile = "export.csv";
    	$header = true;                //list headers seperated by $seperator at head of file
    	$listnull = true;               //instead of printing "" print "NULL"
     
     
    	// Execute query
    	$result = $bd->ExecRequete($requete); // Classe qui execute la requete
     
    	//print headers, if we need to
    	if ($header)
    	{
    		$row = mysql_fetch_assoc($result); //gives i-index and named indices
    		$header_string = "";
    		$string = "";
    		$fill = 0;
    		//Grab everything
    		foreach ($row as $key => $value)
    		{
    			$header_string = $key; //why is there no str_cat function...
    			$this->Cell(5,45,$header_string,'LR',0,'L',$fill);
    		}
    		$this->Ln();
           	$fill=!$fill;
    	}
     
    	//Build CSV
    	while ($row = mysql_fetch_row($result))
    	{
    		$string = "";
    		$fill = 0;
    		//Fetch all rows till the last one
    		for ($i = 0; $i < (count($row)-1); $i++)
    		{
    			if ($row[$i] == NULL && $listnull) $row[$i] = "NULL";
    			$string = $row[$i];
    			$this->Cell(5,45,$string,'LR',0,'L',$fill);
    		}
    		$this->Ln();
           	$fill=!$fill;
    	}
    }
    }

  4. #4
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    voila c corrigé :
    nécéssite bien sur la classe fdpf
    voila le code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    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('\n',chop($line));
        return $data;
    }
    function Mysql2pdf($bd,$requete)
    {
     
    	//   MySQL2CSV util
    	//   like every other '2' util, converts some data to other data.
    	//   In this case, we're taking a query and building a pdf export.
    	// Configuration
    	$header = true;                //list headers seperated by $seperator at head of file
    	$listnull = true;               //instead of printing "" print "NULL"
    	$width = 50;
    	$height = 5;
     
    	// Execute query
    	$result = $bd->ExecRequete($requete); // Classe qui execute la requete
     
    	//print headers, if we need to
    	if ($header)
    	{
    		$row = mysql_fetch_assoc($result); 
    		$header_string = "";
    		$string = "";
     
    		//Grab everything
    		foreach ($row as $key => $value)
    		{
    			$header_string[] = $key;
    			$string[] = $value;
    		}
    		$fill = 0;
    		foreach ($header_string as $value) {$this->Cell($width,$height,$value,'LR',0,'L',$fill);}
    		$this->Ln();
           	$fill=!$fill;
           	$fill = 0;
    		foreach ($string as $value) {$this->Cell($width,$height,$value,'LR',0,'L',$fill);}
    		$this->Ln();
           	$fill=!$fill;
    	}
     
    	//Build PDF
    	while ($row = mysql_fetch_row($result))
    	{
    		$string = "";
    		$fill = 0;
    		//Fetch all rows till the last one
    		for ($i = 0; $i < (count($row)-1); $i++)
    		{
    			if ($row[$i] == NULL && $listnull) $row[$i] = "NULL";
    			$string = $row[$i];
    			$this->Cell($width,$height,$string,'LR',0,'L',$fill);
    		}
    		$this->Ln();
           	$fill=!$fill;
    	}
    }



    et l'appel :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    /////PDF creation///////
    require_once (".//Pdf//pdf.class.php");
    // Connexion à la base de données
    $bd = new MySQL (NOM, PASSE, BASE, SERVEUR); // Classe de connexion à MySQL : $bd pointe sur @mysql_select_db (BASE, @mysql_pconnect SERVEUR, PASSE, PASSE))
    $Requete = 'SELECT * FROM `MaTable`';
    $pdf=new PDF();
    //Chargement des données
    $pdf->SetFont('Arial','',10);
    $pdf->AddPage();
    $pdf->Mysql2pdf($bd,$Requete);
    $file_pdf='.//FichiersPdf//FichierPdf.pdf';
    $pdf->Output($file_pdf);

  5. #5
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    Salut cedrelo22,
    Vu que je n'avais pas de réponse je ne surveillais même plus ce sujet... Et par hasard je viens de le visualiser de nouveau.
    J'ai donc eu une bonne surprise en voyant que qqun si était enfin intéressé
    J'étais parti sur une autre solution qui ne donne pas de super résultats, en utilisant htmldoc-1.9.x-r1521...

    Je vais donc de ce pas utiliser les scripts que tu me proposes
    Merci!

  6. #6
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    voici la classe bien améliorer :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    <?php
    /*******************************************************************************
    * Software: class PDF extends FPDF                                             *
    * Version:  1	                                                               *
    * Date:     2006-12-06                                                         *
    * Author:   Cédric Loïodice                                                    *
    * License:  Freeware                                                           *
    * You may use, modify and redistribute this software as you wish.              *
    * Role :                                                                       *
    * Cette classe permet d'exporter des tables de MySQL en pdf avec un            *
    * dimmensionnement des colonnes et des tailles de caractères automatique       *
    * afin de tenir sur une largeur de page.  									   *							
    *******************************************************************************/
     
    class PDF extends FPDF
    {
    var $width;
    var $height;
    //Connection à une base de données mysql
    function connect($host='localhost',$username='',$password='',$db=''){
        $this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
        mysql_select_db($db,$this->conn) or die( mysql_error() );
        return true;
    }
    //Execute une requete sql
    function query($query){
        $this->results = mysql_query($query,$this->conn);
        $this->numFields = mysql_num_fields($this->results);
    }
    //Regle la largeur d'une cellule
    function SetWidth($width)
    {
    	$this->width = $width;
    }
    //Regle la largeur d'une cellule en fonction du nombre de cellule
    function AutoWidth($numFields)
    {
    	// starting col width
    	$this->width = (($this->w-$this->lMargin-$this->rMargin))/$numFields;
    }
    //regle la hauteur d'une cellule
    function SetHeight($height)
    {
    	$this->height = $height;
    }
    //Ajuste la taille de la police en fonction de la cellule et ecrit verticalement
    function VCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0)
    {
    	//Output a cell
    	$k=$this->k;
    	if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
    	{
    		$x=$this->x;
    		$ws=$this->ws;
    		if($ws>0)
    		{
    			$this->ws=0;
    			$this->_out('0 Tw');
    		}
    		$this->AddPage($this->CurOrientation);
    		$this->x=$x;
    		if($ws>0)
    		{
    			$this->ws=$ws;
    			$this->_out(sprintf('%.3f Tw',$ws*$k));
    		}
    	}
    	if($w==0)
    		$w=$this->w-$this->rMargin-$this->x;
    	$s='';
    // begin change Cell function 
    	if($fill==1 or $border>0)
    	{
    		if($fill==1)
    			$op=($border>0) ? 'B' : 'f';
    		else
    			$op='S';
    		if ($border>1) {
    			$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
    						$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    		}
    		else
    			$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    	}
    	if(is_string($border))
    	{
    		$x=$this->x;
    		$y=$this->y;
    		if(is_int(strpos($border,'L')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
    		else if(is_int(strpos($border,'l')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
     
    		if(is_int(strpos($border,'T')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
    		else if(is_int(strpos($border,'t')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
     
    		if(is_int(strpos($border,'R')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		else if(is_int(strpos($border,'r')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
     
    		if(is_int(strpos($border,'B')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		else if(is_int(strpos($border,'b')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    	}
    	if(trim($txt)!='')
    	{
    		$cr=substr_count($txt,"\n");
    		if ($cr>0) { // Multi line
    			$txts = explode("\n", $txt);
    			$lines = count($txts);
    			for($l=0;$l<$lines;$l++) {
    				$txt=$txts[$l];
    				$w_txt=$this->GetStringWidth($txt);
    				if ($align=='U')
    					$dy=$this->cMargin+$w_txt;
    				elseif($align=='D')
    					$dy=$h-$this->cMargin;
    				else
    					$dy=($h+$w_txt)/2;
    				$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    				if($this->ColorFlag)
    					$s.='q '.$this->TextColor.' ';
    				$s.=sprintf('BT 0 1 -1 0 %.2f %.2f Tm (%s) Tj ET ',
    					($this->x+.5*$w+(.7+$l-$lines/2)*$this->FontSize)*$k,
    					($this->h-($this->y+$dy))*$k,$txt);
    				if($this->ColorFlag)
    					$s.=' Q ';
    			}
    		}
    		else { // Single line
    			$w_txt=$this->GetStringWidth($txt);
    			$Tz=100;
    			if ($w_txt>$h-2*$this->cMargin) {
    				$Tz=($h-2*$this->cMargin)/$w_txt*100;
    				$w_txt=$h-2*$this->cMargin;
    			}
    			if ($align=='U')
    				$dy=$this->cMargin+$w_txt;
    			elseif($align=='D')
    				$dy=$h-$this->cMargin;
    			else
    				$dy=($h+$w_txt)/2;
    			$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    			if($this->ColorFlag)
    				$s.='q '.$this->TextColor.' ';
    			$s.=sprintf('q BT 0 1 -1 0 %.2f %.2f Tm %.2f Tz (%s) Tj ET Q ',
    						($this->x+.5*$w+.3*$this->FontSize)*$k,
    						($this->h-($this->y+$dy))*$k,$Tz,$txt);
    			if($this->ColorFlag)
    				$s.=' Q ';
    		}
    	}
    // end change Cell function 
    	if($s)
    		$this->_out($s);
    	$this->lasth=$h;
    	if($ln>0)
    	{
    		//Go to next line
    		$this->y+=$h;
    		if($ln==1)
    			$this->x=$this->lMargin;
    	}
    	else
    		$this->x+=$w;
    }
    //Ajuste la taille de la police en fonction de la cellule et ecrit horizontalement
    function HCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
    {
    	//Output a cell
    	$k=$this->k;
    	if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
    	{
    		$x=$this->x;
    		$ws=$this->ws;
    		if($ws>0)
    		{
    			$this->ws=0;
    			$this->_out('0 Tw');
    		}
    		$this->AddPage($this->CurOrientation);
    		$this->x=$x;
    		if($ws>0)
    		{
    			$this->ws=$ws;
    			$this->_out(sprintf('%.3f Tw',$ws*$k));
    		}
    	}
    	if($w==0)
    		$w=$this->w-$this->rMargin-$this->x;
    	$s='';
    // begin change Cell function 12.08.2003 
    	if($fill==1 or $border>0)
    	{
    		if($fill==1)
    			$op=($border>0) ? 'B' : 'f';
    		else
    			$op='S';
    		if ($border>1) {
    			$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
    				$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    		}
    		else
    			$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    	}
    	if(is_string($border))
    	{
    		$x=$this->x;
    		$y=$this->y;
    		if(is_int(strpos($border,'L')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
    		else if(is_int(strpos($border,'l')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
     
    		if(is_int(strpos($border,'T')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
    		else if(is_int(strpos($border,'t')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
     
    		if(is_int(strpos($border,'R')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		else if(is_int(strpos($border,'r')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
     
    		if(is_int(strpos($border,'B')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		else if(is_int(strpos($border,'b')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    	}
    	if (trim($txt)!='') {
    		$cr=substr_count($txt,"\n");
    		if ($cr>0) { // Multi line
    			$txts = explode("\n", $txt);
    			$lines = count($txts);
    			//$dy=($h-2*$this->cMargin)/$lines;
    			for($l=0;$l<$lines;$l++) {
    				$txt=$txts[$l];
    				$w_txt=$this->GetStringWidth($txt);
    				if($align=='R')
    					$dx=$w-$w_txt-$this->cMargin;
    				elseif($align=='C')
    					$dx=($w-$w_txt)/2;
    				else
    					$dx=$this->cMargin;
     
    				$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    				if($this->ColorFlag)
    					$s.='q '.$this->TextColor.' ';
    				$s.=sprintf('BT %.2f %.2f Td (%s) Tj ET ',
    					($this->x+$dx)*$k,
    					($this->h-($this->y+.5*$h+(.7+$l-$lines/2)*$this->FontSize))*$k,
    					$txt);
    				if($this->underline)
    					$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
    				if($this->ColorFlag)
    					$s.=' Q ';
    				if($link)
    					$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
    			}
    		}
    		else { // Single line
    			$w_txt=$this->GetStringWidth($txt);
    			$Tz=100;
    			if ($w_txt>$w-2*$this->cMargin) { // Need compression
    				$Tz=($w-2*$this->cMargin)/$w_txt*100;
    				$w_txt=$w-2*$this->cMargin;
    			}
    			if($align=='R')
    				$dx=$w-$w_txt-$this->cMargin;
    			elseif($align=='C')
    				$dx=($w-$w_txt)/2;
    			else
    				$dx=$this->cMargin;
    			$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    			if($this->ColorFlag)
    				$s.='q '.$this->TextColor.' ';
    			$s.=sprintf('q BT %.2f %.2f Td %.2f Tz (%s) Tj ET Q ',
    						($this->x+$dx)*$k,
    						($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,
    						$Tz,$txt);
    			if($this->underline)
    				$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
    			if($this->ColorFlag)
    				$s.=' Q ';
    			if($link)
    				$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
    		}
    	}
    // end change Cell function 12.08.2003
    	if($s)
    		$this->_out($s);
    	$this->lasth=$h;
    	if($ln>0)
    	{
    		//Go to next line
    		$this->y+=$h;
    		if($ln==1)
    			$this->x=$this->lMargin;
    	}
    	else
    		$this->x+=$w;
    }
    //Ajoute des retours à la ligne si le texte est trop long
    function WordWrap(&$text, $maxwidth)
    {
        $text = trim($text);
        if ($text==='')
            return 0;
        $space = $this->GetStringWidth(' ');
        $lines = explode("\n", $text);
        $text = '';
        $count = 0;
     
        foreach ($lines as $line)
        {
            $words = preg_split('/ +/', $line);
            $width = 0;
     
            foreach ($words as $word)
            {
                $wordwidth = $this->GetStringWidth($word);
                if ($wordwidth > $maxwidth)
                {
                    // Word is too long, we cut it
                    for($i=0; $i<strlen($word); $i++)
                    {
                        $wordwidth = $this->GetStringWidth(substr($word, $i, 1));
                        if($width + $wordwidth <= $maxwidth)
                        {
                            $width += $wordwidth;
                            $text .= substr($word, $i, 1);
                        }
                        else
                        {
                            $width = $wordwidth;
                            $text = rtrim($text)."\n".substr($word, $i, 1);
                            $count++;
                        }
                    }
                }
                elseif($width + $wordwidth <= $maxwidth)
                {
                    $width += $wordwidth + $space;
                    $text .= $word.' ';
                }
                else
                {
                    $width = $wordwidth + $space;
                    $text = rtrim($text)."\n".$word.' ';
                    $count++;
                }
            }
            $text = rtrim($text)."\n";
            $count++;
        }
        $text = rtrim($text);
        return $count;
    }
    //Ajoute un chagement de page si necessaire
    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);
    }
    //Transforme la requete sql en pdf
    function Mysql2pdf($requete)
    {
    	// Configuration
    	$header = true;                
    	$listnull = true;               //instead of printing "" print "NULL"
    	$NbHeightMax = 0;
    	// Execute query
    	$this->query($requete);
    	$this->AutoWidth($this->numFields);
    	//print headers, if we need to
    	if ($header)
    	{
       		$fill = 0;
       		for ($i = 0; $i < ($this->numFields); $i++)
       		{
       			$colTitles = mysql_field_name($this->results,$i);
       			//Sauve la position courante
    			$x=$this->GetX();
    			$y=$this->GetY();
    			$this->VCell($this->width,$this->height*2,$colTitles,'LR',0,'L',$fill);
    			//Dessine le cadre
           		$this->Rect($x,$y,$this->width,$this->height*2);
       		}
       		$this->Ln();
           	$fill=!$fill;
    	}
     
    	//Build PDF
    	while ($row = mysql_fetch_row($this->results))
    	{
    		$string = "";
    		$fill = 0;
    		 //Effectue un saut de page si nécessaire
       		 $this->CheckPageBreak($this->height);
    		//Fetch all rows till the last one
    		for ($i = 0; $i < ($this->numFields); $i++)
    		{
    			if ($row[$i] == NULL && $listnull) $row[$i] = "NULL";
    			$string = $row[$i];
    			//Sauve la position courante
    			$x=$this->GetX();
    			$y=$this->GetY();
    			//$NbHeight = $this->WordWrap($string,$width);
    			//if ($NbHeightMax<$NbHeight) $NbHeightMax = $NbHeight;
    			$this->HCell($this->width,$this->height,$string,'LR',0,'L',$fill);
    			//Dessine le cadre
            	$this->Rect($x,$y,$this->width,$this->height);
    		}
    		$this->Ln();
           	$fill=!$fill;
    	}
    }
    }
     
     
    /////PDF creation///////
    $pdf = new PDF('L','pt','A3');
    $pdf->SetFont('Arial','',8);
    $pdf->AddPage();
    // Connexion à la base de données
    $pdf->connect(SERVEUR,NOM,PASSE,BASE);
    //$pdf->SetWidth (25); // We use Autowidth
    $pdf->SetHeight(20);
    $pdf->Mysql2Pdf("SELECT * FROM MyTable");
    $pdf->Output('.//FichiersPdf//FichierPdf.pdf');
    ?>

  7. #7
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    avec les entetes grisés et le numero et nombre de pages :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    448
    449
    450
    451
    <?php
    /*******************************************************************************
    * Software: class PDF extends FPDF                                             *
    * Version:  1	                                                               *
    * Date:     2006-12-06                                                         *
    * Author:   Cédric Loïodice                                                    *
    * License:  Freeware                                                           *
    * You may use, modify and redistribute this software as you wish.              *
    * Role :                                                                       *
    * Cette classe permet d'exporter des tables de MySQL en pdf avec un            *
    * dimmensionnement des colonnes et des tailles de caractères automatique       *
    * afin de tenir sur une largeur de page.  									   *							
    *******************************************************************************/
    class PDF extends FPDF
    {
    	var $width;
    	var $height;
    	//Connection à une base de données mysql
    	function connect($host='localhost',$username='',$password='',$db=''){
    		$this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
    		mysql_select_db($db,$this->conn) or die( mysql_error() );
    		return true;
    	}
    	//Execute une requete sql
    	function query($query){
    		$this->results = mysql_query($query,$this->conn);
    		$this->numFields = mysql_num_fields($this->results);
    	}
    	//Regle la largeur d'une cellule
    	function SetWidth($width)
    	{
    		$this->width = $width;
    	}
    	//Regle la largeur d'une cellule en fonction du nombre de cellule
    	function AutoWidth($numFields)
    	{
    		// starting col width
    		$this->width = (($this->w-$this->lMargin-$this->rMargin))/$numFields;
    	}
    	//regle la hauteur d'une cellule
    	function SetHeight($height)
    	{
    		$this->height = $height;
    	}
    	//Ajuste la taille de la police en fonction de la cellule et ecrit verticalement
    	function VCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0)
    	{
    		//Output a cell
    		$k=$this->k;
    		if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
    		{
    			$x=$this->x;
    			$ws=$this->ws;
    			if($ws>0)
    			{
    				$this->ws=0;
    				$this->_out('0 Tw');
    			}
    			$this->AddPage($this->CurOrientation);
    			$this->x=$x;
    			if($ws>0)
    			{
    				$this->ws=$ws;
    				$this->_out(sprintf('%.3f Tw',$ws*$k));
    			}
    		}
    		if($w==0)
    		$w=$this->w-$this->rMargin-$this->x;
    		$s='';
    		// begin change Cell function
    		if($fill==1 or $border>0)
    		{
    			if($fill==1)
    			$op=($border>0) ? 'B' : 'f';
    			else
    			$op='S';
    			if ($border>1) {
    				$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
    				$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    			}
    			else
    			$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    		}
    		if(is_string($border))
    		{
    			$x=$this->x;
    			$y=$this->y;
    			if(is_int(strpos($border,'L')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'l')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'T')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
    			else if(is_int(strpos($border,'t')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
     
    			if(is_int(strpos($border,'R')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'r')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'B')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'b')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		}
    		if(trim($txt)!='')
    		{
    			$cr=substr_count($txt,"\n");
    			if ($cr>0) { // Multi line
    				$txts = explode("\n", $txt);
    				$lines = count($txts);
    				for($l=0;$l<$lines;$l++) {
    					$txt=$txts[$l];
    					$w_txt=$this->GetStringWidth($txt);
    					if ($align=='U')
    					$dy=$this->cMargin+$w_txt;
    					elseif($align=='D')
    					$dy=$h-$this->cMargin;
    					else
    					$dy=($h+$w_txt)/2;
    					$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    					if($this->ColorFlag)
    					$s.='q '.$this->TextColor.' ';
    					$s.=sprintf('BT 0 1 -1 0 %.2f %.2f Tm (%s) Tj ET ',
    					($this->x+.5*$w+(.7+$l-$lines/2)*$this->FontSize)*$k,
    					($this->h-($this->y+$dy))*$k,$txt);
    					if($this->ColorFlag)
    					$s.=' Q ';
    				}
    			}
    			else { // Single line
    				$w_txt=$this->GetStringWidth($txt);
    				$Tz=100;
    				if ($w_txt>$h-2*$this->cMargin) {
    					$Tz=($h-2*$this->cMargin)/$w_txt*100;
    					$w_txt=$h-2*$this->cMargin;
    				}
    				if ($align=='U')
    				$dy=$this->cMargin+$w_txt;
    				elseif($align=='D')
    				$dy=$h-$this->cMargin;
    				else
    				$dy=($h+$w_txt)/2;
    				$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    				if($this->ColorFlag)
    				$s.='q '.$this->TextColor.' ';
    				$s.=sprintf('q BT 0 1 -1 0 %.2f %.2f Tm %.2f Tz (%s) Tj ET Q ',
    				($this->x+.5*$w+.3*$this->FontSize)*$k,
    				($this->h-($this->y+$dy))*$k,$Tz,$txt);
    				if($this->ColorFlag)
    				$s.=' Q ';
    			}
    		}
    		// end change Cell function
    		if($s)
    		$this->_out($s);
    		$this->lasth=$h;
    		if($ln>0)
    		{
    			//Go to next line
    			$this->y+=$h;
    			if($ln==1)
    			$this->x=$this->lMargin;
    		}
    		else
    		$this->x+=$w;
    	}
    	//Ajuste la taille de la police en fonction de la cellule et ecrit horizontalement
    	function HCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
    	{
    		//Output a cell
    		$k=$this->k;
    		if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
    		{
    			$x=$this->x;
    			$ws=$this->ws;
    			if($ws>0)
    			{
    				$this->ws=0;
    				$this->_out('0 Tw');
    			}
    			$this->AddPage($this->CurOrientation);
    			$this->x=$x;
    			if($ws>0)
    			{
    				$this->ws=$ws;
    				$this->_out(sprintf('%.3f Tw',$ws*$k));
    			}
    		}
    		if($w==0)
    		$w=$this->w-$this->rMargin-$this->x;
    		$s='';
    		// begin change Cell function 12.08.2003
    		if($fill==1 or $border>0)
    		{
    			if($fill==1)
    			$op=($border>0) ? 'B' : 'f';
    			else
    			$op='S';
    			if ($border>1) {
    				$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
    				$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    			}
    			else
    			$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    		}
    		if(is_string($border))
    		{
    			$x=$this->x;
    			$y=$this->y;
    			if(is_int(strpos($border,'L')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'l')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'T')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
    			else if(is_int(strpos($border,'t')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
     
    			if(is_int(strpos($border,'R')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'r')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'B')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'b')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		}
    		if (trim($txt)!='') {
    			$cr=substr_count($txt,"\n");
    			if ($cr>0) { // Multi line
    				$txts = explode("\n", $txt);
    				$lines = count($txts);
    				//$dy=($h-2*$this->cMargin)/$lines;
    				for($l=0;$l<$lines;$l++) {
    					$txt=$txts[$l];
    					$w_txt=$this->GetStringWidth($txt);
    					if($align=='R')
    					$dx=$w-$w_txt-$this->cMargin;
    					elseif($align=='C')
    					$dx=($w-$w_txt)/2;
    					else
    					$dx=$this->cMargin;
     
    					$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    					if($this->ColorFlag)
    					$s.='q '.$this->TextColor.' ';
    					$s.=sprintf('BT %.2f %.2f Td (%s) Tj ET ',
    					($this->x+$dx)*$k,
    					($this->h-($this->y+.5*$h+(.7+$l-$lines/2)*$this->FontSize))*$k,
    					$txt);
    					if($this->underline)
    					$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
    					if($this->ColorFlag)
    					$s.=' Q ';
    					if($link)
    					$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
    				}
    			}
    			else { // Single line
    				$w_txt=$this->GetStringWidth($txt);
    				$Tz=100;
    				if ($w_txt>$w-2*$this->cMargin) { // Need compression
    					$Tz=($w-2*$this->cMargin)/$w_txt*100;
    					$w_txt=$w-2*$this->cMargin;
    				}
    				if($align=='R')
    				$dx=$w-$w_txt-$this->cMargin;
    				elseif($align=='C')
    				$dx=($w-$w_txt)/2;
    				else
    				$dx=$this->cMargin;
    				$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    				if($this->ColorFlag)
    				$s.='q '.$this->TextColor.' ';
    				$s.=sprintf('q BT %.2f %.2f Td %.2f Tz (%s) Tj ET Q ',
    				($this->x+$dx)*$k,
    				($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,
    				$Tz,$txt);
    				if($this->underline)
    				$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
    				if($this->ColorFlag)
    				$s.=' Q ';
    				if($link)
    				$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
    			}
    		}
    		// end change Cell function 12.08.2003
    		if($s)
    		$this->_out($s);
    		$this->lasth=$h;
    		if($ln>0)
    		{
    			//Go to next line
    			$this->y+=$h;
    			if($ln==1)
    			$this->x=$this->lMargin;
    		}
    		else
    		$this->x+=$w;
    	}
    	//Ajoute des retours à la ligne si le texte est trop long
    	function WordWrap(&$text, $maxwidth)
    	{
    		$text = trim($text);
    		if ($text==='')
    		return 0;
    		$space = $this->GetStringWidth(' ');
    		$lines = explode("\n", $text);
    		$text = '';
    		$count = 0;
     
    		foreach ($lines as $line)
    		{
    			$words = preg_split('/ +/', $line);
    			$width = 0;
     
    			foreach ($words as $word)
    			{
    				$wordwidth = $this->GetStringWidth($word);
    				if ($wordwidth > $maxwidth)
    				{
    					// Word is too long, we cut it
    					for($i=0; $i<strlen($word); $i++)
    					{
    						$wordwidth = $this->GetStringWidth(substr($word, $i, 1));
    						if($width + $wordwidth <= $maxwidth)
    						{
    							$width += $wordwidth;
    							$text .= substr($word, $i, 1);
    						}
    						else
    						{
    							$width = $wordwidth;
    							$text = rtrim($text)."\n".substr($word, $i, 1);
    							$count++;
    						}
    					}
    				}
    				elseif($width + $wordwidth <= $maxwidth)
    				{
    					$width += $wordwidth + $space;
    					$text .= $word.' ';
    				}
    				else
    				{
    					$width = $wordwidth + $space;
    					$text = rtrim($text)."\n".$word.' ';
    					$count++;
    				}
    			}
    			$text = rtrim($text)."\n";
    			$count++;
    		}
    		$text = rtrim($text);
    		return $count;
    	}
    	//Ajoute un chagement de page si necessaire
    	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);
    			return true;
    		}
    		else return false;
    	}
    	function Footer() {
    		// Check if footer for this page already exists
    		$this->SetY(-15);
    		//Page number
    		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    	}
    	//Transforme une requete sql en pdf
    	function Mysql2pdf($requete)
    	{
     
    		//   MySQL2CSV util
    		//   like every other '2' util, converts some data to other data.
    		//   In this case, we're taking a query and building a pdf export.
    		// Configuration
    		$header = true; // If we want header
    		$headerfirstpage = true;   // The first header
    		$listnull = true;
    		$NbHeightMax = 0;
    		// Execute query
    		$this->query($requete);
    		$this->AutoWidth($this->numFields);
    		//Build PDF
    		while ($row = mysql_fetch_row($this->results))
    		{
    			$string = "";
    			$fill = 0;
    			//print headers, if we need to
    			if (($headerfirstpage)and ($header))
    			{
    				$fill = 1;
    				for ($i = 0; $i < ($this->numFields); $i++)
    				{	
    				$this->setFillColor(200,200,200);
    				$colTitles = mysql_field_name($this->results,$i);
    				//Sauve la position courante
    				$x=$this->GetX();
    				$y=$this->GetY();
    				$this->VCell($this->width,$this->height*2,$colTitles,'LR',0,'L',$fill);
    				//Dessine le cadre
    				$this->Rect($x,$y,$this->width,$this->height*2);
    				}
    				$this->Ln();
    				$fill=!$fill;
    			}
     
    			//Fetch all rows till the last one
    			for ($i = 0; $i < ($this->numFields); $i++)
    			{
    				if ($row[$i] == NULL && $listnull) $row[$i] = "NULL";
    				$string = $row[$i];
    				//Sauve la position courante
    				$x=$this->GetX();
    				$y=$this->GetY();
    				//$NbHeight = $this->WordWrap($string,$width);
    				//if ($NbHeightMax<$NbHeight) $NbHeightMax = $NbHeight;
    				$this->HCell($this->width,$this->height,$string,'LR',0,'L',$fill);
    				//Dessine le cadre
    				$this->Rect($x,$y,$this->width,$this->height);
    			}
    			$this->Ln();
    			$fill=!$fill;
    			//Effectue un saut de page si nécessaire
    			$headerfirstpage = $this->CheckPageBreak($this->height);
    		}
    	}
    }
     
    /////PDF creation///////
    $pdf = new PDF('L','pt','A3');
    $pdf->SetFont('Arial','',8);
    $pdf->AliasNbPages();
    $pdf->AddPage();
    // Connexion à la base de données
    $pdf->connect(SERVEUR,NOM,PASSE,BASE);
    //$pdf->SetWidth (25); // We use Autowidth
    $pdf->SetHeight(20);
    $pdf->Mysql2Pdf("SELECT * FROM MyTable");
    $pdf->Output('.//FichiersPdf//FichierPdf.pdf');
    ?>

  8. #8
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    Ok çà marche mais si il n'y a pas assez de place dans la cellule, le texte est écrit sur le texte écrit auparavant dans la cellule... Ce qui est illisible...
    comment régler çà ?

  9. #9
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    Je n'ai pas ce problème ??
    la fonction HCell permet justement de diminuer la taille de la police si le texte dépasse par contre si il ya trop de texte, l'affichage devient trop petit

    Au départ j'avais utilisé la fonction wordwrap qui ajoute des retours à la ligne si le texte est trop grand mais les résulttats pour moi n'étaiant pas satisfaisant par rapport à ceux obtenu avec Hcell
    voila le code de la classe que j'ai encore léérement modifié :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    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
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    387
    388
    389
    390
    391
    392
    393
    394
    395
    396
    397
    398
    399
    400
    401
    402
    403
    404
    405
    406
    407
    408
    409
    410
    411
    412
    413
    414
    415
    416
    417
    418
    419
    420
    421
    422
    423
    424
    425
    426
    427
    428
    429
    430
    431
    432
    433
    434
    435
    436
    437
    438
    439
    440
    441
    442
    443
    444
    445
    446
    447
    <?php
    /*******************************************************************************
    * Software: class PDF extends FPDF                                             *
    * Version:  1	                                                               *
    * Date:     2006-12-06                                                         *
    * Author:   Cédric Loïodice                                                    *
    * License:  Freeware                                                           *
    * You may use, modify and redistribute this software as you wish.              *
    * Role :                                                                       *
    * Cette classe permet d'exporter des tables de MySQL en pdf avec un            *
    * dimmensionnement des colonnes et des tailles de caractères automatique       *
    * afin de tenir sur une largeur de page.  									   *
    *******************************************************************************/
     
     
    class PDF extends FPDF
    {
    	var $width;
    	var $height;
    	//Connection à une base de données mysql
    	function connect($host='localhost',$username='',$password='',$db=''){
    		$this->conn = mysql_connect($host,$username,$password) or die( mysql_error() );
    		mysql_select_db($db,$this->conn) or die( mysql_error() );
    		return true;
    	}
    	//Execute une requete sql
    	function query($query){
    		$this->results = mysql_query($query,$this->conn);
    		$this->numFields = mysql_num_fields($this->results);
    	}
    	//Regle la largeur d'une cellule
    	function SetWidth($width)
    	{
    		$this->width = $width;
    	}
    	//Regle la largeur d'une cellule en fonction du nombre de cellule
    	function AutoWidth($numFields)
    	{
    		// starting col width
    		$this->width = (($this->w-$this->lMargin-$this->rMargin))/$numFields;
    	}
    	//regle la hauteur d'une cellule
    	function SetHeight($height)
    	{
    		$this->height = $height;
    	}
    	//Ajuste la taille de la police en fonction de la cellule et ecrit verticalement
    	function VCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0)
    	{
    		//Output a cell
    		$k=$this->k;
    		if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
    		{
    			$x=$this->x;
    			$ws=$this->ws;
    			if($ws>0)
    			{
    				$this->ws=0;
    				$this->_out('0 Tw');
    			}
    			$this->AddPage($this->CurOrientation);
    			$this->x=$x;
    			if($ws>0)
    			{
    				$this->ws=$ws;
    				$this->_out(sprintf('%.3f Tw',$ws*$k));
    			}
    		}
    		if($w==0)
    		$w=$this->w-$this->rMargin-$this->x;
    		$s='';
    		// begin change Cell function
    		if($fill==1 or $border>0)
    		{
    			if($fill==1)
    			$op=($border>0) ? 'B' : 'f';
    			else
    			$op='S';
    			if ($border>1) {
    				$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
    				$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    			}
    			else
    			$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    		}
    		if(is_string($border))
    		{
    			$x=$this->x;
    			$y=$this->y;
    			if(is_int(strpos($border,'L')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'l')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'T')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
    			else if(is_int(strpos($border,'t')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
     
    			if(is_int(strpos($border,'R')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'r')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'B')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'b')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		}
    		if(trim($txt)!='')
    		{
    			$cr=substr_count($txt,"\n");
    			if ($cr>0) { // Multi line
    				$txts = explode("\n", $txt);
    				$lines = count($txts);
    				for($l=0;$l<$lines;$l++) {
    					$txt=$txts[$l];
    					$w_txt=$this->GetStringWidth($txt);
    					if ($align=='U')
    					$dy=$this->cMargin+$w_txt;
    					elseif($align=='D')
    					$dy=$h-$this->cMargin;
    					else
    					$dy=($h+$w_txt)/2;
    					$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    					if($this->ColorFlag)
    					$s.='q '.$this->TextColor.' ';
    					$s.=sprintf('BT 0 1 -1 0 %.2f %.2f Tm (%s) Tj ET ',
    					($this->x+.5*$w+(.7+$l-$lines/2)*$this->FontSize)*$k,
    					($this->h-($this->y+$dy))*$k,$txt);
    					if($this->ColorFlag)
    					$s.=' Q ';
    				}
    			}
    			else { // Single line
    				$w_txt=$this->GetStringWidth($txt);
    				$Tz=100;
    				if ($w_txt>$h-2*$this->cMargin) {
    					$Tz=($h-2*$this->cMargin)/$w_txt*100;
    					$w_txt=$h-2*$this->cMargin;
    				}
    				if ($align=='U')
    				$dy=$this->cMargin+$w_txt;
    				elseif($align=='D')
    				$dy=$h-$this->cMargin;
    				else
    				$dy=($h+$w_txt)/2;
    				$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    				if($this->ColorFlag)
    				$s.='q '.$this->TextColor.' ';
    				$s.=sprintf('q BT 0 1 -1 0 %.2f %.2f Tm %.2f Tz (%s) Tj ET Q ',
    				($this->x+.5*$w+.3*$this->FontSize)*$k,
    				($this->h-($this->y+$dy))*$k,$Tz,$txt);
    				if($this->ColorFlag)
    				$s.=' Q ';
    			}
    		}
    		// end change Cell function
    		if($s)
    		$this->_out($s);
    		$this->lasth=$h;
    		if($ln>0)
    		{
    			//Go to next line
    			$this->y+=$h;
    			if($ln==1)
    			$this->x=$this->lMargin;
    		}
    		else
    		$this->x+=$w;
    	}
    	//Ajuste la taille de la police en fonction de la cellule et ecrit horizontalement
    	function HCell($w,$h=0,$txt='',$border=0,$ln=0,$align='',$fill=0,$link='')
    	{
    		//Output a cell
    		$k=$this->k;
    		if($this->y+$h>$this->PageBreakTrigger and !$this->InFooter and $this->AcceptPageBreak())
    		{
    			$x=$this->x;
    			$ws=$this->ws;
    			if($ws>0)
    			{
    				$this->ws=0;
    				$this->_out('0 Tw');
    			}
    			$this->AddPage($this->CurOrientation);
    			$this->x=$x;
    			if($ws>0)
    			{
    				$this->ws=$ws;
    				$this->_out(sprintf('%.3f Tw',$ws*$k));
    			}
    		}
    		if($w==0)
    		$w=$this->w-$this->rMargin-$this->x;
    		$s='';
    		// begin change Cell function 12.08.2003
    		if($fill==1 or $border>0)
    		{
    			if($fill==1)
    			$op=($border>0) ? 'B' : 'f';
    			else
    			$op='S';
    			if ($border>1) {
    				$s=sprintf(' q %.2f w %.2f %.2f %.2f %.2f re %s Q ',$border,
    				$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    			}
    			else
    			$s=sprintf('%.2f %.2f %.2f %.2f re %s ',$this->x*$k,($this->h-$this->y)*$k,$w*$k,-$h*$k,$op);
    		}
    		if(is_string($border))
    		{
    			$x=$this->x;
    			$y=$this->y;
    			if(is_int(strpos($border,'L')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'l')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,$x*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'T')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
    			else if(is_int(strpos($border,'t')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-$y)*$k);
     
    			if(is_int(strpos($border,'R')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'r')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',($x+$w)*$k,($this->h-$y)*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
     
    			if(is_int(strpos($border,'B')))
    			$s.=sprintf('%.2f %.2f m %.2f %.2f l S ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    			else if(is_int(strpos($border,'b')))
    			$s.=sprintf('q 2 w %.2f %.2f m %.2f %.2f l S Q ',$x*$k,($this->h-($y+$h))*$k,($x+$w)*$k,($this->h-($y+$h))*$k);
    		}
    		if (trim($txt)!='') {
    			$cr=substr_count($txt,"\n");
    			if ($cr>0) { // Multi line
    				$txts = explode("\n", $txt);
    				$lines = count($txts);
    				//$dy=($h-2*$this->cMargin)/$lines;
    				for($l=0;$l<$lines;$l++) {
    					$txt=$txts[$l];
    					$w_txt=$this->GetStringWidth($txt);
    					if($align=='R')
    					$dx=$w-$w_txt-$this->cMargin;
    					elseif($align=='C')
    					$dx=($w-$w_txt)/2;
    					else
    					$dx=$this->cMargin;
     
    					$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    					if($this->ColorFlag)
    					$s.='q '.$this->TextColor.' ';
    					$s.=sprintf('BT %.2f %.2f Td (%s) Tj ET ',
    					($this->x+$dx)*$k,
    					($this->h-($this->y+.5*$h+(.7+$l-$lines/2)*$this->FontSize))*$k,
    					$txt);
    					if($this->underline)
    					$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
    					if($this->ColorFlag)
    					$s.=' Q ';
    					if($link)
    					$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
    				}
    			}
    			else { // Single line
    				$w_txt=$this->GetStringWidth($txt);
    				$Tz=100;
    				if ($w_txt>$w-2*$this->cMargin) { // Need compression
    					$Tz=($w-2*$this->cMargin)/$w_txt*100;
    					$w_txt=$w-2*$this->cMargin;
    				}
    				if($align=='R')
    				$dx=$w-$w_txt-$this->cMargin;
    				elseif($align=='C')
    				$dx=($w-$w_txt)/2;
    				else
    				$dx=$this->cMargin;
    				$txt=str_replace(')','\\)',str_replace('(','\\(',str_replace('\\','\\\\',$txt)));
    				if($this->ColorFlag)
    				$s.='q '.$this->TextColor.' ';
    				$s.=sprintf('q BT %.2f %.2f Td %.2f Tz (%s) Tj ET Q ',
    				($this->x+$dx)*$k,
    				($this->h-($this->y+.5*$h+.3*$this->FontSize))*$k,
    				$Tz,$txt);
    				if($this->underline)
    				$s.=' '.$this->_dounderline($this->x+$dx,$this->y+.5*$h+.3*$this->FontSize,$txt);
    				if($this->ColorFlag)
    				$s.=' Q ';
    				if($link)
    				$this->Link($this->x+$dx,$this->y+.5*$h-.5*$this->FontSize,$w_txt,$this->FontSize,$link);
    			}
    		}
    		// end change Cell function 12.08.2003
    		if($s)
    		$this->_out($s);
    		$this->lasth=$h;
    		if($ln>0)
    		{
    			//Go to next line
    			$this->y+=$h;
    			if($ln==1)
    			$this->x=$this->lMargin;
    		}
    		else
    		$this->x+=$w;
    	}
    	//Ajoute des retours à la ligne si le texte est trop long
    	function WordWrap(&$text, $maxwidth)
    	{
    		$text = trim($text);
    		if ($text==='')
    		return 0;
    		$space = $this->GetStringWidth(' ');
    		$lines = explode("\n", $text);
    		$text = '';
    		$count = 0;
     
    		foreach ($lines as $line)
    		{
    			$words = preg_split('/ +/', $line);
    			$width = 0;
     
    			foreach ($words as $word)
    			{
    				$wordwidth = $this->GetStringWidth($word);
    				if ($wordwidth > $maxwidth)
    				{
    					// Word is too long, we cut it
    					for($i=0; $i<strlen($word); $i++)
    					{
    						$wordwidth = $this->GetStringWidth(substr($word, $i, 1));
    						if($width + $wordwidth <= $maxwidth)
    						{
    							$width += $wordwidth;
    							$text .= substr($word, $i, 1);
    						}
    						else
    						{
    							$width = $wordwidth;
    							$text = rtrim($text)."\n".substr($word, $i, 1);
    							$count++;
    						}
    					}
    				}
    				elseif($width + $wordwidth <= $maxwidth)
    				{
    					$width += $wordwidth + $space;
    					$text .= $word.' ';
    				}
    				else
    				{
    					$width = $wordwidth + $space;
    					$text = rtrim($text)."\n".$word.' ';
    					$count++;
    				}
    			}
    			$text = rtrim($text)."\n";
    			$count++;
    		}
    		$text = rtrim($text);
    		return $count;
    	}
    	//Ajoute un chagement de page si necessaire
    	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);
    			return true;
    		}
    		else return false;
    	}
    	function Footer() {
    		// footer for this page
    		$this->SetY(-15);
    		//Page number
    		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    	}
    	//Transforme une requete sql en pdf
    	function Mysql2pdf($requete)
    	{
    		//   like every other '2' util, converts some data to other data.
    		//   In this case, we're taking a query and building a pdf export.
    		// Configuration
    		$header = true; // If we want header
    		$listnull = true;
    		$fill = 1;
    		// Execute query
    		$this->query($requete);
    		$this->AutoWidth($this->numFields);
    		//Build PDF
    		while ($row = mysql_fetch_row($this->results))
    		{
    			$string = "";
    			//print headers, if we need to
    			if ($header)
    			{
    				for ($i = 0; $i < ($this->numFields); $i++)
    				{
    					$this->setFillColor(180,180,180);
    					$colTitles = mysql_field_name($this->results,$i);
    					//Sauve la position courante
    					$x=$this->GetX();
    					$y=$this->GetY();
    					$this->VCell($this->width,$this->height*2,$colTitles,'LR',0,'L',$fill);
    					//Dessine le cadre
    					$this->Rect($x,$y,$this->width,$this->height*2);
    				}
    				$fill=!$fill;
    				$this->Ln();
    			}
    			//Fetch all rows till the last one
    			for ($i = 0; $i < ($this->numFields); $i++)
    			{
    				$this->setFillColor(225,225,225);
    				if ($row[$i] == NULL && $listnull) $row[$i] = "NULL";
    				$string = $row[$i];
    				//Sauve la position courante
    				$x=$this->GetX();
    				$y=$this->GetY();
    				$this->HCell($this->width,$this->height,$string,'LR',0,'L',$fill);
    				//Dessine le cadre
    				$this->Rect($x,$y,$this->width,$this->height);
    			}
    			$fill=!$fill;
    			$this->Ln();
    			//Effectue un saut de page si nécessaire
    			$header = $this->CheckPageBreak($this->height) and ($header);
    		}
    	}
    }
     
     
    /////PDF creation///////
    $pdf = new PDF('L','pt','A3');
    $pdf->SetFont('Arial','',8);
    $pdf->AliasNbPages();
    $pdf->AddPage();
    // Connexion à la base de données
    $pdf->connect(SERVEUR,NOM,PASSE,BASE);
    //$pdf->SetWidth (25); // We use Autowidth
    $pdf->SetHeight(20);
    $pdf->Mysql2Pdf("SELECT * FROM MyTable");
    $pdf->Output('.//FichiersPdf//FichierPdf.pdf');
    ?>

  10. #10
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    voici un résulat obtenu :
    Images attachées Images attachées

  11. #11
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    test.pdf Voici un exemple de pdf que j'obtiens...
    Cela illustre ce que je disais à propos de l'illisibilité

  12. #12
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    Les polices de caractères deviennent trop petites avec autant de texte dans les cellules, il faut alors utiliser la fonction wordwrap qui ajoute des retours a la ligne au texte et augmenter la hauteur de la cellule.

    je vais essayer de tester cette solution

  13. #13
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    Oky je vais essayer aussi avec wordwrap

  14. #14
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    Avec worwrap le résultat est meilleur pour moi mais pas dans tous les cas...
    En fait, si le contenu des cellules est supérieur à la taille max de "height" c'est la cata ...
    ceci pose donc un problème d'automatisation pour pouvoir traiter tous les cas...

  15. #15
    Inactif  
    Inscrit en
    Août 2006
    Messages
    74
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 74
    Points : 123
    Points
    123
    Par défaut
    j'ai eu le meme probleme
    j'ai gardé la premiere solution sans wordwrap
    en effet je n'ai pas beaucoup de texte dans mes champs


  16. #16
    Membre du Club
    Profil pro
    Inscrit en
    Mai 2004
    Messages
    86
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2004
    Messages : 86
    Points : 54
    Points
    54
    Par défaut
    pour l'instant je n'ai pas de solution....

Discussions similaires

  1. [FPDF] problème avec les images dans un tableau dynamique
    Par GADSN dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 08/04/2009, 11h11
  2. [FPDF] \n dans un tableau avec FPDF
    Par voielactee38 dans le forum Bibliothèques et frameworks
    Réponses: 1
    Dernier message: 12/12/2008, 11h53
  3. [FPDF] Comment centrer le contenu des cellules d'un tableau généré avec FPDF ?
    Par hacksi dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 07/02/2008, 15h38
  4. [FPDF] Problème avec un while dans un tableau
    Par nigg4z dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 11/10/2007, 15h13
  5. [FPDF] PB avec fpdf sur serveur LINUX
    Par tissard dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 09/03/2007, 09h00

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo