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 :

[PDF] Génération PDF avec TCPDF


Sujet :

Bibliothèques et frameworks PHP

  1. #1
    Membre régulier
    Homme Profil pro
    Etudiant du Genie Logiciel
    Inscrit en
    Juillet 2011
    Messages
    397
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Etudiant du Genie Logiciel
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2011
    Messages : 397
    Points : 73
    Points
    73
    Par défaut [PDF] Génération PDF avec TCPDF
    Bonjour à tous,

    j'utilise TCPDF pour générer mes documents PDF. la génération se passe très bien. le hic est que je n'arrive pas a ouvrir le document généré. j'ai le message suivant lors de l'ouverture du document avec acrobat reader:
    Adobe Reader n'a pas pu ouvrir fichier.pdf, car le type de fichier n'est pas pris en charge ou le fichier est endommagé (il a été envoyé en tant que pièce jointe et n'a pas été décodé correctement, par exemple)
    cette erreur peut-être due a quoi ?

    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
     
    if ($format == 'pdf')
    			//$report = html2pdf($report);
    			////////
    			// create new PDF document
    			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    			// set document information
    			$pdf->SetCreator(PDF_CREATOR);
    			$pdf->SetAuthor('Nicola Asuni');
    			$pdf->SetTitle('TCPDF Example 001');
    			$pdf->SetSubject('TCPDF Tutorial');
    			$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
     
    			// set default header data
    			$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
    			$pdf->setFooterData(array(0,64,0), array(0,64,128));
     
    			// set header and footer fonts
    			$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    			$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
     
    			// set default monospaced font
    			$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
     
    			// set margins
    			$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    			$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    			$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
     
    			// set auto page breaks
    			$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     
    			// set image scale factor
    			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
     
    			// set some language-dependent strings (optional)
    			if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    				require_once(dirname(__FILE__).'/lang/eng.php');
    				$pdf->setLanguageArray($l);
    			}
     
    			// ---------------------------------------------------------
     
    			// set default font subsetting mode
    			$pdf->setFontSubsetting(true);
     
    			// Set font
    			// dejavusans is a UTF-8 Unicode font, if you only need to
    			// print standard ASCII chars, you can use core fonts like
    			// helvetica or times to reduce file size.
    			$pdf->SetFont('dejavusans', '', 14, '', true);
     
    			// Add a page
    			// This method has several options, check the source code documentation for more information.
    			$pdf->AddPage();
     
    			// set text shadow effect
    			$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
     
    			// Set some content to print
    			$html = '<<<EOD
    			<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
    			<i>This is the first example of TCPDF library.</i>
    			<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
    			<p>Please check the source code documentation and other examples for further information.</p>
    			<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
    			EOD';
    			// Print text using writeHTMLCell()
    			$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    			// ---------------------------------------------------------
    			// Close and output PDF document
    			// This method has several options, check the source code documentation for more information.
    			ob_clean();
    			$report=$pdf->Output('example_001.pdf', 'I');
     
    			//////////
    		}

  2. #2
    Membre actif
    Inscrit en
    Août 2006
    Messages
    191
    Détails du profil
    Informations forums :
    Inscription : Août 2006
    Messages : 191
    Points : 263
    Points
    263
    Par défaut
    Salut,

    Tu as vraiment juste recopié l example_001 du site TCPDF qui est d'ailleurs disponible dans le répertoire examples de TCPDF.

    si j’exécute ceci chez moi ça m affiche sans souci une page pdf.
    .../tcpdf/examples/example_001.php

    par contre, ton IF en haut n'a pas d accolade.

    donc en gros il s applique à la première ligne qui suis non commentée.
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    qui provoquerais une erreur php qui renverrai une erreur 500 basique.

    Donc le message d erreur que tu nous donnes ne peux pas être généré par ce bout de code isoler mais par un autre bout de code que tu ne nous à pas montré.

    Je ne peux pas t'aider plus là. Sachant que l'erreur parle de l ouverture du fichier "fichier.pdf" alors que le fichier généré s appel "example_001.pdf" et qu'il n'est pas écrit mais renvoyé à l'écran !

  3. #3
    Membre régulier
    Homme Profil pro
    Etudiant du Genie Logiciel
    Inscrit en
    Juillet 2011
    Messages
    397
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Etudiant du Genie Logiciel
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2011
    Messages : 397
    Points : 73
    Points
    73
    Par défaut
    salut,
    je t'explique ma démarche. je veux changer de librairie de reporting de ma plateforme et utiliser TCPDF. donc l'exemple c'est juste un test. le fichier est bien généré et téléchargé.

    côté client : génération du fichier pdf via un appel ajax
    Code JavaScript : 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
            loadingData(true);
                        $.ajax({
                            type: "POST",
                                url: "func/fn_reports.gen2.php",
                            data: data,
                            cache: false,
                            success: function (t) {
                                loadingData(false);
    							switch(data.format){
    								case "pdf" :
    									$.generateFile({
                    filename: e.name.toLowerCase() + "_" + e.dtf + "_" + e.dtt,
                    content: t,
                    script: "func/fn_saveas.php?format=" + e.format
                }), reportsGeneratedReload()
    									break ;
    									case "html" :
    									break ;
    								   }
     
                            },
                            error: function (deleted_model, err) {
                                loadingData(false);
                            }
                        });

    voici l'endroit ou le PDF est généré :
    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
    if(@$_POST['cmd'] == 'report')
    	{
    		// check privileges
    		if ($_SESSION["privileges"] == 'subuser')
    		{
    			$user_id = $_SESSION["manager_id"];
    		}
    		else
    		{
    			$user_id = $_SESSION["user_id"];
    		}
     
    		// generate or send report to e-mail
    		if (isset($_POST['schedule']))
    		{
    			//check user usage
    			if (!checkUserUsage($user_id, 'email')) die;
     
    			reportsSend();
    		}
    		else
    		{
    			$report = reportsGenerate();
     
    			if ($report != false)
    			{
    				echo $report;
    			}
    		}
     
    		die;
    	}
    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
    function reportsGenerate()
    	{
     
    if ($format == 'pdf')
    		{
    			//$report = html2pdf($report);
    			////////
    			// create new PDF document
    			$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
    			// set document information
    			$pdf->SetCreator(PDF_CREATOR);
    			$pdf->SetAuthor('Nicola Asuni');
    			$pdf->SetTitle('TCPDF Example 001');
    			$pdf->SetSubject('TCPDF Tutorial');
    			$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
     
    			// set default header data
    			$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE.' 001', PDF_HEADER_STRING, array(0,64,255), array(0,64,128));
    			$pdf->setFooterData(array(0,64,0), array(0,64,128));
     
    			// set header and footer fonts
    			$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
    			$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
     
    			// set default monospaced font
    			$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
     
    			// set margins
    			$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
    			$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
    			$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
     
    			// set auto page breaks
    			$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     
    			// set image scale factor
    			$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
     
    			// set some language-dependent strings (optional)
    			if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    				require_once(dirname(__FILE__).'/lang/eng.php');
    				$pdf->setLanguageArray($l);
    			}
     
    			// ---------------------------------------------------------
     
    			// set default font subsetting mode
    			$pdf->setFontSubsetting(true);
     
    			// Set font
    			// dejavusans is a UTF-8 Unicode font, if you only need to
    			// print standard ASCII chars, you can use core fonts like
    			// helvetica or times to reduce file size.
    			$pdf->SetFont('dejavusans', '', 14, '', true);
     
    			// Add a page
    			// This method has several options, check the source code documentation for more information.
    			$pdf->AddPage();
     
    			// set text shadow effect
    			$pdf->setTextShadow(array('enabled'=>true, 'depth_w'=>0.2, 'depth_h'=>0.2, 'color'=>array(196,196,196), 'opacity'=>1, 'blend_mode'=>'Normal'));
     
    			// Set some content to print
    			$html = '<<<EOD
    			<h1>Welcome to <a href="http://www.tcpdf.org" style="text-decoration:none;background-color:#CC0000;color:black;">&nbsp;<span style="color:black;">TC</span><span style="color:white;">PDF</span>&nbsp;</a>!</h1>
    			<i>This is the first example of TCPDF library.</i>
    			<p>This text is printed using the <i>writeHTMLCell()</i> method but you can also use: <i>Multicell(), writeHTML(), Write(), Cell() and Text()</i>.</p>
    			<p>Please check the source code documentation and other examples for further information.</p>
    			<p style="color:#CC0000;">TO IMPROVE AND EXPAND TCPDF I NEED YOUR SUPPORT, PLEASE <a href="http://sourceforge.net/donate/index.php?group_id=128076">MAKE A DONATION!</a></p>
    			EOD';
    			// Print text using writeHTMLCell()
    			$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
    			// ---------------------------------------------------------
    			// Close and output PDF document
    			// This method has several options, check the source code documentation for more information.
    			ob_clean();
     
            $report=$pdf->Output("test01.pdf", 'D');
     
    			//////////
    		}
    if (!isset($_POST['schedule']))
    		{
    			$report = base64_encode($report);	
    		}
     
     
    return $report ;
     
    }
    ce fichier est bien envoyé au navigateur. sauf que, quand j'essaie de l'ouvrir avec un lecteur de PDF, on m'envoit le message ci-dessus.

  4. #4
    Membre régulier
    Homme Profil pro
    Etudiant du Genie Logiciel
    Inscrit en
    Juillet 2011
    Messages
    397
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Cameroun

    Informations professionnelles :
    Activité : Etudiant du Genie Logiciel
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juillet 2011
    Messages : 397
    Points : 73
    Points
    73
    Par défaut
    salut,
    j'ai reussi a ouvrir le fichier mais ce dernier est vide.
    aidé moi a comprendre
    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
     
    function reportGenerate(data) {
        if ("overspeed" != data.type && "underspeed" != data.type && "rag" != data.type && "rag_driver" != data.type || "" != data.speed_limit) {
            if ("" != data.imei) {
                if ("zone_in_out" != data.type || "" != data.zone_ids) {
                    if ("logic_sensors" != data.type && "sensor_graph" != data.type || "" != data.sensor_names) {
                        loadingData(true);
                        $.ajax({
                            type: "POST",
                            url: "func/fn_reports.gen2.php",
                            data: data,
                            cache: false,
                            success: function (t) {
                                loadingData(false);
                                switch (data.format) {
                                    case "pdf" :
                                        var test = window.btoa(unescape(encodeURIComponent(t)));
                                        $.generateFile({
                                            filename: data.name.toLowerCase() + "_" + data.dtf + "_" + data.dtt,
                                            content: test,
                                            script: "func/fn_saveas1.php?format=" + data.format
                                        })
                                        //window.open('data:application/pdf;base64,' + test);
                                        break;
                                    case "html" :
                                        var decodedString = atob(t);
                                        console.log("tttt\t\t " + decodedString);
                                        break;
                                reportsGeneratedReload();
                            },
                            error: function (deleted_model, err) {
                                loadingData(false);
                            }
                        });
                    } else {
                        notifyBox("error", la.ERROR, la.AT_LEAST_ONE_SENSOR_SELECTED);
                    }
                } else {
                    notifyBox("error", la.ERROR, la.AT_LEAST_ONE_ZONE_SELECTED);
                }
            } else {
                notifyBox("error", la.ERROR, la.AT_LEAST_ONE_OBJECT_SELECTED);
            }
        } else {
            notifyBox("error", la.ERROR, la.SPEED_LIMIT_CANT_BE_EMPTY);
        }
    }

Discussions similaires

  1. [PDF] Génération PDF avec TCPDF
    Par flilou dans le forum Bibliothèques et frameworks
    Réponses: 12
    Dernier message: 28/02/2011, 10h08
  2. Génération automatique de PDF avec Win2PDF
    Par cdelefil dans le forum API, COM et SDKs
    Réponses: 4
    Dernier message: 03/10/2007, 11h01
  3. [FPDF] génération de pdf avec Fpdf sous firefox
    Par discogarden dans le forum Bibliothèques et frameworks
    Réponses: 6
    Dernier message: 08/06/2006, 10h50
  4. [FPDF] Génération de fichiers PDF avec PHP
    Par deY! dans le forum Bibliothèques et frameworks
    Réponses: 3
    Dernier message: 10/04/2006, 17h06
  5. [FPDF] génération d'attestation en PDF avec PHP
    Par moabomotal dans le forum Bibliothèques et frameworks
    Réponses: 2
    Dernier message: 15/03/2006, 13h50

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