doc 轉換pdf swf

系統環境:CentOs5.5
用到的工具:Openoffice 3 , Pdf2Swf tool , Jodconverter , FlexPaper
 
網上找了些資料,早有人已經實現了這樣的功能,只不過是用JAVA來寫的東東,PHP的沒找着。
結合網上的資料根據實現操作經驗。
紀錄並總結一下:
 
整體思路如下:
實現步驟: 1. DOC上傳   2. DOC轉成PDF   3. PDF轉成SWF  4. 顯示
基於對Linux環境引入openoffice sdk + pdf2swf tool,分兩個步驟,先利用openoffice sdk把文檔統一轉成pdf,然後利用pdf2swf tool把pdf轉成swf
 
實現過程:
要實現DOC轉成PDF,在LINUX下面有Openoffice可以實現。
那麼首先需要的是安裝Openoffice這個東東。
 
1. 安裝openoffice3
這個安裝過程很糾結,遇到過各種問題,因爲先後在幾臺服務器上安裝過,最順利的安裝方法如下,如果遇到問題請看前面的文章有講openoffice的安裝。
 
tar zxvf OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_zh-CN.tar.gz
cd RPEM
rpm -ivh *.rpm --nodeps –force
安裝後的默認目錄是在:/opt/目錄下面
啓動服務:
/opt/openoffice.org3/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
在CentOs下面會缺少黑體和宋體的,直接找到Windows下面的字體目錄把黑體和宋體複製進去到目錄
 
/opt/openoffice.org/basis3.3/share/fonts/truetype/
需要重啓後纔會生效
 
2. 安裝OpenOffice SDK3.3
 
tar zxvf OOo-SDK_3.3.0_Linux_x86-64_install-rpm_en-US.tar.gz
cd OOO330_m20_native_packed-1_en-US.9567/RPMS/
rpm -vih *.rpm
 
3. 安裝jodconverter.2.2.2 ,安裝了這個之後就已經可以實現DOC轉PDF了。
這個安裝很簡單,直接上網站下一個這個東東回來。
解壓,複製到一個目錄裏面去,就能直接用了,調用它裏面的/lib/jodconverter-cli-2.2.2.jar這個玩意兒就行,可以直接運行命令測試:     
 
java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar /tmp/1.doc /tmp/1.pdf
 
4. 安裝swftools
中文支持安裝:
mkdir –p /usr/share/xpdf
cd /usr/share/xpdf/
下載中文支持及字體庫
wget ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
wget http://www.nginxs.com/download/font.zip
tar zxvf xpdf-chinese-simplified.tar.gz
unzip font.zip
mv Gbsn00lp.ttf gkai00mp.ttf xpdf-chinese-simplified/CMap/ cd /usr/share/xpdf/xpdf-chinese-simplified
編輯,加入新增的字體
 
vim add-to-xpdfrc
內容如下:
cidToUnicodeAdobe-GB1/usr/share/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode
unicodeMapISO-2022-CN/usr/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap
unicodeMapEUC-CN/usr/share/xpdf/chinese-simplified/EUC-CN.unicodeMap
unicodeMapGBK/usr/share/xpdf/chinese-simplified/GBK.unicodeMap
cMapDirAdobe-GB1/usr/share/xpdf/chinese-simplified/CMap toUnicodeDir/usr/share/xpdf/chinese-simplified/CMap
displayCIDFontTT Adobe-GB1 /usr/share/xpdf/chinese-simplified/CMap/gkai00mp.ttf
保存後退出
SwfTool安裝:
cd /usr/local/wenku
wget http://www.swftools.org/swftools-0.9.1.tar.gz
tar zxvf swftools-0.9.1.tar.gz
cd swftools-0.9.1
./configure 
make
make install
測試一下是否可用
pdf2swf -o /usr/output.swf -T -z -t -f /usr/test1.pdf -s languagedir=/usr/share/xpdf/chinese-simplified -s flashversion=9
如果測試成功,那麼就已經OK一大半了,就是顯示的問題了。
 
5. 用FlexPaper實現在線預覽
這個是網上下載吧,看着demo改下JS就行了,裏面是用的JS讀取SWF的路徑,這個簡單吧,不講了。
 
PHP來調用LINUX命令實現轉換,可能會遇到一個問題說PHP沒有執行權限,注意把PHP的用戶改成權限也就是nobody這個用戶的權限。
下面是我自己寫的代碼,PHP初學不久,不夠精簡,僅供參考
Yii框架中文件轉換功能,DOC轉SWF

  1. <?php  
  2. /** 
  3. 文件轉換 
  4. oscar 2011-11-25 [email protected] 
  5. */  
  6. class converter extends CWidget{  
  7.   
  8. public function run($file)  
  9. {  
  10.   
  11. $filesArr = array('pdf','doc','docx','xls','xlsx','ppt','pptx','txt');  
  12. //文件上傳並轉換  
  13. if($_FILES["file"] && $_FILES["file"]["error"] <=0 ){  
  14.   
  15. $fileName = iconv('UTF-8','gb2312',$_FILES["file"]["name"]);  
  16. $types = explode('.',$fileName);  
  17. $typesIf = $types[1];  
  18. //改名爲時間戳  
  19. $types[0] = time();  
  20. $fileName = $types[0].'.'.$types[1];  
  21. $filetype = $typesIf;  
  22.   
  23. //限制上載類型  
  24. if(!in_array($typesIf,$filesArr)){  
  25. echo '<script type="text/javascript">alert("upload file types in : pdf,doc,docx,xsl,xlsx,ppt,pptx,txt");location.href=location.href;</script>';  
  26. }  
  27. /* 
  28. function check_is_chinese($s){ 
  29. return preg_match('/[\x80-\xff]./', $s); 
  30. } 
  31. //檢測中文文件名 
  32. if (check_is_chinese($fileName)) { 
  33. $types[0] = time(); 
  34. $fileName = $types[0].'.'.$types[1]; 
  35. } 
  36. */  
  37.   
  38. //更改路徑  
  39. if($typesIf=='pdf'){  
  40. $path = 'converter/files/pdf/';  
  41. }else{  
  42. $path = 'converter/files/doc/';  
  43. }  
  44.   
  45. if (file_exists($path . $fileName)){  
  46. echo '<script type="text/javascript">alert("文件已經存在了");location.href=location.href;</script>';  
  47. }else{  
  48. move_uploaded_file($_FILES["file"]["tmp_name"], $path.$fileName);  
  49. //var path  
  50. $docpath = '/data/oa/frontend/www/converter/files/doc/';  
  51. $pdfpath = '/data/oa/frontend/www/converter/files/pdf/';  
  52. $swfpath = '/data/oa/frontend/www/converter/files/swf/';  
  53.   
  54. if (file_exists($path . $fileName)){  
  55. //執行轉換  
  56. if($typesIf=='pdf'){ //PDF 轉SWF  
  57. $pdf = $fileName;  
  58. $swf = str_replace('pdf','swf',$pdf);  
  59. exec('/usr/local/wenku/swftools/bin/pdf2swf -o '.$swfpath.$swf.' -T -z -t -f '.$pdfpath.$pdf.' -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9');  
  60. $path2 = $pdfpath.$pdf;  
  61. $path3 = $swfpath.$swf;  
  62. }else{ //DOC 轉 PDF  
  63. $doc = $fileName;  
  64. $format = explode('.',$fileName);  
  65. $formatName = $format[0].'.pdf';  
  66. $command = 'java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar '.$docpath.$doc.' '.$pdfpath.$formatName;  
  67. exec($command);  
  68. $path1 = $docpath.$doc;  
  69. $path2 = $pdfpath.$formatName;  
  70.   
  71. if(file_exists( $pdfpath.$formatName)){  
  72. $pdf = $formatName;  
  73. $swf = str_replace('pdf','swf',$pdf);  
  74. $swfcommand = '/usr/local/wenku/swftools/bin/pdf2swf -o '.$swfpath.$swf.' -T -z -t -f '.$pdfpath.$pdf.' -s languagedir=/usr/share/xpdf/xpdf-chinese-simplified -s flashversion=9';  
  75. exec($swfcommand);  
  76. $path3 = $swfpath.$swf;  
  77. }  
  78. }  
  79. }  
  80. }  
  81.   
  82. // print_r($_FILES["file"]);  
  83. $filetype = isset($filetype) ? $filetype : '';  
  84. $path1 = isset($path1) ? str_replace('/data/oa/frontend/www','',$path1) : '';  
  85. $path2 = isset($path2) ? str_replace('/data/oa/frontend/www','',$path2) : '';  
  86. $path3 = isset($path3) ? str_replace('/data/oa/frontend/www','',$path3) : '';  
  87. $reArr = array('filetype' => $filetype,'path1'=>$path1,'path2'=>$path2,'path3'=>$path3);  
  88. return $reArr;  
  89. }  
  90.   
  91. }  
  92.   
  93. //刪除文件  
  94. public function DelFile($path,$pdfpath,$swfpath){  
  95. $pathcommand = 'rm -rf /data/oa/frontend/www'.$path;  
  96. exec($pathcommand);  
  97. $pdfpathcommand = 'rm -rf /data/oa/frontend/www'.$pdfpath;  
  98. exec($pdfpathcommand);  
  99. $swfpathcommand = 'rm -rf /data/oa/frontend/www'.$swfpath;  
  100. exec($swfpathcommand);  
  101. }  
  102. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章