PHP - Export HTML Text To MS Word

In this tutorial we will create a Export HTML Text To MS Word. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding...

Before we get started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7. <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - Export HTML Text To MS Word</h3 >
  16.                 <hr style="border-top:1px dotted #ccc;"/>
  17.                 <div class ="col-md-2"></div>
  18.                 <div class ="col-md-8">
  19.                         <form action="export.php" method="POST">
  20.                                 <div class="form-group">
  21.                                         <h4>Title</h4>
  22.                                         <input type="text" name="title" class="form-control"/>
  23.                                 </div>
  24.                                 <br />
  25.                                 <div class="form-group">
  26.                                         <h4>Content</h4>
  27.                                         <textarea class="form-control" name="content" style="resize:none; height:250px;"></textarea>
  28.                                 </div>
  29.                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-export"></span> Export as MS Word</button>
  30.                         </form>
  31.                 </div>
  32.         </div>
  33. </body>
  34. </html>

Creating Export Function

This code contains the main function of the application. This will get that content of the data inputs and then will force to export as an MS Word Document. To do that just copy and write this block of codes inside the text editor, then save it as export.php.
  1. <?php
  2.        
  3.         if(ISSET($_POST['save'])){
  4.                 if(!empty($_POST['title']) && !empty($_POST['content'])){      
  5.                         $output ="
  6.                                 <h1>".$_POST['title']."</h1>
  7.                                 <p>".$_POST['content']."</p>
  8.                         ";
  9.                        
  10.                         $date = date("Y-m-d");
  11.                        
  12.                         header("Content-Type: application/vnd.msword");
  13.                         header("Expires: 0");//no-cache
  14.                         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//no-cache
  15.                         header("content-disposition: attachment;filename=".$date.".doc");
  16.                        
  17.                         echo "<html>";
  18.                         echo $output;
  19.                         echo "</html>";
  20.                        
  21.                 }else{
  22.                         echo "<script>alert('Please complete the required field!')</script>";
  23.                         echo "<script>window.location='index.php'</script>";
  24.                 }
  25.         };
  26. ?>
There you have it we successfully created Export HTML Text To MS Word. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!

Comments

PHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word FilePHP Script for convert or export HTML text to MS Word File

Add new comment