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

index.php


 <!DOCTYPE html>  
 <html>  
      <head>  
           <title>Webslesson Tutorial | PHP Script for convert or export HTML text to MS Word File</title>  
           <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
           <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
           <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
      </head>  
      <body>  
           <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" />  
                </form>  
           </div>  
      </body>  
 </html>  

export.php


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

0 comments:

Post a Comment