<?php
//基于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 '<pre>';
print_r ($content);
echo '</pre>';
//----------------------------------------------------linux系統下end----------------------------------------------
//特別聲明:windows下實現的預覽基本會按照原文檔樣式輸出,而linux下則是將內容轉化成text內容進行輸出,會“失真”。 如果有更好的方法希望可以分享出來。
- 前言
- Interview
- 01-cookie與session
- 04-mysql索引
- 05-memcache與redis
- 06-高并發
- 07-主從同步
- 08-myisam與inondb
- 09-框架區別
- 10-php7新特性
- 11-設計模式
- 12-MySQL優化
- 13-php自動加載機制
- 14-MongoDB命令
- 15-Python爬蟲之scrapy框架
- 16-php運行原理
- 17-單點登錄
- Linux
- linux常見命令
- awk
- sed
- bash-shell
- vim學習教程
- PHP
- php運行機制
- php執行效率
- word2pdf
- php預覽Word
- wechat_wafter2
- Python
- python
- win-bat
- svn操作命令
- git常用操作命令
- git_install
- gitlab安裝
- nextcloud
- docker