1

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.

4

1 回答 1

2

您不能只回显 HTML 并希望它会神奇地生成 PDF 二进制文件,因为您发送了 PDF 标头。查看 PHP PDF 库 ( http://php.net/manual/en/book.pdf.php ) 或其他基于 PHP 的库以创建 PDF。

同样,我很震惊输出带有 Excel 标头的 HTML 会创建一个可用的 Excel 文件。这必须通过与 Excel 的幸运兼容性才能起作用。如果您想以更可靠的方式创建 Excel 文件,您应该使用真正的 Excel 库(或创建与 Excel 兼容的 CSV 文件)。

于 2013-02-19T01:30:53.840 回答