Loading.......
Loading...

Convert HTML to MS Word Document using PHP

Posted on 24th October, 2021
CONVERT HTML TO MS WORD HTML TO WORD PHP CONVERT HTML TO MICROSOFTWORD
This tutorial teaches you how to convert html content in an easy way to Microsoft Word document using php

The tutorial is beneficial to as many who may have designed an application that requires downloading some content in Microsoft Word format.

There are two main pages created in this tutorial namely the index.php page which is made up of html form and the export.php page which contains the php codes that converts the data sent from the index.php page into a Microsoft Word document.

PHP SCRIPT TO DO THE CONVERSION

<?php
// Check to be sure that the only request we can process is a POST REQUEST
if(isset($_SERVER["REQUEST_METHOD"]) && strip_tags($_SERVER["REQUEST_METHOD"]) == strip_tags("POST"))
{
	if(isset($_POST['create_ms_word']))
	{
		// If all fields in index.php page are filled, procceed to process the data
		if(isset($_POST['topic']) && !empty($_POST['topic']) && isset($_POST['content']) && !empty($_POST['content'])) 
		{	
			// Below are data sent from the index.php page
			$output ="
				<h1>".$_POST['topic']."</h1>
				<p>".nl2br($_POST['content'])."</p>
			";
			
			$filename = date("d-m-Y").'.doc'; // This is the name of our MS Word file that will be created
			
			header("Cache-Control: ");// leave blank to avoid IE errors 
            @header("Pragma: ");// leave blank to avoid IE errors 
			header("Expires: 0");
			header("Content-Type: application/vnd.msword"); // Creates the MS Word Document
			header("content-disposition: attachment;filename=".$filename); // Downloads the document
			
			echo "<html>"; // Starts HTML 
			echo $output; // The body content of our MS Word Document goes in here
			echo "</html>"; // Ends the HTML
			
		}
		else
		{
			// If all fields in index.php page are not filled, alert an error message below
			echo "<script>alert('Please fill in all the required fields')</script>"; 
			echo "<script>window.location='index.php'</script>";
		}
	}
	else
	{
		die('Sorry, no proper data was passed');
	}
}
else 
{
	// Deny access if the request brought to this page is not a POST REQUEST
	die('Access Denied');
}
?>
The script is very easy to understand and customize with programming code comments to ease usability.
To see the system in action, please click on the Live Demo button below and click on the Download button to download the script if you like it.

Thank You!
The Vasplus Team.
Post Comment
Press Enter to send
No comment yet.

Submit your Job or Project Today!

We can help you turn your idea into reality, take over your existing project, or extend your current development team.

Submit your idea job or project below and we will follow up with you shortly.

OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Please Wait....
Please Wait...