linux下doc,ppt等文件转换成swf格式

最近做公司的一个内部系统,需要用到在线预览的功能,找了很久,感觉下面这篇文章还不错哦,而且最终页解决了这个问题,所以想记录一下,方便他人也方便自己以后查找
系统环境:CentOs5.5 
用到的工具: Openoffice3 ,  Pdf2Swftool ,  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的安装。
    
  1. tar zxvf OOo_3.3.0_Linux_x86-64_install-rpm-wJRE_zh-CN.tar.gz
  2. cd RPEM
  3. rpm -ivh *.rpm --nodeps –force
  4. 安装后的默认目录是在:/opt/目录下面
  5. 启动服务:
  6. /opt/openoffice.org3/program/soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard &
     在CentOs下面会缺少黑体和宋体的,直接找到Windows下面的字体目录把黑体和宋体复制进去到目录
 
  1. /opt/openoffice.org/basis3.3/share/fonts/truetype/
  2. 需要重启后才会生效
  2. 安装 OpenOfficeSDK3.3
      
  1. tar zxvf OOo-SDK_3.3.0_Linux_x86-64_install-rpm_en-US.tar.gz
  2. cd OOO330_m20_native_packed-1_en-US.9567/RPMS/
  3. rpm -vih *.rpm
  3. 安装jodconverter.2.2.2 ,安装了这个之后就已经可以实现DOC转PDF了。
  这个安装很简单,直接上网站下一个这个东东回来。
解压,复制到一个目录里面去,就能直接用了,调用它里面的/lib/jodconverter-cli-2.2.2.jar这个玩意儿就行,可以直接运行命令测试:     
     
  1. java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar 1.doc 1.pdf
  4. 安装swftools
  中文支持安装:
  
  1. mkdir –p /usr/share/xpdf
  2. cd /usr/share/xpdf/
  3. 下载中文支持及字体库
  4. wget ftp://ftp.foolabs.com/pub/xpdf/xpdf-chinese-simplified.tar.gz
  5. wget http://www.nginxs.com/download/font.zip
  6. tar zxvf xpdf-chinese-simplified.tar.gz
  7. unzip font.zip
  8. mv Gbsn00lp.ttf gkai00mp.ttf xpdf-chinese-simplified/CMap/ cd /usr/share/xpdf/xpdf-chinese-simplified
  9. 编辑,加入新增的字体

  10. vim add-to-xpdfrc
  11. 内容如下:
  12. cidToUnicode Adobe-GB1 /usr/share/xpdf/chinese-simplified/Adobe-GB1.cidToUnicode
  13. unicodeMap ISO-2022-CN /usr/share/xpdf/chinese-simplified/ISO-2022-CN.unicodeMap
  14. unicodeMap EUC-CN /usr/share/xpdf/chinese-simplified/EUC-CN.unicodeMap
  15. unicodeMap GBK /usr/share/xpdf/chinese-simplified/GBK.unicodeMap
  16. cMapDir Adobe-GB1 /usr/share/xpdf/chinese-simplified/CMap toUnicodeDir /usr/share/xpdf/chinese-simplified/CMap
  17. displayCIDFontTT Adobe-GB1 /usr/share/xpdf/chinese-simplified/CMap/gkai00mp.ttf
  18. 保存后退出
 SwfTool安装:
  1. cd /usr/local/wenku
  2. wget http://www.swftools.org/swftools-0.9.1.tar.gz
  3. tar zxvf swftools-0.9.1.tar.gz
  4. cd swftools-0.9.1
  5. ./configure 6)make
  6. make install
  7. 测试一下是否可用
  8. pdf2swf -o /path/output.swf -T -z -t -f /path/yourpdffile.pdf -s languagedir=/usr/share/xpdf/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 jincan.liu@gmail.com
  5. */
  6. class converter extends CWidget{

  7. public function run($file)
  8. {

  9. $filesArr = array('pdf','doc','docx','xls','xlsx','ppt','pptx','txt');
  10. //文件上传并转换
  11. if($_FILES["file"] && $_FILES["file"]["error"] <=0 ){

  12. $fileName = iconv('UTF-8','gb2312',$_FILES["file"]["name"]);
  13. $types = explode('.',$fileName);
  14. $typesIf = $types[1];
  15. //改名为时间戳
  16. $types[0] = time();
  17. $fileName = $types[0].'.'.$types[1];
  18. $filetype = $typesIf;

  19. //限制上载类型
  20. if(!in_array($typesIf,$filesArr)){
  21. echo '<script type="text/javascript">alert("upload file types in : pdf,doc,docx,xsl,xlsx,ppt,pptx,txt");location.href=location.href;</script>';
  22. }
  23. /*
  24. function check_is_chinese($s){
  25. return preg_match('/[\x80-\xff]./', $s);
  26. }
  27. //检测中文文件名
  28. if (check_is_chinese($fileName)) {
  29. $types[0] = time();
  30. $fileName = $types[0].'.'.$types[1];
  31. }
  32. */

  33. //更改路径
  34. if($typesIf=='pdf'){
  35. $path = 'converter/files/pdf/';
  36. }else{
  37. $path = 'converter/files/doc/';
  38. }

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

  47. if (file_exists($path . $fileName)){
  48. //执行转换
  49. if($typesIf=='pdf'){ //PDF 转SWF
  50. $pdf = $fileName;
  51. $swf = str_replace('pdf','swf',$pdf);
  52. 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');
  53. $path2 = $pdfpath.$pdf;
  54. $path3 = $swfpath.$swf;
  55. }else{ //DOC 转 PDF
  56. $doc = $fileName;
  57. $format = explode('.',$fileName);
  58. $formatName = $format[0].'.pdf';
  59. $command = 'java -jar /usr/local/wenku/jodconverter-2.2.2/lib/jodconverter-cli-2.2.2.jar '.$docpath.$doc.' '.$pdfpath.$formatName;
  60. exec($command);
  61. $path1 = $docpath.$doc;
  62. $path2 = $pdfpath.$formatName;

  63. if(file_exists( $pdfpath.$formatName)){
  64. $pdf = $formatName;
  65. $swf = str_replace('pdf','swf',$pdf);
  66. $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';
  67. exec($swfcommand);
  68. $path3 = $swfpath.$swf;
  69. }
  70. }
  71. }
  72. }

  73. // print_r($_FILES["file"]);
  74. $filetype = isset($filetype) ? $filetype : '';
  75. $path1 = isset($path1) ? str_replace('/data/oa/frontend/www','',$path1) : '';
  76. $path2 = isset($path2) ? str_replace('/data/oa/frontend/www','',$path2) : '';
  77. $path3 = isset($path3) ? str_replace('/data/oa/frontend/www','',$path3) : '';
  78. $reArr = array('filetype' => $filetype,'path1'=>$path1,'path2'=>$path2,'path3'=>$path3);
  79. return $reArr;
  80. }




  81. }

  82. //删除文件
  83. public function DelFile($path,$pdfpath,$swfpath){
  84. $pathcommand = 'rm -rf /data/oa/frontend/www'.$path;
  85. exec($pathcommand);
  86. $pdfpathcommand = 'rm -rf /data/oa/frontend/www'.$pdfpath;
  87. exec($pdfpathcommand);
  88. $swfpathcommand = 'rm -rf /data/oa/frontend/www'.$swfpath;
  89. exec($swfpathcommand);
  90. }

  91. }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值