Export HTML Form To MS Word Using PHP Source Code

In this tutorial we will create a Export HTML Form To MS Word using PHP. This code will export the text content to MS word when user click the export button. The code use a PHP POST to call a specific method that export the HTML content into readable MS word format using header() function with a different set of parameters. This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

Getting 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. And, 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">Export HTML Form To MS Word Using PHP Source Code</h3 >
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class ="col-md-6">
  18. <form action="export.php" method="POST">
  19. <div class="form-group">
  20. <h4>Title</h4>
  21. <input type="text" name="title" class="form-control"/>
  22. </div>
  23. <br />
  24. <div class="form-group">
  25. <h4>Content</h4>
  26. <textarea class="form-control" name="content" style="resize:none; height:250px;"></textarea>
  27. </div>
  28. <button name="export" class="btn btn-primary"><span class="glyphicon glyphicon-export"></span> Export as MS Word</button>
  29. </form>
  30. </div>
  31. </div>
  32. </body>
  33. </html>

Creating the Main Function

This code contains the main function of the application. This code will export the text in to MS word when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as export.php
  1. <?php
  2.  
  3. if(ISSET($_POST['export'])){
  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("Ymd");
  11.  
  12. header("Content-Type: application/vnd.msword");
  13. header("Expires: 0");
  14. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  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 Form To MS Word using PHP. 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!

Add new comment