Friday, 12 August 2016

Convert or Export HTML text to MS Word with PHP Script

In this PHP Tutorial post we are going to learn how to convert or export html text or file to Microsoft Word File format by using PHP programming Language. If you are 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. For this I have create one HTML form with two field for entering heading text and description in html format. In PHP script I have used simple PHP HTTP header() function that sends a raw HTTP header to a client. You can also format the word file from html code by putting simple html tag and you can also use inline stylesheet for change color, increase font size etc. The export of HTML file to Microsoft word document by using PHP script is the highest required functionality. Here in video tutorial, I have describe simple step by step with php coding guidance for beginners, who want to learn how to create Microsoft Word Document(.doc) from HTML file by using php script.

Convert or Export HTML text to MS Word with PHP Script

Source Code


 <!DOCTYPE html>  
           <title>Webslesson Tutorial | PHP Script for convert or export HTML text to MS Word File</title>  
           <link rel="stylesheet" href="" />  
           <script src=""></script>  
           <script src=""></script>  
           <br /><br />  
           <div class="container" style="width:800px;">  
                <br />  
                <h3 align="center">PHP Script for convert or export HTML text to MS Word File</h3>  
                <br />  
                <form method="post" action="export.php">  
                     <label>Enter Title</label>  
                     <input type="text" name="heading" class="form-control" />  
                     <br />  
                     <label>Enter Description in HTML Formate</label>  
                     <textarea name="description" class="form-control" rows="10"></textarea>  
                     <br />  
                     <input type="submit" name="create_word" class="btn btn-info" value="Export to Word" />  


      if(empty($_POST["heading"]) || empty($_POST["description"]))  
           echo '<script>alert("Both Fields are required")</script>';  
           echo '<script>window.location = "index.php"</script>';  
           header("Content-type: application/");  
           header("Content-Disposition: attachment;Filename=".rand().".doc");  
           header("Pragma: no-cache");  
           header("Expires: 0");  
           echo '<h1>'.$_POST["heading"].'</h1>';  
           echo $_POST["description"];  


  1. what about images? the images is not loading in word document

  2. I tried this method and word file was generated but when i opened the word file there was no proper page layout.So how to generate word file from html with proper page layout

  3. it doesnt work for me it shows Cannot modify header information - headers already sent by (output started at Z:\home\localhost\www\diplom\export.php:1) in Z:\home\localhost\www\diplom\export.php on line 12 and on line and online and online