首页 / 编程开发 / PHP导入带图片的word文档转成html并本地化图片

PHP导入带图片的word文档转成html并本地化图片

2023-04-25 11:51:22编程开发 阅读 662

服务器安装必要的依赖


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, tp 控制器upload

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];
						$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);
                        
                    }
                }
						echo $html;
						print_r($log);die();
					}else{
						die('no match');
					}
				}else{
					echo '没有转换文件,输出 '.$reulst;
				}

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

tp 视图 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=test&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>
调用页

<!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=test&a=upload","上传文档","width=500,height=480,screenX=400,screenY=100");
        }
    </script>
    <div id="message"></div>
</div>
</body>




本文《PHP导入带图片的word文档转成html并本地化图片》由爱思考吧 isres.com 分享,转载请注明出处。本文网址:https://www.isres.com/php/37.html

本站主要收集测评能够节省时间和提升效率的软件工具并分享使用与学习的过程和感受,任何情况下用户都需遵守所使用软件资源的相关协议。与《PHP导入带图片的word文档转成html并本地化图片》有关的本站文章及资源仅供个人学习交流使用,相关资源请下载后24小时内删除,请勿用于其它用途,因此产生的任何问题由您自行承担。

猜你喜欢