Generally, the export feature is used to download HTML file as MS Word document file and save it for offline use. The conversion of HTML file into word document using PHP code is the most requested feature for developers. Microsoft Word or Doc (.doc) format is ideal for exporting HTML content in a file. The export to doc functionality can be easily implemented using php code.
The server-side export to doc functionality makes the web application user-friendly. The user can export all data of the web page content into doc format. In this tutorial, we will show you How to export HTML file to MS Word document (.doc) using php code.
There is an easy step-by-step coding instruction available for developers, who want to convert HTML file to MS word document(.doc) using php code.
It can all be done by PHP headers
#The coding starts with including the application vnd.ms-word
2 3 4 5 6 7 8 9 10 11 12 |
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"); |
Export HTML to MS Word Document
Step-1: HTML to word file in php involves designing an HTML form including a name index.php to get inputs
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 |
<!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>How to export HTML file to MS Word document (.doc) using php code</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="<strong>export-to-word.php</strong>"> <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="exportToWord">Export Doc</button> </form> </body> </html> |
SEE ALSO: Export HTML Table Data to Excel using JavaScript
Step-2: Receive the data inputs passed from HTML form by means of PHP post method.
For this purpose create a page named “export-to-word.php”
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php if(isset($_POST['exportToWord'])){ header("Content-type: application/vnd.ms-word"); ?> # replace Wordfile.doc with whatever you want the filename to default to header("Content-Disposition: attachment;Filename=exporttoword.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>";; } ?> |
Conclusion
Our example code will help you to easily export HTML file to MS Word document (.doc) using php code.
Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request
Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co