Linux unoconv
unoconv 转换 doc,docx,xls,xlsx,ppt,pptx 文件
安装unoconv
yum install unoconv
doc转换成pdf
/usr/bin/unoconv -f pdf /home/1.doc
doc转换成txt
/usr/bin/unoconv -f txt /home/1.doc
docx转换成txt
/usr/bin/unoconv -f txt /home/1.docx
php中使用需要另外安装unoconv
下载地址:https://github.com/unoconv/unoconv
解压并安装
unzip unoconv-master.zip
cd unoconv-master/
make install
执行完以上操作后接下来可以执行转换命令了,例如
/usr/bin/unoconv -f txt /www/1.docx
得到 /www/1.txt
如果中文乱码可以用以下方式解决
将Windows下的Fonts,如:C:\Windows\Fonts,压缩成Fonts.zip压缩包。
将压缩包拷贝到Linux目录下,执行以下命令便可:
unzip Fonts.zip
mkdir /usr/share/fonts/win
cp Fonts/* /usr/share/fonts/win
cd /usr/share/fonts/win
chmod -Rf 755 *
mkfontscale
mkfontdir
fc-cache –fv
重启服务器便可。
php中使用
shell_exec('/usr/bin/unoconv -f txt /www/1.docx - 2>&1');
得到 /www/1.txt
$filename='/www/1.txt';
$handle = fopen($filename, "r");//读取文件
//通过filesize获得文件大小,将整个文件一下子读到一个字符串中
$contents = fread($handle, filesize ($filename));//获取到了内容
fclose($handle);//关闭文件
unlink($filename);//删除/www/1.txt