How to convert /export HTML file to MS Word document (.doc) using php code?

 

The conversion of HTML file into word document using PHP code is the most requested functionalities. There is an easy step-by-step coding instruction available for beginners, who want to know how to convert HTML file to MS word document(.doc) using php code.

It can all be done by means of PHP headers

#The coding starts with including the application vnd.ms-word

header("Content-type: application/vnd.ms-word");

# And then replacing wordfile.doc with what you need the filename to default to.

header("Content-Disposition: attachment;Filename=Wordfile.doc");

header("Pragma: no-cache");

header("Expires: 0");

The first step in HTML to word document conversion in php involves designing an HTML form including a name index.php to get inputs

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

<style>

h1 {

height: 50px;

width: 100%;

font-size: 18px;

background: #18aa8d;

color: white;

line-height: 150%;

border-radius: 3px 3px 0 0;

box-shadow: 0 2px 5px 1px rgba(0, 0, 0, 0.2);

}

form {

box-sizing: border-box;

width: 500px;

margin: 100px auto 0;

box-shadow: 2px 2px 5px 1px rgba(0, 0, 0, 0.2);

padding-bottom: 40px;

border-radius: 3px;

}

form h1 {

box-sizing: border-box;

padding: 20px;

}

.text-field {

margin: 40px 25px;

width: 80%;

display: block;

border: none;

padding: 10px 0;

-webkit-transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);

transition: all 0.3s cubic-bezier(0.64, 0.09, 0.08, 1);

background: -webkit-linear-gradient(top, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);

background: linear-gradient(to bottom, rgba(255, 255, 255, 0) 96%, #1abc9c 4%);

background-position: -100% 0;

background-size: 100% 100%;

background-repeat: no-repeat;

color: #0e6252;

}

.text-field:focus, .text-field:valid {

box-shadow: none;

outline: none;

background-position: 0 0;

border-bottom: solid 1px #1abc9c;

}

button {

float:right;

border: none;

background: #1abc9c;

cursor: pointer;

border-radius: 3px;

padding: 6px;

width: 40px;

color: white;

margin-right: 40px;

box-shadow: 0 3px 6px 0 rgba(0, 0, 0, 0.2);

}

button:hover {

-webkit-transform: translateY(-3px);

-ms-transform: translateY(-3px);

transform: translateY(-3px);

box-shadow: 0 6px 6px 0 rgba(0, 0, 0, 0.2);

} 
.text-field-1 {

background:none;

}

</style>

</head>

<body>

<form method="POST" action="word.php">

<h1>Form</h1>

<input placeholder="Heading" name="heading" class="text-field" type="text" required="">

<textarea rows="3" cols="6"  name="content" placeholder="content"  class="text-field text-field-1"></textarea>

<button type="submit" name="word">Doc</button> &nbsp;&nbsp;&nbsp;

</form>

</body>

</html>

 The second step deals with making a doc conversion php page named word.php using a set of code. Receive the data inputs passed from HTML form by means of PHP post method.

Using a post method, get dynamic inputs to export doc file from user

Eg:

<?php $_POST['heading']; ?>

# conversion Code word.php

<?php

if(isset($_POST['word'])){

header("Content-type: application/vnd.ms-word"); ?>

# replace Wordfile.doc with whatever you want the filename to default to

header("Content-Disposition: attachment;Filename=Wordfile.doc");

header("Pragma: no-cache");

header("Expires: 0");

$current_date = date('d-m-Y');
$heading = $_POST['heading'];

$content = $_POST['content'];

echo "<div style='font-size: 1em; line-height: 1.6em; color: #4E6CA3; padding:10px;' align='right'>Report Date: $current_date</div>";

echo "<div style='font-size: 1em; line-height: 1.6em; color: #4E6CA3; padding:10px;' align='left'>$heading</div>";

echo "<div style='font-size: 1em; line-height: 1.6em; color: #4E6CA3; padding:10px;' align='left'>$content</div>";;

}

?>

Just follow the aforementioned instructions whenever you wonder

How to export HTML file to MS word(.doc) using php code

This is the simplest technique for conversion of HTML into word document. You need to just copy and paste the coding part to execute the conversion function and include additional inputs according to your requirement.

4 Responses so far.

  1. Thanraj says:
    Thanks for the blog. This explained in detail of converting the HTML file to MS Word document using php code. It helped me a lot.
  2. Jim says:
    Can we convert HTML file to MS Word document without using PHP code?
  3. Raja Parthiban says:
    This blog really helped me to convert and export the html file to ms word document. i was finding a solution and got the right one from your blog. thanks for it.
  4. shreya says:
    Good one. I have a doubt.. only php code can be used to convert html file to msword document ?

LEAVE A COMMENT