In this tutorial, you can learn about how to convert text or export HTML to Microsoft Word File format by using PHP programming. If you develop any web application and in that application, you want to add one feature like you want to create Word Document from your PHP Web Application then at that time you can use this PHP Tutorial Script for Export HTML text to Microsoft Word. You can also format the word file from HTML code by putting a simple HTML tag and you can also use inline style sheet for change color, increase the font size, etc.
Step 1)
Create a new PHP file like “example.php” and put the below code inside that file with PHP starting and ending code.
header(“Content-type: application/vnd.ms-word”);
header(“Content-Disposition: attachment; Filename=SaveAsWordDoc.doc”);
$html = "<meta charset="utf-8">
<title>A simple, clean, and responsive HTML export to Ms Word</title>
<style>body {height: 297mm;width: 210mm;margin: 0 auto;font-size: 10pt;line-height: 10pt;font-family: Times New Roman, Arial, sans-serif;color: #000;}/style>
<form>
<table style="width:210mm;" width="100%" cellspacing="0" cellpadding="5" border="0">
<tbody>
<tr>
<td>REF No: 123456</td>
</tr>
<tr>
<td><strong>Date: 24 October 2019 </strong></td>
</tr>
<tr>
<td>
Dear USER NAME</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<table width="100%" cellspacing="0" cellpadding="5" border="0">
<tbody>
<tr>
<td width="30">
<div style="border: solid 1px #000; margin: 2pt; float: left;"> </div></td>
<td> USER data.</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>";
echo $html;
Step 2)
Now you can run this file to your browser. you can get your HTML or text to Ms-word file. this will support in each and every browser.