centos下使用php实现word转html并读取导入数据库
作者:stoat 发布时间:2018-08-08 分类:技术
原理:linux系统下利用libreoffice+unoconv将word转化为html,如果在windows环境下转换word为html,则需要下载office套件并开启COM组件,windows下实现较为简单,本文不讨论。
主流程
1,安装libreoffice
下载rpm包 http://download.documentfoundation.org/libreoffice/stable/5.4.7/rpm/x86_64/LibreOffice_5.4.7_Linux_x86-64_rpm.tar.gz http://download.documentfoundation.org/libreoffice/stable/5.4.7/rpm/x86_64/LibreOffice_5.4.7_Linux_x86-64_rpm_sdk.tar.gz http://download.documentfoundation.org/libreoffice/stable/5.4.7/rpm/x86_64/LibreOffice_5.4.7_Linux_x86-64_rpm_langpack_zh-CN.tar.gz http://download.documentfoundation.org/libreoffice/stable/5.4.7/rpm/x86_64/LibreOffice_5.4.7_Linux_x86-64_rpm_helppack_zh-CN.tar.gz 解压后,进入目录,批量安装 cd /usr/local/libreoffice/LibreOffice_5.4.7.2_Linux_x86-64_rpm/RPMS cd /usr/local/libreoffice/LibreOffice_5.4.7.2_Linux_x86-64_rpm_sdk/RPMS yum localinstall *.rpm rpm -ivh lib*
2,安装unoconv
wget https://raw.githubusercontent.com/dagwieers/unoconv/master/unoconv chmod +x unoconv ln -s /usr/libreoffice/unoconv /usr/bin/unoconv yum install unoconv
注:如需要转换pdf的话则需要下载windows下的字体文件。
3,php下转化word为html,所有的html文件放置在word2html文件夹下
function WordToHtml($source, $name) { set_time_limit(0); $dir = dirname($source); chmod($dir,0755); // 生成html文件 $html = $dir.'/word2html/'.$name; shell_exec('/usr/bin/unoconv -f html -o ' . $html . ' ' . $source); $content = file_get_contents($html); return $content; }
4,读取并格式化html代码
读取函数