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

PHP & Base de données Discussion :

Récupérer des données de MySQL et les envoyer dans un PDF


Sujet :

PHP & Base de données

  1. #1
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut Récupérer des données de MySQL et les envoyer dans un PDF
    Bonjour,

    J'ai un formulaire inscription.php qui envoi toutes les données dans ma db MySQL, maintenant je voudrais récupérer c'est donnée dans un fichier PDF ?

    Comment faire avez vous un solution ?

    merci à vous

  2. #2
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Regarde du côté de FPDF, par contre il s'agit de construire un PDF, pas d'utiliser un PDF existant.
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  3. #3
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut
    le problème c'est que c'est un fichier PDF que je devrais construire genre lettre type ou je devrais juste importer le nom, prenom, date ... de ma db MySQL ensuite il y aurait aussi le texte à placer ??

    Pouvez vous m'aider ?

    merci à vous

  4. #4
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Je n'ai pas compris votre question.
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  5. #5
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut
    sorry je vais m'expliquer :

    J'ai une lettre type et je voudrais importer dans cette lettre le nom, prenom, date de ma db mySQL dans un fichier PDF.

    J'ai voulu faire un petit test sur ma db et j'obtiens comme msg d'erreur ceci :
    " Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for MySQL result index 5 (or the query data is unbuffered) in /var/www/vhosts/test.be/httpdocs/db/mysql_report.php on line 292
    FPDF error: Some data has already been output, can't send PDF file
    "
    Alors qu'il fonctionne en local et que qu'en je le test sur mon site sa ne fonctionne plus ??

    voici le source :
    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
     
    <?php
    define(FPDF_FONTPATH,'font/');
    require('fpdf.php');
     
    class PDF extends FPDF {
     
    var $tablewidths;
    var $headerset;
    var $footerset;
     
    function _beginpage($orientation) {
    	$this->page++;
    	if(!$this->pages[$this->page]) // solved the problem of overwriting a page, if it already exists
    		$this->pages[$this->page]='';
    	$this->state=2;
    	$this->x=$this->lMargin;
    	$this->y=$this->tMargin;
    	$this->lasth=0;
    	$this->FontFamily='';
    	//Page orientation
    	if(!$orientation)
    		$orientation=$this->DefOrientation;
    	else
    	{
    		$orientation=strtoupper($orientation{0});
    		if($orientation!=$this->DefOrientation)
    			$this->OrientationChanges[$this->page]=true;
    	}
    	if($orientation!=$this->CurOrientation)
    	{
    		//Change orientation
    		if($orientation=='P')
    		{
    			$this->wPt=$this->fwPt;
    			$this->hPt=$this->fhPt;
    			$this->w=$this->fw;
    			$this->h=$this->fh;
    		}
    		else
    		{
    			$this->wPt=$this->fhPt;
    			$this->hPt=$this->fwPt;
    			$this->w=$this->fh;
    			$this->h=$this->fw;
    		}
    		$this->PageBreakTrigger=$this->h-$this->bMargin;
    		$this->CurOrientation=$orientation;
    	}
    }
     
    function Header()
    {
    	global $maxY;
     
    	// Check if header for this page already exists
    	if(!$this->headerset[$this->page]) {
     
    		foreach($this->tablewidths as $width) {
    			$fullwidth += $width;
    		}
    		$this->SetY(($this->tMargin) - ($this->FontSizePt/$this->k)*2);
    		$this->cellFontSize = $this->FontSizePt ;
    		$this->SetFont('Arial','',( ( $this->titleFontSize) ? $this->titleFontSize : $this->FontSizePt ));
    		$this->Cell(0,$this->FontSizePt,$this->titleText,0,1,'C');
    		$l = ($this->lMargin);
    		$this->SetFont('Arial','',$this->cellFontSize);
    		foreach($this->colTitles as $col => $txt) {
    			$this->SetXY($l,($this->tMargin));
    			$this->MultiCell($this->tablewidths[$col], $this->FontSizePt,$txt);
    			$l += $this->tablewidths[$col] ;
    			$maxY = ($maxY < $this->getY()) ? $this->getY() : $maxY ;
    		}
    		$this->SetXY($this->lMargin,$this->tMargin);
    		$this->setFillColor(200,200,200);
    		$l = ($this->lMargin);
    		foreach($this->colTitles as $col => $txt) {
    			$this->SetXY($l,$this->tMargin);
    			$this->cell($this->tablewidths[$col],$maxY-($this->tMargin),'',1,0,'L',1);
    			$this->SetXY($l,$this->tMargin);
    			$this->MultiCell($this->tablewidths[$col],$this->FontSizePt,$txt,0,'C');
    			$l += $this->tablewidths[$col];
    		}
    		$this->setFillColor(255,255,255);
    		// set headerset
    		$this->headerset[$this->page] = 1;
    	}
     
    	$this->SetY($maxY);
    }
     
    function Footer() {
    	// Check if footer for this page already exists
    	if(!$this->footerset[$this->page]) {
    		$this->SetY(-15);
    		//Page number
    		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C');
    		// set footerset
    		$this->footerset[$this->page] = 1;
    	}
    }
     
    function morepagestable($lineheight=8) {
    	// some things to set and 'remember'
    	$l = $this->lMargin;
    	$startheight = $h = $this->GetY();
    	$startpage = $currpage = $this->page;
     
    	// calculate the whole width
    	foreach($this->tablewidths as $width) {
    		$fullwidth += $width;
    	}
     
    	// Now let's start to write the table
    	$row = 0;
    	while($data=mysql_fetch_row($this->results)) {
    		$this->page = $currpage;
    		// write the horizontal borders
    		$this->Line($l,$h,$fullwidth+$l,$h);
    		// write the content and remember the height of the highest col
    		foreach($data as $col => $txt) {
     
    			$this->page = $currpage;
    			$this->SetXY($l,$h);
    			$this->MultiCell($this->tablewidths[$col],$lineheight,$txt,0,$this->colAlign[$col]);
     
    			$l += $this->tablewidths[$col];
     
    			if($tmpheight[$row.'-'.$this->page] < $this->GetY()) {
    				$tmpheight[$row.'-'.$this->page] = $this->GetY();
    			}
    			if($this->page > $maxpage)
    				$maxpage = $this->page;
    			unset($data[$col]);
    		}
    		// get the height we were in the last used page
    		$h = $tmpheight[$row.'-'.$maxpage];
    		// set the "pointer" to the left margin
    		$l = $this->lMargin;
    		// set the $currpage to the last page
    		$currpage = $maxpage;
    		unset($data[$row]);
    		$row++ ;
    	}
    	// draw the borders
    	// we start adding a horizontal line on the last page
    	$this->page = $maxpage;
    	$this->Line($l,$h,$fullwidth+$l,$h);
    	// now we start at the top of the document and walk down
    	for($i = $startpage; $i <= $maxpage; $i++) {
    		$this->page = $i;
    		$l = $this->lMargin;
    		$t = ($i == $startpage) ? $startheight : $this->tMargin;
    		$lh = ($i == $maxpage) ? $h : $this->h-$this->bMargin;
    		$this->Line($l,$t,$l,$lh);
    		foreach($this->tablewidths as $width) {
    			$l += $width;
    			$this->Line($l,$t,$l,$lh);
    		}
    	}
    	// set it to the last page, if not it'll cause some problems
    	$this->page = $maxpage;
    }
     
    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;
    }
     
    function query($query){
    	$this->results = mysql_query($query,$this->conn);
    	$this->numFields = mysql_num_fields($this->results);
    }
     
    function mysql_report($query,$dump=false,$attr=array()){
     
    	foreach($attr as $key=>$val){
    		$this->$key = $val ;
    	}
     
    	$this->query($query);
     
    	// if column widths not set
    	if(!isset($this->tablewidths)){
     
    		// starting col width
    		$this->sColWidth = (($this->w-$this->lMargin-$this->rMargin))/$this->numFields;
     
    		// loop through results header and set initial col widths/ titles/ alignment
    		// if a col title is less than the starting col width / reduce that column size
    		for($i=0;$i<$this->numFields;$i++){
    			$stringWidth = $this->getstringwidth(mysql_field_name($this->results,$i)) + 6 ;
    			if( ($stringWidth) < $this->sColWidth){
    				$colFits[$i] = $stringWidth ;
    				// set any column titles less than the start width to the column title width
    			}
    			$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
    			switch (mysql_field_type($this->results,$i)){
    				case 'int':
    					$this->colAlign[$i] = 'R';
    					break;
    				default:
    					$this->colAlign[$i] = 'L';
    			}
    		}
     
    		// loop through the data, any column whose contents is bigger that the col size is
    		// resized
    		while($row=mysql_fetch_row($this->results)){
    			foreach($colFits as $key=>$val){
    				$stringWidth = $this->getstringwidth($row[$key]) + 6 ;
    				if( ($stringWidth) > $this->sColWidth ){
    					// any col where row is bigger than the start width is now discarded
    					unset($colFits[$key]);
    				}else{
    					// if text is not bigger than the current column width setting enlarge the column
    					if( ($stringWidth) > $val ){
    						$colFits[$key] = ($stringWidth) ;
    					}
    				}
    			}
    		}
     
    		foreach($colFits as $key=>$val){
    			// set fitted columns to smallest size
    			$this->tablewidths[$key] = $val;
    			// to work out how much (if any) space has been freed up
    			$totAlreadyFitted += $val;
    		}
     
    		$surplus = (sizeof($colFits)*$this->sColWidth) - ($totAlreadyFitted);
    		for($i=0;$i<$this->numFields;$i++){
    			if(!in_array($i,array_keys($colFits))){
    				$this->tablewidths[$i] = $this->sColWidth + ($surplus/(($this->numFields)-sizeof($colFits)));
    			}
    		}
     
    		ksort($this->tablewidths);
     
    		if($dump){
    			Header('Content-type: text/plain');
    			for($i=0;$i<$this->numFields;$i++){
    				if(strlen(mysql_field_name($this->results,$i))>$flength){
    					$flength = strlen(mysql_field_name($this->results,$i));
    				}
    			}
    			switch($this->k){
    				case 72/25.4:
    					$unit = 'millimeters';
    					break;
    				case 72/2.54:
    					$unit = 'centimeters';
    					break;
    				case 72:
    					$unit = 'inches';
    					break;
    				default:
    					$unit = 'points';
    			}
    			print "All measurements in $unit\n\n";
    			for($i=0;$i<$this->numFields;$i++){
    				printf("%-{$flength}s : %-10s : %10f\n",
    					mysql_field_name($this->results,$i),
    					mysql_field_type($this->results,$i),
    					$this->tablewidths[$i] );
    			}
    			print "\n\n";
    			print "\$pdf->tablewidths=\n\tarray(\n\t\t";
    			for($i=0;$i<$this->numFields;$i++){
    				($i<($this->numFields-1)) ?
    				print $this->tablewidths[$i].", /* ".mysql_field_name($this->results,$i)." */\n\t\t":
    				print $this->tablewidths[$i]." /* ".mysql_field_name($this->results,$i)." */\n\t\t";
    			}
    			print "\n\t);\n";
    			exit;
    		}
     
    	} else { // end of if tablewidths not defined
     
    		for($i=0;$i<$this->numFields;$i++){
    			$this->colTitles[$i] = mysql_field_name($this->results,$i) ;
    			switch (mysql_field_type($this->results,$i)){
    				case 'int':
    					$this->colAlign[$i] = 'R';
    					break;
    				default:
    					$this->colAlign[$i] = 'L';
    			}
    		}
    	}
     
    	mysql_data_seek($this->results,0);
    	$this->Open();
    	$this->setY($this->tMargin);
    	$this->AddPage();
    	$this->morepagestable($this->FontSizePt);
    	$this->Output();
    }
     
    }
     
    $pdf = new PDF('L','pt','A3');
    $pdf->SetFont('Arial','',11.5);
    $pdf->AliasNbPages();
    $pdf->connect('localhost','****','****','dbclients');
    $attr=array('titleFontSize'=>18,'titleText'=>'Liste complète de la db');
    $pdf->mysql_report("SELECT * FROM clients ORDER BY nom LIMIT 100",false,$attr);
    ?>

  6. #6
    Membre éprouvé Avatar de obito
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    773
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2008
    Messages : 773
    Points : 948
    Points
    948
    Par défaut
    Bonsoir ! Pour la première erreur je pense que ca vient de ce genre de ligne :

    Ca supprime la ligne donc impossible pour lui de pointer à la ligne 0 puisqu'à la fin il ne restera aucun résultat (je pense ^^)

    pour cette erreur :

    FPDF error: Some data has already been output, can't send PDF file"
    Tu dois avoir un echo qui traine dans ton script, hors il ne dois y en avoir aucun
    ►Ne pas oublier le

  7. #7
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut
    Bonsoir,

    Pourtant en local sa fonctionne très bien ??

  8. #8
    Membre éprouvé Avatar de obito
    Homme Profil pro
    Développeur .NET
    Inscrit en
    Janvier 2008
    Messages
    773
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 35
    Localisation : France, Gironde (Aquitaine)

    Informations professionnelles :
    Activité : Développeur .NET

    Informations forums :
    Inscription : Janvier 2008
    Messages : 773
    Points : 948
    Points
    948
    Par défaut
    Peut être que en ligne il faut spécifié un en tête :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <?php
    header("Content-Type: application/pdf");
    ?>
    A mettre tout en haut du script
    ►Ne pas oublier le

  9. #9
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut
    J'ai tester sa ne fonctionne pas, je ne comprend pas car en local sa fonctionne.

    Merci pour votre aide

  10. #10
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    Quelle est le probleme que tu rencontres au point ou nous en sommes ?
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  11. #11
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut
    bonsoir,

    le script fonctionne très bien en local, alors qu'en ligne il m'indique un msg d'erreur : "Warning: mysql_data_seek() [function.mysql-data-seek]: Offset 0 is invalid for MySQL result index 5 (or the query data is unbuffered) in /var/www/vhosts/test.be/httpdocs/db/mysql_report.php on line 292
    FPDF error: Some data has already been output, can't send PDF file
    "

    J'ai pourtant bien ajouter tout les fichiers qu'il faut dans le même rép.

  12. #12
    Modérateur
    Avatar de sabotage
    Homme Profil pro
    Inscrit en
    Juillet 2005
    Messages
    29 208
    Détails du profil
    Informations personnelles :
    Sexe : Homme

    Informations forums :
    Inscription : Juillet 2005
    Messages : 29 208
    Points : 44 155
    Points
    44 155
    Par défaut
    A quoi sert ce data_seek() dans ton code ?
    N'oubliez pas de consulter les FAQ PHP et les cours et tutoriels PHP

  13. #13
    Membre du Club
    Inscrit en
    Août 2007
    Messages
    416
    Détails du profil
    Informations forums :
    Inscription : Août 2007
    Messages : 416
    Points : 63
    Points
    63
    Par défaut
    Je ne sais pas exactement à quoi il sert

Discussions similaires

  1. Réponses: 1
    Dernier message: 14/04/2014, 09h44
  2. Réponses: 4
    Dernier message: 08/04/2011, 09h44
  3. Réponses: 1
    Dernier message: 11/10/2010, 14h31
  4. Réponses: 4
    Dernier message: 29/05/2009, 11h54
  5. Réponses: 1
    Dernier message: 31/01/2007, 11h59

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