Home  >  Article  >  Backend Development  >  [php tutorial] Sharing methods of converting HTML to word in PHP

[php tutorial] Sharing methods of converting HTML to word in PHP

WBOY
WBOYOriginal
2016-07-25 08:45:531447browse
In PHP development, how does PHP implement HTML to word conversion? Is there any way? Next, I will introduce to you the method of transferring html into word using php. This has been tested. This method is a bit disadvantageous. The html page code must be written in php, but it can run anyway. See the program
class word{
function start(){
ob_start();
echo '="urn:schemas-microsoft-comfficeffice"
xmlns:w="urn :schemas-microsoft-comffice:word"
xmlns="http://www.w3.org/TR/REC-html40">';
}
function save($path)
{
echo "";
$data = ob_get_contents();
}
function wirtefile($fn,$data){
$fp=fopen($fp,$data);
fwrite($fp,$data);
}
}
$html='
< ;td style="border:1px solid green;">Hobbies
< td style="border:1px solid green;">None
Name
Gender Age Remarks
Zhang San Male 32 Football None
李思
43Basketball
';
//Batch generation
//for($ i=1;$i<3;$i++){
$word = new word();
$word->start();
$wordname='Personal test php generates word.doc';
echo $html;
$word->save($wordname);
header('Content-type:application/word');
header('Content-Disposition: attachment; filename='.$wordname .'');
//readfile($wordname);
ob_flush();//Refresh the cache before each execution
flush();
//}
?>
The following is for everyone Share a piece of code using PHP to convert word to HTML
If you want a perfect solution to convert office to pdf or html, it is best to use windows office software. Libreoffice cannot convert perfectly, and wps does not have an API.
First confirm whether the com module is turned on. If there is a com_dotnet module in phpinfo, it means it is turned on. If not, modify php.ini,
The code is as follows:
com.allow_dcom = true
Remove the previous comments and restart and it will be OK. The official PHP website says that before PHP5.4.5, the COM module was built-in. In fact, it is not necessarily the case. In PHP 5.3.39 under the official website, the COM module is not built-in.
If it is not a built-in module, add it to php.ini, provided that you have the extension in your ext folder. The code is as follows:
extension=php_com_dotnet.dll
Then restart and it will be OK
function word2html($wordname, $htmlname)
{
$word = new COM("word.application") or die("Unable to instanciate Word");
$word->Visible = 1;
$word->Documents ->Open($wordname);
$word->Documents[1]->SaveAs($htmlname,8);
$word->Quit();
$word = null;
unset($word);
}
word2html('D:/www/test/6.docx','D:/www/test/6.html');
Note:
1. Conversion Check the source code for the HTML that comes out, it’s quite messy
2. Winword.exe will be called during the conversion process
3. If the page keeps loading, rename the document and then convert it again.
The above is the entire content of this article. Friends who are interested can try it themselves. If there is a better way to convert HTML to word, please share it with everyone.
Recommended reading: Data sorting in PHP, Infinitus classification, commonly used analysis techniques

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn