基于windows系統(tǒng)下和linux系統(tǒng)下php的在線預覽word文檔功能

位置:首頁 / 新聞中心 / 知識教程

知識教程 Admin 2024-02-22 18:02:44 958

//基于windows系統(tǒng)下和linux系統(tǒng)下的在線預覽word文檔功能

//----------------------------------------------------windows系統(tǒng)下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內(nèi)容操作 START

//打開一個word文檔

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

//定義一個目錄來存放生成的html文件,不存在則創(chuàng)建

$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文件內(nèi)容并輸出到頁面 (文本的樣式不會丟失)

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

echo $content;

//獲取word文檔內(nèi)容并輸出到頁面(文本的原樣式已丟失)

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

// echo $content;

//關閉與COM組件之間的連接

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

$word->Quit();

$word = null;

unset($word);

}

//----------------------------------------------------windows系統(tǒng)下end----------------------------------------------

//----------------------------------------------------linux系統(tǒng)下start-------------------------------------------

首先要在命令行中輸入下面內(nèi)容并執(zhí)行

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/*

//控制器內(nèi)容:*/

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系統(tǒng)下end----------------------------------------------

//特別聲明:windows下實現(xiàn)的預覽基本會按照原文檔樣式輸出,而linux下則是將內(nèi)容轉化成text內(nèi)容進行輸出,會“失真”。 如果有更好的方法希望可以分享出來。

以上就是“基于windows系統(tǒng)下和linux系統(tǒng)下php的在線預覽word文檔功能”的詳細內(nèi)容,更多請關注木子天禾科技其它相關文章!

以上就是“基于windows系統(tǒng)下和linux系統(tǒng)下php的在線預覽word文檔功能”的詳細內(nèi)容,更多請關注木子天禾科技其它相關文章!

15934152105 掃描微信