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] Débutant avec fpdf


Sujet :

Bibliothèques et frameworks PHP

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Par défaut [FPDF] Débutant avec fpdf
    Bonjour,

    Je voudrais pouvoir imprimer une table dans un PDF, j'ai donc fait une recherche sur google qui ma renvoyé sur http://www.fpdf.org/.

    Après avoir lu j'ai fait un premier test:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
     
    <?php
    require('fpdf.php');
     
    $pdf = new FPDF();
    $pdf->AddPage();
    $pdf->SetFont('Arial','B',16);
    $pdf->Cell(40,10,'Hello World !');
    $pdf->Output();
    ?>
    ça ma bien afficher un Hello World en PDF.

    Ensuite j'ai essayé avec Mysql imprimer une table en me basant sur un exemple du site fpdf :

    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
     
    <?php
    //PDF USING MULTIPLE PAGES
    //CREATED BY: Carlos Vasquez S.
    //E-MAIL: cvasquez@cvs.cl
    //CVS TECNOLOGIA E INNOVACION
    //SANTIAGO, CHILE
     
    require('fpdf.php');
     
    //Connect to your database
    include("conectmysql.php");
     
    //Create new pdf file
    $pdf=new FPDF();
     
    //Disable automatic page break
    $pdf->SetAutoPageBreak(false);
     
    //Add first page
    $pdf->AddPage();
     
    //set initial y axis position per page
    $y_axis_initial = 25;
     
    //print column titles
    $pdf->SetFillColor(232,232,232);
    $pdf->SetFont('Arial','B',12);
    $pdf->SetY($y_axis_initial);
    $pdf->SetX(25);
    $pdf->Cell(30,6,'ID_CANDIDAT',1,0,'L',1);
    $pdf->Cell(100,6,'NOM',1,0,'L',1);
    $pdf->Cell(30,6,'PRENOM',1,0,'R',1);
     
    $y_axis = $y_axis + $row_height;
     
    //Select the Products you want to show in your PDF file
    $result=mysql_query('select ID_CANDIDAT,NOM,PRENOM from tbl_candidats ORDER BY ID_CANDIDAT',$link);
     
    //initialize counter
    $i = 0;
     
    //Set maximum rows per page
    $max = 25;
     
    //Set Row Height
    $row_height = 6;
     
    while($row = mysql_fetch_array($result))
    {
        //If the current row is the last one, create new page and print column title
        if ($i == $max)
        {
            $pdf->AddPage();
     
            //print column titles for the current page
            $pdf->SetY($y_axis_initial);
            $pdf->SetX(25);
            $pdf->Cell(30,6,'ID_CANDIDAT',1,0,'L',1);
            $pdf->Cell(100,6,'NOM',1,0,'L',1);
            $pdf->Cell(30,6,'PRENOM',1,0,'R',1);
     
            //Go to next row
            $y_axis = $y_axis + $row_height;
     
            //Set $i variable to 0 (first row)
            $i = 0;
        }
     
        $id = $row['ID_CANDIDAT'];
        $nom = $row['NOM'];
        $prenom = $row['PRENOM'];
     
        $pdf->SetY($y_axis);
        $pdf->SetX(25);
        $pdf->Cell(30,6,$id,1,0,'L',1);
        $pdf->Cell(100,6,$nom,1,0,'L',1);
        $pdf->Cell(30,6,$prenom,1,0,'R',1);
     
        //Go to next row
        $y_axis = $y_axis + $row_height;
        $i = $i + 1;
    }
     
    mysql_close($link);
     
    //Send file
    $pdf->Output();
    ?>
    Et la j'ai droit a des erreurs:

    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
     
    Notice: Undefined variable: row_height in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 34
     
    Notice: Undefined variable: y_axis in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 34
     
    Notice: Undefined variable: link in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 37
     
    Warning: mysql_query() expects parameter 2 to be resource, null given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 37
     
    Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 48
     
    Notice: Undefined variable: link in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 84
     
    Warning: mysql_close() expects parameter 1 to be resource, null given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 84
    FPDF error: Some data has already been output, can't send PDF file
    Pourtant je me suis basé sur l'exemple.

    Que faire ??? je sais que Undefined variable: row_height veut dire que ma variable n'est pas définie mais même si je la définie ça ne change rien.

    D'avance merci

    D'avance merci

  2. #2
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 506
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 506
    Par défaut
    Pour ceci c'est facile, suffit de lire
    Notice: Undefined variable: row_height in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 34

    Notice: Undefined variable: y_axis in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 34

    Notice: Undefined variable: link in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 37

    donc, pour une programmation propre on initialise ses variables

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    $row_height = NULL;
     
    .....
    la variable $link, elle viens d'où ?

    Règle déjà ça, on verra pour la suite !

  3. #3
    Membre éclairé
    Profil pro
    Inscrit en
    Juin 2006
    Messages
    661
    Détails du profil
    Informations personnelles :
    Localisation : Belgique

    Informations forums :
    Inscription : Juin 2006
    Messages : 661
    Par défaut
    Merci de ton aide MaitrePylos, j'ai donc initialiser les Variables.

    J'ai remplacé $link par la variable qui contient ma chaîne de connexion du fichier conectmysql.php:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
     
    <?php
    $bdd = new PDO('mysql:host=192.168.1.4;dbname=recrutement', 'DEV1', 'DEV1');
     
      if ( mysql_errno() > 0 ) {
     
           echo mysql_error();
     
           exit;
     
        }
     
    ?>
    Voici donc 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
    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
     
    <?php
    //PDF USING MULTIPLE PAGES
    //CREATED BY: Carlos Vasquez S.
    //E-MAIL: cvasquez@cvs.cl
    //CVS TECNOLOGIA E INNOVACION
    //SANTIAGO, CHILE
     
    require('fpdf.php');
     
    $row_height = NULL;
    $y_axis= NULL;
     
     
    //Connect to your database
    include("conectmysql.php");
     
    //Create new pdf file
    $pdf=new FPDF();
     
    //Disable automatic page break
    $pdf->SetAutoPageBreak(false);
     
    //Add first page
    $pdf->AddPage();
     
    //set initial y axis position per page
    $y_axis_initial = 25;
     
    //print column titles
    $pdf->SetFillColor(232,232,232);
    $pdf->SetFont('Arial','B',12);
    $pdf->SetY($y_axis_initial);
    $pdf->SetX(25);
    $pdf->Cell(30,6,'ID_CANDIDAT',1,0,'L',1);
    $pdf->Cell(100,6,'NOM',1,0,'L',1);
    $pdf->Cell(30,6,'PRENOM',1,0,'R',1);
     
    $y_axis = $y_axis + $row_height;
     
    //Select the Products you want to show in your PDF file
    $result=mysql_query('select ID_CANDIDAT,NOM,PRENOM from tbl_candidats ORDER BY ID_CANDIDAT',$bdd);
     
    //initialize counter
    $i = 0;
     
    //Set maximum rows per page
    $max = 25;
     
    //Set Row Height
    $row_height = 6;
     
    while($row = mysql_fetch_array($result))
    {
        //If the current row is the last one, create new page and print column title
        if ($i == $max)
        {
            $pdf->AddPage();
     
            //print column titles for the current page
            $pdf->SetY($y_axis_initial);
            $pdf->SetX(25);
            $pdf->Cell(30,6,'ID_CANDIDAT',1,0,'L',1);
            $pdf->Cell(100,6,'NOM',1,0,'L',1);
            $pdf->Cell(30,6,'PRENOM',1,0,'R',1);
     
            //Go to next row
            $y_axis = $y_axis + $row_height;
     
            //Set $i variable to 0 (first row)
            $i = 0;
        }
     
        $id = $row['ID_CANDIDAT'];
        $nom = $row['NOM'];
        $prenom = $row['PRENOM'];
     
        $pdf->SetY($y_axis);
        $pdf->SetX(25);
        $pdf->Cell(30,6,$id,1,0,'L',1);
        $pdf->Cell(100,6,$nom,1,0,'L',1);
        $pdf->Cell(30,6,$prenom,1,0,'R',1);
     
        //Go to next row
        $y_axis = $y_axis + $row_height;
        $i = $i + 1;
    }
     
    mysql_close($bdd);
     
    //Send file
    $pdf->Output();
    ?>
    Message d’erreurs:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
     
    Warning: mysql_query() expects parameter 2 to be resource, object given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 41
     
    Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 52
     
    Warning: mysql_close() expects parameter 1 to be resource, object given in C:\Program Files (x86)\EasyPHP-5.3.8.1\www\test.php on line 88
    FPDF error: Some data has already been output, can't send PDF file
    J'ai trouvé le script ici http://www.fpdf.org/fr/script/script30.php .

  4. #4
    Expert confirmé

    Homme Profil pro
    Développeur Web
    Inscrit en
    Septembre 2010
    Messages
    5 419
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Puy de Dôme (Auvergne)

    Informations professionnelles :
    Activité : Développeur Web
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Septembre 2010
    Messages : 5 419
    Par défaut
    Le message d'erreur te dis que mysql_query attend une ressource (de connexion) et que tu lui donne un objet comme second paramètre (=$bdd).

    Ne fais pas une connexion avec PDO mais avec mysql_connect si tu utilise mysql par la suite. Ou sinon fais tout avec PDO mais choisi l'un ou l'autre.

  5. #5
    Modérateur

    Avatar de MaitrePylos
    Homme Profil pro
    DBA
    Inscrit en
    Juin 2005
    Messages
    5 506
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 52
    Localisation : Belgique

    Informations professionnelles :
    Activité : DBA
    Secteur : Service public

    Informations forums :
    Inscription : Juin 2005
    Messages : 5 506
    Par défaut
    Bon va essayer de mettre sur la voie .

    Ton fichier conectmysql.php

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
     
    $login = 'DEV1';
    $mdp = 'DEV1;
    // Pour MySQL :
    $bdd = 'mysql:host=localhost;dbname=recrutement';
     
    try{
    $bdd = new PDO($bdd, $login, $mdp);
    }
    catch (PDOException $error) {
    die("Erreur de connexion : " . $error->getMessage() );
    }

    Ton code Fpdf

    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
     
    <?php
    require('fpdf.php');
    include("conectmysql.php");
     
    $row_height = NULL;
    $y_axis= NULL;
     
    //Create new pdf file
    $pdf=new FPDF();
     
    //Disable automatic page break
    $pdf->SetAutoPageBreak(false);
     
    //Add first page
    $pdf->AddPage();
     
    //set initial y axis position per page
    $y_axis_initial = 25;
     
    //print column titles
    $pdf->SetFillColor(232,232,232);
    $pdf->SetFont('Arial','B',12);
    $pdf->SetY($y_axis_initial);
    $pdf->SetX(25);
    $pdf->Cell(30,6,'ID_CANDIDAT',1,0,'L',1);
    $pdf->Cell(100,6,'NOM',1,0,'L',1);
    $pdf->Cell(30,6,'PRENOM',1,0,'R',1);
     
    $y_axis = $y_axis + $row_height;
     
    //Select the Products you want to show in your PDF file
    $sql = 'SELECT ID_CANDIDAT,NOM,PRENOM FROM tbl_candidats ORDER BY ID_CANDIDAT';
     
    $stmt = $bdd->query($sql);
     
     
    //initialize counter
    $i = 0;
     
    //Set maximum rows per page
    $max = 25;
     
    //Set Row Height
    $row_height = 6;
     
    while($row =  $stmt->fetch(PDO::FETCH_ASSOC))
    {
        //If the current row is the last one, create new page and print column title
        if ($i == $max)
        {
            $pdf->AddPage();
     
            //print column titles for the current page
            $pdf->SetY($y_axis_initial);
            $pdf->SetX(25);
            $pdf->Cell(30,6,'ID_CANDIDAT',1,0,'L',1);
            $pdf->Cell(100,6,'NOM',1,0,'L',1);
            $pdf->Cell(30,6,'PRENOM',1,0,'R',1);
     
            //Go to next row
            $y_axis = $y_axis + $row_height;
     
            //Set $i variable to 0 (first row)
            $i = 0;
        }
     
        $id = $row['ID_CANDIDAT'];
        $nom = $row['NOM'];
        $prenom = $row['PRENOM'];
     
        $pdf->SetY($y_axis);
        $pdf->SetX(25);
        $pdf->Cell(30,6,$id,1,0,'L',1);
        $pdf->Cell(100,6,$nom,1,0,'L',1);
        $pdf->Cell(30,6,$prenom,1,0,'R',1);
     
        //Go to next row
        $y_axis = $y_axis + $row_height;
        $i = $i + 1;
    }
     
    $bdd = null;
     
    $pdf->Output();
    ?>

Discussions similaires

  1. Débutant avec livre microapp PHP5, MACOSX
    Par rvm31 dans le forum EDI, CMS, Outils, Scripts et API
    Réponses: 10
    Dernier message: 13/07/2006, 12h53
  2. pb de débutant avec un tableau
    Par nielsou dans le forum C++
    Réponses: 5
    Dernier message: 09/06/2006, 16h15
  3. [C#][service windows] problème de débutant avec 1 timer
    Par Nycos62 dans le forum Windows Forms
    Réponses: 3
    Dernier message: 14/10/2005, 11h22
  4. [Delphi 2005] Débutant avec problème de création de fichier
    Par Patcdc dans le forum Bases de données
    Réponses: 2
    Dernier message: 06/06/2005, 18h41
  5. probleme de débutant avec D3DXVECTOR3
    Par airseb dans le forum DirectX
    Réponses: 6
    Dernier message: 16/08/2003, 21h03

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