Search code examples
phphtml2pdf

TCPDF ERROR: Some data has already been output to browser, can't send PDF file


i'm trying to convert this 'PHP' page to 'PDF' page but i toke this message everytime. Although, everything is looks right.

the errors :

-First one : Notice: Undefined offset: 0 in \html2pdf\html2pdf.class.php on line 4957

-2end error : Notice: Undefined offset: 0 in \html2pdf\html2pdf.class.php on line 4957

-3rd error : Warning: Cannot modify header information - headers already sent by (output started at \html2pdf\html2pdf.class.php:4957) in \html2pdf_tcpdf_5.0.002\tcpdf.php on line 6122

I separate the code of page to make it clearly.

I'm so tired and tomorrow is the presintation day. please help !

Head of page: first part of html2pdf

<?php ob_start(); ?>

2end line:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Facture</title>
    </head>

Body part

    <body>
        <?php
        include_once"./html2pdf/html2pdf.class.php";
        include_once"Traitement.php";
        session_start();
        $numcom="";$datecom="";$dateliv="";
        $cur=Traitement::commande($_GET['numcom'],$_SESSION['log']);
        if($cur->rowCount()<>0){
            while($row=$cur->fetch()){
                $numcom=$row[0];
                $datecom=$row[1];
                $dateliv=$row[2];
            }
        }
            $cur->closeCursor();
      ?>
            <div style='margin:0 0 0 0;text-align:left;'>
                <h3 style='margin:1% 0 0 0;display:inline-block'><span style='display:inline-block'>Client: </span><span style='display:inline-block'>".$_SESSION['log']."</span></h3>
                <h3 style='margin:1% 0 0 0'><span style='display:inline-block'>Code de commande: </span><span style='display:inline-block'>".$numcom."</span></h3>
            </div>
            <div style='margin:0 5% 0 0;text-align:left;padding:0 0 0 2%'>
                <h3 style='margin:1% 0 0 0'><span style='display:inline-block'>Date de commande: </span><span style='display:inline-block'>".$datecom."</span></h3>
                <h3 style='margin:1% 0 0 0'><span style='display:inline-block'>Date de livraison: </span><span style='display:inline-'>".$dateliv."</span></h3>
            </div>
        <table style='margin:5% 0 0 0;border:1px solid white'>
            <thead style='background:white;color:black'>
                <th align='center' style=''>Article</th>
                <th align='center' style=''>Quantité</th>
                <th align='center' style=''>Prix unitaire</th>
                <th align='center' style=''>Sous-Total</th>
            </thead>
            <tbody>
                <?php
                $cur=Traitement::achatparcommnde($_GET['numcom'],$_SESSION['log']);
                if($cur->rowCount()<>0){
                    while($row=$cur->fetch()){
                        $nom=$row[0];
                        $prixunitaire=$row[1];
                        $qte=$row[2];
                        $soustotal=$row[1]*$row[2];
                        echo"<td align='center' style=''>$nom</td>
                        <td align='center' style=''>$qte</td>
                        <td align='center' style=''>$prixunitaire Dhs</td>
                        <td align='center' style=''>$soustotal Dhs</td>";
                    }
                }
                    ?>
            </tbody>
            </table>
            <h2 id='foot' align='center' style=''>Merci pour votre achat</h2>
    </body>
</html>

Bottom of page: second part of html2pdf

<?php
$contents=ob_get_clean();
    $pdf=new HTML2PDF("P","A4","fr");
    $pdf->writeHTML($contents);
    $pdf->OutPut("facture.pdf");
?>

Solution

  • Your "Notice: bla bla" errors are the one causing you trouble, as they're output. Indeed they're not errors just warnings that can be ignored.

    Disable PHP Notice errors with:

    error_reporting(E_ALL & ~E_NOTICE);
    

    at the start of your PHP (before your require() or include() of html2pdf module).