linux下php导入带图片的word文档转为html,图片保存下来生成路径。

linux下

yum install libreoffice
yum install libreoffice-headless

如果出现异常,在页面上不一定表现出来,而是页面卡着一直没响应,通过apache日志看到相关错误

[Java framework] Error in function createSettingsDocument (elements.cxx).
javaldx failed!
Warning: failed to read path from javaldx

处理为#因为用户 www没有HOME 会跑到root导致没权限,可指定 export HOME=/tmp/ && 解决

$shell = "export HOME=/tmp/ && /usr/bin/libreoffice  --headless  --convert-to html --print-to-file --printer-name nasty_lowres_printer --outdir {$_SERVER['DOCUMENT_ROOT']}/out_file/  {$full_result_path} > /dev/null 2>/dev/null &";

php如果做了函数安全过滤,需要放行shell_exec
php代码

public function upload(){
    if(IS_POST){
        header('Content-type: text/html; charset=UTF-8');
        $reulst = upload('file');
        //如果成功上传doc,转html
        $full_result_path = $_SERVER['DOCUMENT_ROOT'].$reulst;
        if(file_exists($full_result_path)){
            $shell = "/usr/bin/libreoffice  --headless  --convert-to html --print-to-file --printer-name nasty_lowres_printer --outdir {$_SERVER['DOCUMENT_ROOT']}/out_file/  {$full_result_path}";
            $doc_file_contents = shell_exec($shell);
            $pattern = '/\/(\d+).html/';
            preg_match_all($pattern,$doc_file_contents,$match);
            if(!empty($match[0])){
                $path = ($_SERVER['DOCUMENT_ROOT'].'/out_file'.$match[0][0]);
                $html = file_get_contents($path);
                $img_pattern = '/<img([^<]*)src="([^\"]+)"/i';
                preg_match_all($img_pattern,$html,$match2);
                $base64_arr = $match2[2];
                set_time_limit(120);
                $Qiniu = new Qiniu('xxxxx','xxxxx','xxxxx');
                //七牛日志
                $log = [];
                foreach($base64_arr as $base64){
                    if(strlen($base64) < 60){
                        $html = str_replace($base64,'',$html);
                        $html = preg_replace('/<img src( |="")([^>]*)>/i','',$html);
                    }else{
                        preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64, $ext_result);
                        $file_name = date('YmdHis').'.'. rand(10000, 99999).'.'.$ext_result[2];
                        $qiniukey= date("Ymd").'/doc/'.$file_name;
                        $status = $Qiniu->upload_file($base64,$qiniukey);
                        array_push($log,json_encode($status));
                        if($status['status']=='success'){
                            $status = 'http://cms3.jmcdn.cn/'.$status['key'];
                            $html = str_replace($base64,$status,$html);
                        }else{
                            //如果七牛保存失败,则保存到本地 [by shw 20180709反复测试,有时候会传七牛失败 ,但最后几次均全部成功,可能和网络有关
                            //也可能文件名,测试也是加了随机数文件名之后,确认下,如果还有问题则需要在这里还需要传一次到网上,本地图片无法主站下图]
                            $new_file = "{$_SERVER['DOCUMENT_ROOT']}/out_file/".$file_name;
                            file_put_contents($new_file, base64_decode(str_replace($ext_result[1], '', $base64)));
                            $html = str_replace($base64,"/out_file/".$file_name,$html);
                        }
                    }
                }
                $html = removeFormat($html);
                echo $html;
                print_r($log);die();
            }else{
                die('no match');
            }
        }else{
            echo '没有转换文件,输出 '.$reulst;
        }

        die();
    }
    $this->display();
}

html页面
upload的视图

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>上传文件</title>
    <script type="text/javascript">
        function sub() {
            var obj = new XMLHttpRequest();
            obj.onreadystatechange = function() {
                if (obj.status == 200 && obj.readyState == 4) {
                    var result = obj.responseText;
                    if(result.indexOf("upload") >-1 && result.indexOf("image") >-1){
                        window.opener.document.getElementById("message").innerHTML = result;
                        document.getElementById('con').innerHTML = "上传成功";
                        document.getElementById("a1").style.display = "none";
                        document.getElementById("a2").style.display = "block";
                    }else{
                        document.getElementById('con').innerHTML = result;
                    }

                }
            }
            // 通过Ajax对象的upload属性的onprogress事件感知当前文件上传状态
            obj.upload.onprogress = function(evt) {
                // 上传附件大小的百分比
                var per = Math.floor((evt.loaded / evt.total) * 100) + "%";
                // 当上传文件时显示进度条
                document.getElementById('parent').style.display = 'block';
                // 通过上传百分比设置进度条样式的宽度
                document.getElementById('son').style.width = per;
                // 在进度条上显示上传的进度值
                document.getElementById('son').innerHTML = per;
            }
            // 通过FormData收集零散的文件上传信息
            var fm = document.getElementById('userfile3').files[0];
            var fd = new FormData();
            fd.append('userfile', fm);
            obj.open("post", "/index.php?g=portal&m=shw&a=upload");
            obj.send(fd);
        }
    </script>
    <style type="text/css">
        #parent {
            width: 200px;
            height: 20px;
            border: 2px solid gray;
            background: lightgray;
            display: none;
        }
        #son {
            width: 0;
            height: 100%;
            background: lightgreen;
            text-align: center;
        }
    </style>
</head>
<body>
<h2>图片上传</h2>
<div id="parent">
    <div id="son"></div>
</div>
<p id="con"></p>
<div id="a1">
    <input type="file" name="userfile" id="userfile3"><br><br>

    <input type="button" name="btn" value="文件上传" onclick="sub()">
</div>

<div id="a2" style="display: none">
    <input type="button" name="btn" value="关闭窗口,返回继续编辑" onclick="window.close()">
</div>

</body>
</html>

html页面调用者

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div>
    <input type="button" value="上传"  class="btn btn-primary" onclick="upload()">
    <script>
        function upload(){
            window.open("/index.php?g=portal&m=shw&a=upload","上传文档","width=500,height=480,screenX=400,screenY=100");
        }
    </script>
    <div id="message"></div>
</div>
</body>
</html>

标签: word2html

非特殊说明,本博所有文章均为博主原创。

最新文章

发表评论