php实现word转html文档的例子

word文档不适合放到网页上了,如果我们要放到网页中去是需要一个个复制了,如果你还在复制就out了,下文小编来为各位整理一篇php实现word转html文档的例子,希望文章对各位有帮助。

要想完美解决,office转pdf或者html,最好还是用windows office软件,libreoffice不能完美转换,wps没有api。

先确认com模块是不是开启,phpinfo里面如果有com_dotnet模块,说明已开启,如果没有,修改php.ini,
com.allow_dcom = true

前面的注释去掉,重启就OK了,php官方网站说,php5.4.5之前,com模块是内置的,其实也不一定全是,官网下的php 5.3.39,com模块就没有内置。

如果不是内置模块的话,php.ini加上,前提你的ext文件夹下,有该扩展

extension=php_com_dotnet.dll

然后重启就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'); 

注意:

1,转换出来的html,查看源码,比较乱的
2,转换过程中会调用winword.exe
3,如果页面一直在加载,把文档重命名,然后在重新转。

补充一个例子


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 is 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 </html>  
    $text = str_replace("</html>","",$text);  
 
    //clean up white space again  
    $text = eregi_replace("[[:space:]]+", " ", $text);  
    $text = str_replace("> <",">\r\r<",$text);  
    $text = str_replace("<br>","<br>\r",$text);  
} 


PHPWord Beta 0.6.2 开发者指南 目 录 首先我们要了解文档最基本的信息和设置: 4 计量单位:缇(twips) 4 字体设置 4 文档属性设置 4 新建文档 5 添加页面 5 页面样式 5 页面样式属性 6 文本 7 添加文本 7 添加文本资源 7 文本样式 8 样式属性列表 9 添加换行符 10 添加分页符 10 列表 10 添加列表 10 列表样式 11 列表样式属性列表 11 超链接 11 添加超链接 11 超链接样式 12 图片 13 添加图片 13 图片样式 13 图片样式属性 13 添加GD生成图片 14 添加水印 14 添加对象 15 添加标题 15 添加目录 16 表格 17 添加表格 17 添加行 17 添加单元格 17 单元格样式 19 表格样式 20 页脚 22 页眉 23 模版 23 其他问题修改 25 解决文本缩进问题 25 表格对齐和表格缩进 27 图片缩进和绝对相对悬浮定位 30 首先我们要了解文档最基本的信息和设置:  因为是国外编辑的类库,存在对中文支持的问题,使用前,我们需要进行一些修正: 1、解决编码问题,PHPword 会对输入的文字进行utf8_encode编码化,如果你使用GBK、GB2312或者utf8编码的话就会出现乱码,如果你用utf8编码,就查找类库中所有方法中的 utf8_encode 码将其删除,如果你采用GBK或者GB2312编码,使用iconv进行编码换。 2、解决中文字体支持,在writer/word2007/base.php中 312行添加 $objWriter->writeAttribute('w:eastAsia',$font) 3、启动php zip支持,windows环境下在php配置文件php.ini中,将extension=php_zip.dll前面的分号“;”去除;(如果没有,请添加extension=php_zip.dll此行并确保php_zip.dll文件存在相应的目录),然后同样在php.ini文件中,将 zlib.output_compression = Off 改为zlib.output_compression = On ; 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值