phpms-word.docexport-to-word

Export PHP page to .doc


I'm having a problem with it when I tried it. It just load the php page but doesn't give me a download of .doc

Here is my code from the first page to get request to export to MSWord Thank you so much!!

<a href="admweeklyprint.php">
<button class="btn btn-primary">Export to MsWord</button>
</a>

And here's the code of what I'm trying to export

<?php
include ('database_connection.php');


date_default_timezone_set('Asia/Manila');  

$currentdate = date("y-m-d");
$currentTime = date("g:i:s a");

$sqldata ="SELECT *
from tbl_orders c, tbl_orderitems p WHERE dateordered > NOW() - INTERVAL 7 DAY  AND c.orderID=p.orderID and paymentstatus='Complete' order by c.orderID ASC";

$getdata=mysql_query($sqldata);

?>


<html>
<head>
 <link href="bootstrap/css/bootstrap.css" rel="stylesheet">
    <link id="switch_style" href="bootstrap/css/united/bootstrap.css" rel="stylesheet">
    <link href="css/main.css" rel="stylesheet">
    <link href="css/jquery.rating.css" rel="stylesheet">


    </head>

    <body>
     <center> <h1><img src="mvramPDF.PNG" alt="Logo" /> </h1> 
    <h2>Sale Report </h2>


     </center>


<center>
    <table class="table table-bordered table-striped">
          <thead>
              <tr>
                <th>Order ID</th>
                <th>Product ID</th>
                <th>Model</th>
                <th>Quantity</th>
                <th>Price</th>
                <th>Total</th>
                <th>Status</th>
                <th>Date</th>
                <th>Total Price with Tax</th>
              </tr>
            </thead>

<?php

$docname = "records_" . $currentdate . ".doc";


           $x = 1;



       $totalFinal=0;    

 $LastorderID=0;                  
    while($data=  mysql_fetch_array($getdata))
    {
             $orderID = $data["orderID"];
             $totalprice = $data["totalprice"];
             $status = $data["paymentstatus"];
             $dordered = $data["dateordered"];    
            $prodID = $data["productID"];
            $model = $data["model"];
            $price = $data["price"];
            $quantity = $data["quantity"];            



         $tax = $totalprice * .12;    
   $overall = $tax + $totalprice;


            $total=$data['price']*$quantity;



    header("Content-type: application/vnd.ms-word");
    header("Content-Disposition: attachment;Filename=$docname");
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";     
     echo'<tr>
           <tbody>
              <td>'.(($LastorderID==$orderID) ? "&nbsp;" : $orderID).'</td>  
               <td>'.$prodID.'</td>
               <td>'.$model.'</td>
               <td>'.$quantity.'</td>
               <td>&#8369;'.$price.'</td>
               <td>&#8369;'.$total.'</td>
               <td>'.(($LastorderID==$orderID) ? "&nbsp;" : $status).'</td>
               <td>'.(($LastorderID==$orderID) ? "&nbsp;" : $dordered).'</td>
              <td>'.(($LastorderID==$orderID) ? "&nbsp;" : "&#8369;  $overall").'</td>  
                </tr>          
           ';    

        if($LastorderID!=$orderID) $totalFinal+=$overall;   



 $LastorderID=$orderID;
        $x++;     

    }



echo '              
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td><strong>Total Sales: &#8369;'.$totalFinal.'</strong></td>
              </tr>         

</tbody>
         </table>';


 ?>

 </table>
</center>
</body>
</html>

Thank you so much!!


Solution

  • First, you have to convert it into a .doc with a converter, it's more easy to export a pdf with tcpdf or some other lib.

    Second if you want to force a download or display a file you have to send the header() before you echo the first line of the html <html>.

    http://www.php.net/manual/en/function.header.php