Time to Read: 1 minute

Please follow these Simple & Quick Steps to Export your HTML Content to Doc in CakePHP:-

Step#1: Build the <HTML> content dynamically according to the requirement.

Step#2: Store the <HTML>content in a variable (Example: $ExportContent)

$ExportContent = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>

<body>

<h2>Mark sheet Table</h2>

<table style="width:100%" border="1" cellspacing="0">
<tbody><tr> 
       <th>Number</th>
       <th>First Name</th>
       <th>Last Name</th>
       <th>Points</th>
</tr>
<tr> 
       <td align="center">1</td> 
       <td>Eve</td> 
       <td>Jackson</td> 
       <td align="right">94</td>
</tr>
<tr> 
       <td align="center">2</td> 
       <td>John</td> 
       <td>Doe</td> 
       <td align="right">80</td>
</tr>
</tbody></table>

</body>
</html>
';

Step#3: After building the content pass appropriate headers to convert this <HTML>content to Doc

header("Content-type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

Step#4: Provide file name to “Content-Disposition” header that we want to give the doc file name

header("Content-Disposition: attachment; Filename=myfile.doc");
echo $ExportContent;

Did you find above tips useful? Feel free to drop in your suggestion…

Interested in anything on CakePHP? Let’s have a quick discussion.