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.
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"];
}
}
?>
what ever we write in description its showing as it is with all html tags.
ReplyDeleteBro code is not working please fix
ReplyDeletewhat about images? the images is not loading in word document
ReplyDeleteI 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
ReplyDeletewrong code
ReplyDeleteAll I see is HTML format in downloaded doc file! Please help me out.
ReplyDeletewill this work with PHPWord?
ReplyDeletesir when i run the file it's still a html tags in word
ReplyDeleteerror not working
ReplyDeleteit 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
ReplyDeletethanks Bro
ReplyDeleteBy any chance: do you have also manage to change for example the background color in the word document?
ReplyDelete