php预览word,php预览Word

该博客详细介绍了在Windows和Linux系统下实现在线预览Word文档的方法。在Windows系统中,通过COM组件与Word应用程序交互,将Word转换为HTML以保持样式预览。而在Linux环境下,利用antiword工具将Word文档转换为纯文本内容输出。虽然Linux系统下会失去原始样式,但两者都提供了预览功能。
摘要由CSDN通过智能技术生成

//基于windows系统下和linux系统下的在线预览word文档功能

//----------------------------------------------------windows系统下start-------------------------------------------

//控制器:

//获取文件路径

$file = substr(thumb($extends['file']),1);

if(!is_file($file)){

$this->error("文件不存在!");

}else{

//获取文件类型

$file_type = strtolower(substr($file,strrpos($file,".")+1));

switch ($file_type){

case 'doc':

case 'docx':

$name = "show_word_".$id.".html";

$view_path = "Uploads/View_file/".$name;

if(file_exists($view_path)){

$content = file_get_contents($view_path);

echo $content;

}else{

view_word($file,$name);

}

break;

}

}

//view_word方法

//预览word文件

function view_word($wordname,$htmlname){

//建立一个指向新COM组件的索引

$word = new COM("word.application") or die("Unable to instanciate Word");

//显示目前正在使用的Word的版本号

//echo "Loading Word, v. {$word->Version}";

//把它的可见性设置为0(假),如果要使它在最前端打开,使用1(真)

$word->Visible = 1;

//读取Word内容操作 START

//打开一个word文档

$word->Documents->Open(realpath($wordname));

//定义一个目录来存放生成的html文件,不存在则创建

$path = WEB_ROOT."Uploads/View_file/";

if (!file_exists($path)){

mkdir($path);

}

//将filename.doc转换为html格式,并保存为html文件

$word->Documents[1]->SaveAs($path.$htmlname,8);

//获取htm文件内容并输出到页面 (文本的样式不会丢失)

$content = file_get_contents($path.$htmlname);

echo $content;

//获取word文档内容并输出到页面(文本的原样式已丢失)

// $content= $word->ActiveDocument->content->Text;

// echo $content;

//关闭与COM组件之间的连接

$word->Documents->close(true);

$word->Quit();

$word = null;

unset($word);

}

//----------------------------------------------------windows系统下end----------------------------------------------

//----------------------------------------------------linux系统下start-------------------------------------------

首先要在命令行中输入下面内容并执行

wget http://www.winfield.demon.nl/linux/antiword-0.37.tar.gz

tar zxvf antiword-0.37.tar.gz

cd antiword-0.37

make

make install

make global_install

cp /root/bin/*antiword /usr/local/bin/

mkdir /usr/share/antiword

cp -R /root/.antiword/* /usr/share/antiword/

chmod 777 /usr/local/bin/*antiword

chmod 755 /usr/share/antiword/*

//控制器内容:*/

header("Content-type: text/html; charset=utf-8");

$content = shell_exec('/usr/local/bin/antiword -w 0 -m UTF-8 '.$file);

echo '

';

print_r ($content);

echo '

';

//----------------------------------------------------linux系统下end----------------------------------------------

//特别声明:windows下实现的预览基本会按照原文档样式输出,而linux下则是将内容转化成text内容进行输出,会“失真”。 如果有更好的方法希望可以分享出来。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值