實現如同百度文庫那樣類似功能需要進行一系列轉化,一般流程想將word轉化為pdf格式,再將pdf格式轉化為swf格式。在網頁上顯示其實都是swf格式內容。
首先將word轉化為swf,需要調用com組件,可以通過office 或者wps進行轉化,但我嘗試都沒有成功,最後通過OpenOffice 4.0.0 進行轉化才成功,OpenOffice 4.0.0支援windows 和linux作業系統,故需要先下載openOffice,官網上應該有。將word轉化為pdf,好像只是支援英文的標題的文檔,不支援漢文名字文檔,可以先將檔案重新命名為英文,轉化之後再將檔案變為漢文即可,可能還需要開始openoffice 服務,
需要開啟php中com元件服務
修改php設定檔
先到PHP.INI中開啟COM選項,com.allow_dcom = true
注意: PHP 5.4.5後,com/dotnet
模組已經成了單獨的擴充,所以需要在PHP.ini中配置extension=php_com_dotnet.dll ,
如果PHP VERSION<5.4.5 則不需要
驗證服務是否開啟, 開啟8100連接埠
代碼如下:
<?phpclass RunTime//頁面執行時間類 { private $starttime;//頁面開始執行時間 private $stoptime;//頁面結束執行時間 private $spendtime;//頁面執行花費時間 function getmicrotime()//擷取返回當前微秒數的浮點數 { list($usec,$sec)=explode(" ",microtime()); return ((float)$usec + (float)$sec); } function start()//頁面開始執行函數,返回開始頁面執行的時間 { $this->starttime=$this->getmicrotime(); } function end()//顯示頁面執行的時間 { $this->stoptime=$this->getmicrotime(); $this->spendtime=$this->stoptime-$this->starttime; //return round($this->spendtime,10); } function display() { //$this->end(); echo "<p>已耗用時間:".round($this->spendtime,10)."秒</p>"; } } /*調用方法 */$timer=new Runtime(); $timer->start(); function MakePropertyValue($name,$value,$osm){ $oStruct = $osm->Bridge_GetStruct ("com.sun.star.beans.PropertyValue"); $oStruct->Name = $name; $oStruct->Value = $value; return $oStruct; } function word2pdf($doc_url, $output_url){ $osm = new COM("com.sun.star.ServiceManager") or die ("Please be sure that OpenOffice.org is installed.\n"); $args = array(MakePropertyValue("Hidden",true,$osm)); $oDesktop = $osm->createInstance("com.sun.star.frame.Desktop"); $oWriterDoc = $oDesktop->loadComponentFromURL ($doc_url,"_blank", 0, $args); $export_args = array(MakePropertyValue ("FilterName","writer_pdf_Export",$osm)); $oWriterDoc->storeToURL($output_url,$export_args); $oWriterDoc->close(true); } $output_dir = "C:/"; $doc_file = "C:/t.doc"; $pdf_file = "9.pdf"; $output_file = $output_dir . $pdf_file; $doc_file = "file:///" . $doc_file; $output_file = "file:///" . $output_file; word2pdf($doc_file,$output_file); $timer->end();$timer->display();?>
花費時間進行分析:
將一個大小為1.48M的word文檔轉化為pdf需要已耗用時間:1.3652579784秒 自己電腦是這個時間,自己測試
其次將pdf轉化為swf,需要運用另外一個軟體,swftools 通過代碼調用cmd命令,直接上代碼
<?phpclass RunTime//頁面執行時間類 { private $starttime;//頁面開始執行時間 private $stoptime;//頁面結束執行時間 private $spendtime;//頁面執行花費時間 function getmicrotime()//擷取返回當前微秒數的浮點數 { list($usec,$sec)=explode(" ",microtime()); return ((float)$usec + (float)$sec); } function start()//頁面開始執行函數,返回開始頁面執行的時間 { $this->starttime=$this->getmicrotime(); } function end()//顯示頁面執行的時間 { $this->stoptime=$this->getmicrotime(); $this->spendtime=$this->stoptime-$this->starttime; //return round($this->spendtime,10); } function display() { //$this->end(); echo "<p>已耗用時間:".round($this->spendtime,10)."秒</p>"; } } /*調用方法 */$timer=new Runtime(); $timer->start(); //調用系統軟體$command = "\"C:\Program Files\SWFTools\pdf2swf.exe\" -t C:\8.pdf -s flashversion=9 -o C:\m.swf"; echo $command;exec($command);echo 'ok'; $timer->end();$timer->display();?>
將剛才轉化而來的pdf轉化為swf檔案需要用已耗用時間:1.3119211197秒時間
最後則是將swf檔案顯示在網頁中,這一步需要引入多個js檔案和其他檔案,代碼就不寫了,直接下載,在我的上傳資料中
地址:http://download.csdn.net/detail/buyingfei888/5896109