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 6)make
make install
測試一下是否可用
pdf2swf -o /usr/output.swf -T -z -t -f /usr/test1.pdf -s languagedir=/usr/local/share/xpdf/chinese-simplified -s flashversion=9
如果測試成功,那麼就已經OK一大半了,就是顯示的問題了。

5. 用FlexPaper實現在線預覽
這個是網上下載吧,看着demo改下JS就行了,裏面是用的JS讀取SWF的路徑,這個簡單吧,不講了。


PHP來調用LINUX命令實現轉換,可能會遇到一個問題說PHP沒有執行權限,注意把PHP的用戶改成權限也就是nobody這個用戶的權限。


下面是我自己寫的代碼,PHP初學不久,不夠精簡,僅供參考
Yii框架中文件轉換功能,DOC轉SWF

<?php
/**
文件轉換
oscar 2011-11-25 [email protected]
*/
class converter extends CWidget{

public function run($file)
{

$filesArr = array('pdf','doc','docx','xls','xlsx','ppt','pptx','txt');
//文件上傳並轉換
if($_FILES["file"] && $_FILES["file"]["error"] <=0 ){

$fileName = iconv('UTF-8','gb2312',$_FILES["file"]["name"]);
$types = explode('.',$fileName);
$typesIf = $types[1];
//改名爲時間戳
$types[0] = time();
$fileName = $types[0].'.'.$types[1];
$filetype = $typesIf;

//限制上載類型
if(!in_array($typesIf,$filesArr)){
echo '<script type="text/javascript">alert("upload file types in : pdf,doc,docx,xsl,xlsx,ppt,pptx,txt");location.href=location.href;</script>';
}
/*
function check_is_chinese($s){
return preg_match('/[\x80-\xff]./', $s);
}
//檢測中文文件名
if (check_is_chinese($fileName)) {
$types[0] = time();
$fileName = $types[0].'.'.$types[1];
}
*/

//更改路徑
if($typesIf=='pdf'){
$path = 'converter/files/pdf/';
}else{
$path = 'converter/files/doc/';
}

if (file_exists($path . $fileName)){
echo '<script type="text/javascript">alert("文件已經存在了");location.href=location.href;</script>';
}else{
move_uploaded_file($_FILES["file"]["tmp_name"], $path.$fileName);
//var path
$docpath = '/data/oa/frontend/www/converter/files/doc/';
$pdfpath = '/data/oa/frontend/www/converter/files/pdf/';
$swfpath = '/data/oa/frontend/www/converter/files/swf/';

if (file_exists($path . $fileName)){
//執行轉換
if($typesIf=='pdf'){ //PDF 轉SWF
$pdf = $fileName;
$swf = str_replace('pdf','swf',$pdf);
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');
$path2 = $pdfpath.$pdf;
$path3 = $swfpath.$swf;
}else{ //DOC 轉 PDF
$doc = $fileName;
$format = explode('.',$fileName);
$formatName = $format[0].'.pdf';
$command = 'java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar '.$docpath.$doc.' '.$pdfpath.$formatName;
exec($command);
$path1 = $docpath.$doc;
$path2 = $pdfpath.$formatName;

if(file_exists( $pdfpath.$formatName)){
$pdf = $formatName;
$swf = str_replace('pdf','swf',$pdf);
$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';
exec($swfcommand);
$path3 = $swfpath.$swf;
}
}
}
}

// print_r($_FILES["file"]);
$filetype = isset($filetype) ? $filetype : '';
$path1 = isset($path1) ? str_replace('/data/oa/frontend/www','',$path1) : '';
$path2 = isset($path2) ? str_replace('/data/oa/frontend/www','',$path2) : '';
$path3 = isset($path3) ? str_replace('/data/oa/frontend/www','',$path3) : '';
$reArr = array('filetype' => $filetype,'path1'=>$path1,'path2'=>$path2,'path3'=>$path3);
return $reArr;
}


// php100.com php100中文網

}

//刪除文件
public function DelFile($path,$pdfpath,$swfpath){
$pathcommand = 'rm -rf /data/oa/frontend/www'.$path;
exec($pathcommand);
$pdfpathcommand = 'rm -rf /data/oa/frontend/www'.$pdfpath;
exec($pdfpathcommand);
$swfpathcommand = 'rm -rf /data/oa/frontend/www'.$swfpath;
exec($swfpathcommand);
}

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章