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.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
<link rel
="stylesheet" type
="text/css" href
="css/bootstrap.css"/>
</head>
<body>
<nav class="navbar navbar-default">
<div class="container-fluid">
<a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
</div>
</nav>
<div class="col-md-3"></div>
<div class="col-md-6 well">
<h3 class="text-primary">PHP - Export HTML Text To MS Word</h3 >
<hr style="border-top:1px dotted #ccc;"/>
<div class ="col-md-2"></div>
<div class ="col-md-8">
<form action="export.php" method="POST">
<div class="form-group">
<h4>Title</h4>
<input type="text" name="title" class="form-control"/>
</div>
<br />
<div class="form-group">
<h4>Content</h4>
<textarea class="form-control" name="content" style="resize:none; height:250px;"></textarea>
</div>
<button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-export"></span> Export as MS Word</button>
</form>
</div>
</div>
</body>
</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.
<?php
if(ISSET($_POST['save'])){
if(!empty($_POST['title']) && !empty($_POST['content'])){
$output ="
<h1>".$_POST['title']."</h1>
<p>".$_POST['content']."</p>
";
header("Content-Type: application/vnd.msword");
header("Expires: 0");//no-cache
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");//no-cache
header("content-disposition: attachment;filename=".$date.".doc");
echo "<html>";
echo $output;
echo "</html>";
}else{
echo "<script>alert('Please complete the required field!')</script>";
echo "<script>window.location='index.php'</script>";
}
};
?>
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!!!