Example of PHP implementing Word to HTML document2015-10-16 17:07 2751 People read Comments (2) favorite reports Word document is not suitable to put on the page, if we want to put on the page to be copied, if you are still in the copy out, the following small series for you to compile a word to HTML document example, I hope the article will help you.
For a perfect solution, office to PDF or HTML, preferably with Windows Office software, LibreOffice cannot be transformed perfectly, WPS has no API.
First confirm that the COM module is not open, phpinfo inside if there is com_dotnet module, the description has been turned on, if not, modify PHP.ini,
Com.allow_dcom = True
Before the comments removed, restart on OK, PHP official website said, php5.4.5 before, COM module is built-in, in fact, is not necessarily all, the official website of the PHP 5.3.39,com module is not built-in.
If not the built-in module, PHP.ini Plus, the premise of your Ext folder, there is the extension
Extension=php_com_dotnet.dll
And then reboot is OK.
[PHP]View PlainCopyprint?
- 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 ');
Attention:
1, converted out of HTML, view the source code, the more chaotic
2, the Winword.exe is called during the conversion process
3, if the page has been loaded, rename the document and then re-turn.
Add an example
[PHP]View PlainCopyprint?
- function Lego_clean ($text) {
- $text = implode ("\ r",$text);
- //Normalize white space
- $text = eregi_replace ("[[: space:]]+", "", $text);
- $text = str_replace ("> <",">\r\r<",$text);
- $text = str_replace ("<br>","<br>\r",$text);
- //Remove everything before <body>
- $text = strstr ($text,"<body");
- //Keep tags, strip attributes
- $text = ereg_replace ("<p [^>]*bodytextindent[^>]*> ([^\n|\n\015|\015\n]*) </p>"," <p>\\1</p> ",$text);
- $text = eregi_replace ("<p [^>]*margin-left[^>]*> ([^\n|\n\015|\015\n]*) </p>"," <blockquote>\\1</blockquote> ",$text);
- $text = str_replace ("","" ",$text);
- //clean up whatever are left inside <p> and <li>
- $text = eregi_replace ("<p [^>]*>","<p>",$text);
- $text = eregi_replace ("<li [^>]*>","<li>",$text);
- //Kill unwanted tags
- $text = eregi_replace ("</?span[^>]*>", "",$text);
- $text = eregi_replace ("</?body[^>]*>", "",$text);
- $text = eregi_replace ("</?div[^>]*>", "",$text);
- $text = eregi_replace ("<\![ ^>]*> "," ",$text);
- $text = eregi_replace ("</?[ A-z]\:[^>]*> "," ",$text);
- //Kill style and on mouse* tags
- $text = eregi_replace ("([\f\r\t\n\ ' \"]) style=[^>]+"," \\1 ", $text);
- $text = eregi_replace ("([\f\r\t\n\ ' \"]) on[a-z]+=[^>]+"," \\1 ", $text);
- //remove Empty paragraphs
- $text = str_replace ("<p></p>", "",$text);
- //remove closing
- $text = str_replace ("",$text);
- //clean up white space again
- $text = eregi_replace ("[[: space:]]+", "", $text);
- $text = str_replace ("> <",">\r\r<",$text);
- $text = str_replace ("<br>","<br>\r",$text);
- }
Example of PHP implementing Word to HTML document