Convert HTML to Ms Word Using Php Script

Convert HTML to Ms Word Using Php Script

Convert HTML to Ms Word Using Php Script

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 a Word document from your PHP web application, then at that time you can use this PHP tutorial script for exporting 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 an inline style sheet to change the 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;">&amp;</div></td>
<td>USER data.</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</form>";
echo $html;

Step 2)

Now you can run this file in your browser. You can get your HTML or text in an MS Word file. This will be supported in each and every browser.

0 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *