PHP利用openoffice实现文档装换

openoffice

要实现文档转换为PDF,可利用jodconverter开源jar实现文档的转换。 而jodconverter的转换原理是利用openoffice实现转换。而jodconverter必须要有Java环境才可转换。故要实现word -> pdf -> swf 
需要安装的软件有

  1. openoffice
  2. jodconverter jar包
  3. swftools
  4. jdk/jre

word转换PDF

/**
*
* @param unknown $wordpath  要转换为PDF的word文件路径,全路径 如F://duanxin.doc
* @param unknown $outPdfPath  转换成功后的PDF文件路径
* @param unknown $jodconverterPath 安装的jodconverter的jodconverter-cli-2.2.2.jar所在路径,我的为:D://jodconverter//jodconverter-2.2.2//lib//jodconverter-cli-2.2.2.jar
* @return boolean
*/
function word2pdf ($wordpath, $outPdfPath, $jodconverterPath) {
    if (empty($wordpath)) return false;    
    try {
        //这里是因为我吧Java(jdk/jre)加入了环境变量,故可直接写出下面这样, 
        //相当于cmd窗口下直接写 java -jar jodconverter-cli-2.2.2.jar所在路径 word文件 PDF文件
        $p = "java -jar ". $jodconverterPath . ' ' . $wordpath . ' ' . $outPdfPath;
        // 否则该前面应该加入jre/jdk的路径
        exec($p);
        return true;
    } catch (Exception $e) {
        return false;
    }
}
      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

在执行转换前, 必须先启动openoffice服务,可直接用cmd窗口进入openoffice的安装目录即

C:\User\Administrator>cd D:\openoffice\program
C:\User\Administrator>d:
D:\openoffice\program>soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

      
      
  • 1
  • 2
  • 3
  • 4

PDF2SWF

/**
*
* @param pdf2swf swftools安装路径,如 D://SWFTools//pdf2swf.exe
* @param swfPath SWF文件
* @param pdfpath PDF文件
*/
function pdf2swf($pdf2swf, $swfPath, $pdfpath){
    $c = $pdf2swf . ' -o ' . $swfPath . ' -T -z -t -f ' . $pdfpath . ' -s flashversion=9';
    exec($c);
}
      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

我的测试代码如下

<?php
$startOpenofficeExc = 'soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard';

$openoffice   = 'D://openoffice//program//';

$jodconverter = 'D://jodconverter//jodconverter-2.2.2//lib//jodconverter-cli-2.2.2.jar';

$testDoc      = 'F://duanxin.doc';

$testOut      = 'F://duanxin.pdf';

$javaPath     = 'C:\Program Files\Java\jdk1.7.0_79\bin\\';

$pdf2swf      = 'D://SWFTools//pdf2swf.exe';

//启动openoffice
//exec($openoffice . $startOpenofficeExc);
//pdf2swf -o /home/php/1.swf -T -z -t -f /home/php/1.pdf -s flashversion=9

/**
 * 
 * @param unknown $wordpath
 * @param unknown $outPdfPath
 * @param unknown $jodconverterPath
 * @return boolean
 */
function word2pdf ($wordpath, $outPdfPath, $jodconverterPath) {
    if (empty($wordpath)) return false;    
    try {
        $p = "java -jar ". $jodconverterPath . ' ' . $wordpath . ' ' . $outPdfPath;
        exec($p);
        return true;
    } catch (Exception $e) {
        return false;
    }
}

if(word2pdf($testDoc, $testOut, $jodconverter)) {
    header("location: show.php?swfName=duanxin.swf");
} else {
    echo 'error';
}


function pdf2swf($pdf2swf, $swfPath, $pdfpath){
    $c = $pdf2swf . ' -o ' . $swfPath . ' -T -z -t -f ' . $pdfpath . ' -s flashversion=9';
    exec($c);
}
echo '--------------------------------------------------------------------------<br>';

$swfPath2 = 'F://duanxin.swf';
$pdfpath2 = 'F://duanxin.pdf';
pdf2swf($pdf2swf, $swfPath2, $pdfpath2);
      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53

显示SWF

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <title>查看</title>
    <meta charset="utf-8">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <script type="text/javascript" src="js/flexpaper_flash.js"></script>
    <script language="JavaScript">
        function Request(argname) {
            var url = document.location.href;
            var arrStr = url.substring(url.indexOf("?")+1).split("&");
            //return arrStr;
            for(var i =0;i<arrStr.length;i++) {
                var loc = arrStr[i].indexOf(argname+"=");

                if(loc!=-1) {
                    return arrStr[i].replace(argname+"=","").replace("?","");
                    break;
                }

            }
            return "";
        }
        //使用Request("argname")即可得到参数值;
    </script>



    <style>
        body { margin: 0px;}
    </style>
</head>

<body >
<div style="position:absolute;left:10px;top:10px;">
    <a id="viewerPlaceHolder" style="width:690px;height:790px;display:block"></a>
    <script type="text/javascript">
        var flexpaper = new FlexPaperViewer(
                'FlexPaperViewer',//这里表明该FlexPaperViewer.swf是位于项目跟路径下
                'viewerPlaceHolder', { config : {
                    //要显示的SWF文件所在路径, 这里为根目录下的upload文件夹下
                    SwfFile : encodeURI("upload/"+Request("swfName")),
                    ZoomInterval : 0.2,
                    MinZoomSize : 0.2,
                    MaxZoomSize : 1.4,

                    ZoomTime : 0.5,
                    Scale : 1,
                    ZoomTransition : 'easeOut',
                    FitPageOnLoad : true,
                    FitWidthOnLoad : true,
                    FullScreenAsMaxWindow : false,
                    ProgressiveLoading : true,
                    SearchMatchAll : true,
                    ViewModeToolsVisible : true,
                    ZoomToolsVisible : true,
                    NavToolsVisible : true,
                    CursorToolsVisible : true,
                    SearchToolsVisible : true,

                    localeChain: 'zh_CN'
                }});
    </script>
</div>
</body>
</html>

      
      
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71

flexpaper_flash.js, FlexPaperViewer.swf 下载地址

1
0
 
 
查看评论

  暂无评论

发表评论
  • 用 户 名:
  • yuanya
  • 评论内容:
  • 插入代码
  •   
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
  • Godruoyi
  • 个人资料
  •  
    1
    • 访问:13723次
    • 积分:369
    • 等级: 
    • 排名:千里之外
    • 原创:21篇
    • 转载:3篇
    • 译文:0篇
    • 评论:2条
  • 文章分类
  • 最新评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值