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

位置:首页 / 新闻中心 / 知识教程

知识教程 Admin 2024-02-22 18:02:44 2414

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

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

//控制器:

//获取文件路径

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

if(!is_file($file)){undefined

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

}else{undefined

//获取文件类型

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

switch ($file_type){undefined

case 'doc':

case 'docx':

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

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

if(file_exists($view_path)){undefined

$content = file_get_contents($view_path);

echo $content;

}else{undefined

view_word($file,$name);

}

break;

}

}

//view_word方法

//预览word文件

function view_word($wordname,$htmlname){undefined

//建立一个指向新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)){undefined

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内容进行输出,会“失真”。 如果有更好的方法希望可以分享出来。

以上就是“基于windows系统下和linux系统下php的在线预览word文档功能”的详细内容,更多请关注木子天禾科技其它相关文章!

以上就是“基于windows系统下和linux系统下php的在线预览word文档功能”的详细内容,更多请关注木子天禾科技其它相关文章!

15934152105 扫描微信