xjshiya
xjshiya

Reputation: 925

Output PDF file using header

I can output a PHP table in an excel file using this code:

<?php 

require("aacfs.php");

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=Cancelled Flight Summary_sortbydate.xls");
header("Pragma: no-cache");
header("Expires: 0");

echo "<center>Ayala Aviation Corporation</center>";
echo "<center><b>Cancelled Flight Summary</b></center>";
echo "<center>For the period ________</center></br></br>";

$result=mysql_query("select * from reservation where status='Cancelled' order by bdate");   
echo "<center></br></br><table border=1 class='imagetable'><tr>
    <th>Flight Date</th> 
    <th>Client</th>
    <th>Group Code</th>
    <th>Itinerary</th>
    <th>ETD</th>
    <th>Aircraft</th>
    <th>ETA</th>
    <th>Reservation No.</th>
    <th>Last Status</th>
    <th>Remarks</th>
    <th>Reserved By</th>
    </tr>";
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
    $rvno=$row['reservno'];
    $yr=(string) date("Y");
    $rn=$yr."-".$rvno;
    echo"<tr><td>".$row['fdate']."</td><td>".$row['cliename']."</td><td>".$row['grpcode']."</td><td>".$row['itinerary']."</td><td>".$row['etd']."</td><td>".$row['acode']."</td><td>".$row['eta']."</td><td>".$rn."</td><td>".$row['lstatus']."</td><td>".$row['remarks']."</td><td>".$row['adminid']."</td></tr>";

}
}
else
    { ?>
        <tr><td colspan="11" style="text-align:center; color:#FF0000; font-size:16px;">*No Data Available!*</td></tr>
        <?php
    }
        ?><?php



?>

I tried this,

<?php 

require("aacfs.php");

header("Content-type: application/pdf");
header("Content-Disposition: attachment; filename=Cancelled Flight Summary_sortbydate.pdf");
header("Pragma: no-cache");
header("Expires: 0");

echo "<center>Ayala Aviation Corporation</center>";
echo "<center><b>Cancelled Flight Summary</b></center>";
echo "<center>For the period ________</center></br></br>";

$result=mysql_query("select * from reservation where status='Cancelled' order by bdate");   
echo "<center></br></br><table border=1 class='imagetable'><tr>
    <th>Flight Date</th> 
    <th>Client</th>
    <th>Group Code</th>
    <th>Itinerary</th>
    <th>ETD</th>
    <th>Aircraft</th>
    <th>ETA</th>
    <th>Reservation No.</th>
    <th>Last Status</th>
    <th>Remarks</th>
    <th>Reserved By</th>
    </tr>";
if(mysql_num_rows($result)>0)
{
while($row=mysql_fetch_array($result))
{
    $rvno=$row['reservno'];
    $yr=(string) date("Y");
    $rn=$yr."-".$rvno;
    echo"<tr><td>".$row['fdate']."</td><td>".$row['cliename']."</td><td>".$row['grpcode']."</td><td>".$row['itinerary']."</td><td>".$row['etd']."</td><td>".$row['acode']."</td><td>".$row['eta']."</td><td>".$rn."</td><td>".$row['lstatus']."</td><td>".$row['remarks']."</td><td>".$row['adminid']."</td></tr>";

}
}
else
    { ?>
        <tr><td colspan="11" style="text-align:center; color:#FF0000; font-size:16px;">*No Data Available!*</td></tr>
        <?php
    }
        ?><?php



?>

And it does download a PDF file but it can't be opened.

Is it really impossible to output the PHP table in a PDF file the same way it does in the above code? Or I'm just doing something wrong on my 2nd code? This way is the easiest for me so I'm eager to know if it is impossible (if it is, then why?) or possible. And please point me out to an easy alternative to achieve this in case it is not possible, since I'm new to generating external files through PHP. Any help would do. Thanks.

Upvotes: 1

Views: 10674

Answers (1)

Mike Brant
Mike Brant

Reputation: 71422

You can't just echo out HTML and hope it will magically make a PDF binary because you sent a PDF header. Look at the PHP PDF library (http://php.net/manual/en/book.pdf.php) or other PHP-based library for creating PDF's.

Similarly, I am shocked that outputting HTML like that with an Excel header will create a usable Excel file. It must be through lucky compatibility with Excel that this would work. You should use a real Excel library (or create a CSV file compatible with Excel) if you want to create Excel files in a more reliable fashion.

Upvotes: 2

Related Questions