Heim  >  Artikel  >  php教程  >  Linux 下 php 转DOC转PDF转SWF实现百度的文库预览功能

Linux 下 php 转DOC转PDF转SWF实现百度的文库预览功能

WBOY
WBOYOriginal
2016-06-21 08:49:29110898Durchsuche

去年开发了一个OA系统,需要实现文档一键上传并实现在线预览,类似百度文库的功能。

系统环境: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

/**
文件转换
oscar 2011-11-25 jincan.liu@gmail.com
*/
class converter extends CWidget{

public function run($file)
{

$filesArr = array('pdf','doc','docx','xls','xlsx','ppt','pptx','txt');
//文件上传并转换
if($_FILES["file"] && $_FILES["file"]["error"]
$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 '';
}
/*
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 '';
}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);
}

}



Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn