The export functionality to the document on the server is extremely useful for converting HTML content to MS word document and download it as a.docx file. The MS word document can be quickly generated with HTML content via PHP.
Converting HTML to MS Word Document used primarily in the web application to generate.doc/.docx file with dynamic HTML data. The most popular file format is the Microsoft word document for exporting dynamic content offline. The JavaScript can be easily used to export HTML content to MS Word functionality. However, if you want to convert dynamic content to Doc, the interaction on the server-side is required. In this tutorial, we will show you step by step process to convert HTML to MS word file using PHP.
Step-1: Create html_to_doc.php file
In the html_to_doc.php file, we will write a custom class library code that helps to generate MS Word file and include the HTML content in Word document using PHP.
title = '';
$this->htmlHead = '';
$this->htmlBody = '';
}
/**
* Set the document file name
*
* @param String $docfile
*/
function setDocFileName($docfile){
$this->docFile = $docfile;
if(!preg_match("/\.doc$/i",$this->docFile) && !preg_match("/\.docx$/i",$this->docFile)){
$this->docFile .= '.doc';
}
return;
}
/**
* Set the document title
*
* @param String $title
*/
function setTitle($title){
$this->title = $title;
}
/**
* Return header of MS Doc
*
* @return String
*/
function getHeader(){
$return = '
'.$this->title.'
'.$this->htmlHead.'
';
return $return;
}
/**
* Return Document footer
*
* @return String
*/
function getFotter(){
return "";
}
/**
* Create The MS Word Document from given HTML
*
* @param String $html :: HTML Content or HTML File Name like path/to/html/file.html
* @param String $file :: Document File Name
* @param Boolean $download :: Wheather to download the file or save the file
* @return boolean
*/
function createDoc($html, $file, $download = false){
if(is_file($html)){
$html = @file_get_contents($html);
}
$this->_parseHtml($html);
$this->setDocFileName($file);
$doc = $this->getHeader();
$doc .= $this->title;
$doc .= $this->htmlBody;
$doc .= $this->getFotter();
if($download){
@header("Cache-Control: ");// leave blank to avoid IE errors
@header("Pragma: ");// leave blank to avoid IE errors
@header("Content-type: application/octet-stream");
@header("Content-Disposition: attachment; filename=\"$this->docFile\"");
echo $doc;
return true;
}else {
return $this->write_file($this->docFile, $doc);
}
}
/**
* Parse the html and remove part if present into html
*
* @param String $html
* @return void
* @access Private
*/
function _parseHtml($html){
$html = preg_replace("//ims", "", $html);
$html = preg_replace("/((.|\n)*?)<\/script>/ims", "", $html);
preg_match("/((.|\n)*?)<\/head>/ims", $html, $matches);
$head = !empty($matches[1])?$matches[1]:'';
preg_match("/((.|\n)*?)<\/title>/ims", $head, $matches);
$this->title = !empty($matches[1])?$matches[1]:'';
$html = preg_replace("/<head>((.|\n)*?)<\/head>/ims", "", $html);
$head = preg_replace("/<title>((.|\n)*?)<\/title>/ims", "", $head);
$head = preg_replace("/<\/?head>/ims", "", $head);
$html = preg_replace("/<\/?body((.|\n)*?)>/ims", "", $html);
$this->htmlHead = $head;
$this->htmlBody = $html;
return;
}
/**
* Write the content in the file
*
* @param String $file :: File name to be save
* @param String $content :: Content to be write
* @param [Optional] String $mode :: Write Mode
* @return void
* @access boolean True on success else false
*/
function write_file($file, $content, $mode = "w"){
$fp = @fopen($file, $mode);
if(!is_resource($fp)){
return false;
}
fwrite($fp, $content);
fclose($fp);
return true;
}
}
</pre>
<p>In the above code,</p>
<ul class="wp-block-list"><li><strong>setDocFileName()</strong> – Set document file name.</li><li> <strong>setTitle()</strong> – Set document title.</li><li> <strong>getHeader()</strong> – Create header section of the document.</li><li> <strong>getFotter()</strong> – Create footer section of the document.</li><li> <strong>createDoc()</strong> – Create word document in .dcox format.</li><li><strong> _parseHtml()</strong> – Parse and filter HTML from source.</li><li> <strong>write_file()</strong> – Insert content in the word file.</li></ul>
<h2 class="wp-block-heading">Step-2: Create index.php file</h2>
<p>Copy and paste the following code in the <strong>index.php</strong> file. Here we will load the <strong>html_to_doc.php</strong> library and initialize the class. After this, we will specify custom HTML content to convert into Word document. Call the <strong>createDoc()</strong> function to convert HTML content to Word document.</p>
<pre>
<?php
include_once 'html_to_doc.php';
// Initialize class
$htmltodoc = new HTML_TO_DOC();
$htmlContent = '<!Doctype html>
<html>
<head>
<title>TutorialsWebsite
Hello World!
Welcome to Tutorialswebsite.com
This document is created from HTML.
