[RESOLU] Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\Invoicing\functions

Eléphant du PHP | 385 Messages

21 févr. 2017, 15:22

Bonjour à tous,

Pourriez vous m'aider?

J'essaye de faire un générateur de factures, tout fonctionne mais j'ai un problème de header.
Je rempli un formulaire un est envoyé à mon controleur

Code : Tout sélectionner

<?php require_once '../vendor/autoload.php'; if(filter_input(INPUT_POST, 'generateInvoice', FILTER_SANITIZE_STRING)) { //Je stocke mes données dans un objet $corporation = new Model\Corporation(); $corporationName = filter_input(INPUT_POST, 'coporationName', FILTER_SANITIZE_STRING); $corporationAdress = filter_input(INPUT_POST, 'corporationAdress', FILTER_SANITIZE_STRING); $corporation->setName($corporationName); $corporation->setAdress($corporationAdress); // Pour avoir accès aux informations de mon objet je le sérialize en session session_start(); $_SESSION['corporation'] = serialize($corporation); // Ici je récupère le contenu du fichier pdf le problème si j'utilise file_get_content mon php n'ai pas interprété $content = include('../functions/InvoicePdf.php'); $mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); $mpdf->SetDisplayMode('fullpage'); $mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list $mpdf->WriteHTML($content); $mpdf->Output(); } ?>
Mon template facture

Code : Tout sélectionner

<?php $corporation = unserialize($_SESSION['corporation']); ?> <!DOCTYPE html> <html> <head> <title>Print Invoice</title> <style> * { margin:0; padding:0; font-family:Arial; font-size:10pt; color:#000; } body { width:100%; font-family:Arial; font-size:10pt; margin:0; padding:0; } p { margin:0; padding:0; } #wrapper { width:180mm; margin:0 15mm; } .page { height:297mm; width:210mm; page-break-after:always; } table { border-left: 1px solid #ccc; border-top: 1px solid #ccc; border-spacing:0; border-collapse: collapse; } table td { border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; padding: 2mm; } table.heading { height:50mm; } h1.heading { font-size:14pt; color:#000; font-weight:normal; } h2.heading { font-size:9pt; color:#000; font-weight:normal; } hr { color:#ccc; background:#ccc; } #invoice_body { height: 149mm; } #invoice_body , #invoice_total { width:100%; } #invoice_body table , #invoice_total table { width:100%; border-left: 1px solid #ccc; border-top: 1px solid #ccc; border-spacing:0; border-collapse: collapse; margin-top:5mm; } #invoice_body table td , #invoice_total table td { text-align:center; font-size:9pt; border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; padding:2mm 0; } #invoice_body table td.mono , #invoice_total table td.mono { font-family:monospace; text-align:right; padding-right:3mm; font-size:10pt; } #footer { width:180mm; margin:0 15mm; padding-bottom:3mm; } #footer table { width:100%; border-left: 1px solid #ccc; border-top: 1px solid #ccc; background:#eee; border-spacing:0; border-collapse: collapse; } #footer table td { width:25%; text-align:center; font-size:9pt; border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; } </style> </head> <body> <div id="wrapper"> <p style="text-align:center; font-weight:bold; padding-top:5mm;">INVOICE</p> <br /> <table class="heading" style="width:100%;"> <tr> <td style="width:80mm;"> <h1 class="heading"><?php echo $corporation->getName(); ?></h1> <h2 class="heading"> <?php echo $corporation->getAdress(); ?> </h2> </td> <td rowspan="2" valign="top" align="right" style="padding:3mm;"> <table> <tr><td>Invoice No : </td><td>11-12-17</td></tr> <tr><td>Dated : </td><td>01-Aug-2011</td></tr> <tr><td>Currency : </td><td>USD</td></tr> </table> </td> </tr> <tr> <td> <b>Buyer</b> :<br /> Client Name<br /> Client Address <br /> City - Pincode , Country<br /> </td> </tr> </table> <div id="content"> <div id="invoice_body"> <table> <tr style="background:#eee;"> <td style="width:8%;"><b>Sl. No.</b></td> <td><b>Product</b></td> <td style="width:15%;"><b>Quantity</b></td> <td style="width:15%;"><b>Rate</b></td> <td style="width:15%;"><b>Total</b></td> </tr> </table> <table> <tr> <td style="width:8%;">1</td> <td style="text-align:left; padding-left:10px;">Software Development<br />Description : Upgradation of telecrm</td> <td class="mono" style="width:15%;">1</td><td style="width:15%;" class="mono">157.00</td> <td style="width:15%;" class="mono">157.00</td> </tr> <tr> <td colspan="3"></td> <td></td> <td></td> </tr> <tr> <td colspan="3"></td> <td>Total :</td> <td class="mono">157.00</td> </tr> </table> </div> <div id="invoice_total"> Total Amount : <table> <tr> <td style="text-align:left; padding-left:10px;">One Hundred And Fifty Seven only</td> <td style="width:15%;">USD</td> <td style="width:15%;" class="mono">157.00</td> </tr> </table> </div> <br /> <hr /> <br /> <table style="width:100%; height:35mm;"> <tr> <td style="width:65%;" valign="top"> Payment Information :<br /> Please make cheque payments payable to : <br /> <b>ABC Corp</b> <br /><br /> The Invoice is payable within 7 days of issue.<br /><br /> </td> <td> <div id="box"> E & O.E.<br /> For ABC Corp<br /><br /><br /><br /> Authorised Signatory </div> </td> </tr> </table> </div> <br /> </div> <htmlpagefooter name="footer"> <hr /> <div id="footer"> <table> <tr><td>Software Solutions</td><td>Mobile Solutions</td><td>Web Solutions</td></tr> </table> </div> </htmlpagefooter> <sethtmlpagefooter name="footer" value="on" /> </body> </html>
Si vous voyez aussi des choses que je peux améliorer dans la logique du code les conseils sont les bienvenues. Merci à vous.

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

21 févr. 2017, 15:27

salut,

a priori tu as un caractère blanc ou un affichage quelconque dans le fichier InvoicePdf.php et lorsque tu utilises le header il y a une erreur.

si tu peux ne met pas de ?> pour éviter les espaces blancs après le tags de fermeture ;)
il faut voir aussi pour qu'il n'y en ai pas avant le <?php

Le message d'erreur complet te fournit la ligne où l'affichage est fait (output started at).

@+
Il en faut peu pour être heureux ......

Eléphant du PHP | 385 Messages

21 févr. 2017, 18:10

Merci,

J'ai regardé mais rien n'y fait :( .
Si j'enlève le ?> j'ai un problème de balisage.
Le message d'erreur complet

Code : Tout sélectionner

HTML contains invalid UTF-8 character(s) 1 ( ! ) Warning: Cannot modify header information - headers already sent by (output started at C:\wamp64\www\Invoicing\functions\InvoicePdf.php:260) in C:\wamp64\www\Invoicing\vendor\mpdf\mpdf\mpdf.php on line 1746 Call Stack # Time Memory Function Location 1 0.0015 249192 {main}( ) ...\InvoicingController.php:0 2 0.1107 14302664 mPDF->WriteHTML( ) ...\InvoicingController.php:25 3 0.1108 14303112 mPDF->purify_utf8( ) ...\mpdf.php:13124 4 0.1108 14303336 mPDF->Error( ) ...\mpdf.php:31105 5 0.1108 14303488 header ( ) ...\mpdf.php:1746
la ligne 260 du fichier InvoicePdf correspond

Code : Tout sélectionner

</body></html>
j'ai donc essayer dans mon éditeur des retours chariots pour supprimer d'éventuel espace blanc et caractères invisibles mais même erreur.

le problème dans mpdf est avec

Code : Tout sélectionner

if (!$this->is_utf8($html)) { echo "<p><b>HTML contains invalid UTF-8 character(s)</b></p>"; while (mb_convert_encoding(mb_convert_encoding($html, "UTF-32", "UTF-8"), "UTF-8", "UTF-32") != $html) { $a = iconv('UTF-8', 'UTF-8', $html); echo ($a); $pos = $start = strlen($a); $err = ''; while ( ord(substr($html,$pos,1)) > 128 ) { $err .= '[[#'.ord(substr($html,$pos,1)).']]'; $pos++; } echo '<span style="color:red; font-weight:bold">'.$err.'</span>'; $html = substr($html, $pos); } echo $html; $this->Error(""); }
Par contre avant le code de ce site http://www.binarytides.com/create-pdf-f ... df-in-php/ fonctionnait mais j'ai du le modifier pour que la génération soit dynamique.

Merci pour votre aide.

Avatar du membre
Modérateur PHPfrance
Modérateur PHPfrance | 8758 Messages

21 févr. 2017, 18:16

donc je réitère

ligne 1746 du fichier C:\wamp64\www\Invoicing\vendor\mpdf\mpdf\mpdf.php il y a l'utilisation de la fonction header

or ce fichier inclus InvoicePdf.php qui affiche quelque chose ligne 260 (en l’occurrence du html comme tu l'indiques).

il ne faut pas d'affichage avant la fonction header.
Si tu ne peux pas faire autrement utilise du javascrit pour la redirection (location.href) plutôt qu'en php.

@+
Il en faut peu pour être heureux ......

Eléphant du PHP | 385 Messages

22 févr. 2017, 13:32

Merci,

Problème résolu avec

Code : Tout sélectionner

session_start(); $_SESSION['corporation'] = serialize($corporation); ob_start(); include('../functions/InvoicePdf.php'); $content = ob_get_clean(); $mpdf=new mPDF('c','A4','','' , 0 , 0 , 0 , 0 , 0 , 0); $mpdf->SetDisplayMode('fullpage'); $mpdf->list_indent_first_level = 0; // 1 or 0 - whether to indent the first level of a list $mpdf->WriteHTML($content); $mpdf->Output();
Avec la temporisation le problème de header est réglé.

Petit nouveau ! | 2 Messages

21 juin 2019, 16:35

Merci pour l'info également, cela fait plusieurs heures que j'essaye de résoudre mon problème de header
Développeur PHP @JobPhoning