简体   繁体   中英

using html2pdf api to save mysql data generated in php and html table

I am trying to use html2pdf to create pdf of the php file which contains a table in vich th data from mysql database is fetched and is generated by using while loop, but wen I try to run the php file it says the follwoing error

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

can anyone help me in solving the problem please? here is my code:

<?php

session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
//error_reporting(0);
/*
  Author : yaser kazi
  Description : This File Contain Additions and Editing part for the Paxes
 */

if (!isset($_SESSION["username"])) {
    header("location: unauthorize_access.html");
    exit();
}


if (isset($_GET["cityprin"])) {
    
    mysql_connect("localhost", "root", '') or die(mysql_error());
    mysql_select_db('webapp') or die(mysql_error());

    
    $get = mysql_query("SELECT code, `name` FROM city ORDER BY `name`") or die(mysql_error());

    

    
    $ctr = 0;
    
    ini_set("memory_limit", "50M");
    ob_start();
    ?>   
<style type="text/css" media="all">
                <!--
                body {
                    margin:0px;
                    font-family:Verdana, Arial, Helvetica, sans-serif;
                    font-size:12px;
                }

                span {
                    font-size:14px;
                }

                td {
                    width: 100mm;
                    max-width: 100mm;
                    height: 33mm;
                    max-height:33mm;
                    top:0mm;
                }
                table {
                    border-collapse:collapse;
                }
                -->
            </style>
            
    <?php

            echo "<table border=\"1\" align=\"right\" style =\"width: 205mm;\">";
            echo "<th>Srno</th>";
            echo "<th>Code</th>";
            echo "<th>Name</th>";
    
    while ($city = mysql_fetch_array($get)) {
            $ctr++;
            echo "<td style=\"width: 100mm; max-width: 100mm; height:33mm; padding-left:1mm;\" valign=\"right\">".$ctr."</td>";
            echo "<td style=\"width: 100mm; max-width: 100mm; height:33mm; padding-left:7mm;\" valign=\"right\">".$city['code']."</td>";
            echo "<td style=\"width: 100mm; max-width: 100mm; height:33mm; padding-left:7mm;\" valign=\"right\">".$city['name']."</td>";
              }
    echo "</table>";
} // Submit Tag Close

?>
                        
            <?php
            $content = ob_get_clean();
// conversion HTML => PDF
            require_once('html2pdf/html2pdf.class.php');
            try {
                $html2pdf = new HTML2PDF('P', 'A4', 'EN', false, 'ISO-8859-15', array("5mm", "13mm", "0mm", "0mm"));
                $html2pdf->pdf->SetAuthor('CepheiSys');
                $html2pdf->pdf->SetTitle('City_list');
//        $html2pdf->pdf->SetSubject('HTML2PDF Wiki');
                //       $html2pdf->pdf->SetKeywords('HTML2PDF, TCPDF, example, wiki');
                $html2pdf->setDefaultFont('Arial');
                $html2pdf->writeHTML($content);
                $html2pdf->Output('City_list.pdf');
            } catch (HTML2PDF_exception $e) {
                echo $e;
            }
            ?>

You've got some sort of output before you're outputting those headers it seems. There are a few things to check:

  • Do you have any echo statement (or similar) earlier in the code?
  • Is there any sort of space before that PHP tag? A new line or tab would trigger output being sent to the browser
  • Have you turned off the BOM feature in your editor, as this can cause problems with UTF8 documents and trigger output to be sent to the browser
  • Check the error logs, there should be a line number to go with that error. It should give you an idea of where to start looking

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM