phpword

使用phpword一定要使用composer安装,一定要使用composer安装,要使用composer安装
phpword可以使用封装的方法向里面添加特定的内容,也可以使用html生成,还可以使用模板替换(下面就是使用的模板替换)
如果对于格式没有严格的要求或者文件格式不复杂可以使用phpword的方法向里面添加内容,如果文件复杂可以使用模板替换的方法,使用html生成可能对html的格式要求比较严格,否则容易乱码(没仔细研究,有研究的大佬可以可以在下面评论指点一下)。
最后面的是phpword设置格式的代码(因为没有找到比较友好的文档所以特地在这里写出来)

 

    /**
     *notes:使用模板替换生成word
     */
    public function wordTemplate($item)
    {
        require_once '/data/wwwroot/vhost/sues.seabig.cn/htdocs/api/PHPWord/vendor/autoload.php';
        //实例化, 参数传入模板文件地址
        $templateProcessor = new TemplateProcessor('/data/wwwroot/vhost/sues.seabig.cn/htdocs/api/PHPWord/test.docx');
//                    $student = $this->getRow("customers", $item['student_id']); //查询学生
        //处理任务书
        $task = $this->task($item['id']);
//                $templateProcessor->setValue('aim', 'gfgaadsfdsfa');
        foreach ($task as $key => $item) {
            switch ($key) {
                case 'aim':
                case 'content':
                case 'technology':
                case 'plan':
                case 'reference':
                    for ($i = 0; $i <= 12; $i++) {
                        $templateProcessor->setValue($key . $i, $item[$i]);
                    }
                    break;
                case 'major_sign':
                case 'major_agree':
                case 'leader_sign':
                case 'leader_agree':
                case 'student_sign':
                case 'teacher_sign':
                    if ($item != '') {
                        $templateProcessor->setImageValue($key, ['src' => 'http://sues.seabig.cn/uploads/' . $item, 'width' => 35, 'height' => 25]);
                    } else {
                        $templateProcessor->setValue($key, $item);
                    }
                    break;
                default:
                    $templateProcessor->setValue($key, $item);
                    break;
            }
        }
        $file = $templateProcessor->save();
        return $file;
    }

    /**
     * notes:批量下载任务书
     */
    public function assignmentDownload()
    {
        $this->_globals();
        $zipname = 'task.zip';
        $zip = new ZipArchive;
        $zip->open($zipname, ZipArchive::CREATE);//创建新的压缩文件
        for($i=0;$i<$zip->numFiles;$i++){
            $zip->deleteIndex($i);
        }
        if ($_SESSION['login_admin']['roleid'] == '4') { //如果是系主任
            //查询系主任所在专业的学生
            $student = $this->mBasic->getlist("customers", array("speciality_id={$_SESSION['login_admin']['speciality_id']} AND roleid = '10000'"), false);
            if ($student != null) {
                foreach ($student as $item) {
                    $topics = $this->mBasic->getRow("thesis_topics", array("student_id={$item['id']}")); //查询学生
                    $file = $this->wordTemplate($topics);
                    $zip->addFile($file, "{$item['sn']}{$item['name']}.docx");
                }
            }
        } elseif ($_SESSION['login_admin']['roleid'] == '5') { //如果是院长
            //查询系主任所在专业的学生
            $student = $this->mBasic->getlist("customers", array("speciality_id={$_SESSION['login_admin']['dept_id']} AND roleid = '10000'"), false);
            if ($student != null) {
                foreach ($student as $item) {
                    $topics = $this->mBasic->getRow("thesis_topics", array("student_id={$item['id']}")); //查询学生
                    $file = $this->wordTemplate($topics);
                    $zip->addFile($file, "{$item['sn']}{$item['name']}.docx");
                }
            }
        }else{ //如果不是系主任也不是院长直接查询是否有指导的学生
            $topics = $this->mBasic->getlist("thesis_topics", array("teacher_id={$_SESSION['login_admin']['id']}"), false); //查询老师指导的学生
            if ($topics != null) {
                foreach ($topics as $item) {
//                    $student = $this->getRow("customers", $item['student_id']); //查询学生
                    $student = $this->mBasic->getRow("customers", array("id={$item['student_id']}")); //查询学生
                    //处理任务书
                    $file = $this->wordTemplate($item);
                    $zip->addFile($file, "{$student['sn']}{$student['name']}.docx");
                }
            }
        }
        $zip->close();
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $zipname);
        header('Content-Length: ' . filesize($zipname));
        @readfile($zipname);
    }

    /**
     * 任务书信息
     * @param $id //论文id
     * @return mixed
     */
    public function task($id)
    {
        $this->_globals();
        $topic = $this->getRow("thesis_topics", $id);
        $student = $this->getRow("customers", $topic['student_id']);
        $teacher = $this->getRow("customers", $topic['teacher_id']);
        $task = $this->mBasic->getRow("thesis_assignment", array("tid={$id}"));
        $task['topic_name'] = $topic['name']; //论文题目
        $task['aim'] = $this->filter($task['aim']); //目的要求
        $task['content'] = $this->filter($task['content']); //主要工作内容
        $task['technology'] = $this->filter($task['technology']); //技术指标
        $task['plan'] = $this->filter($task['plan']); //进度计划
        $task['reference'] = $this->filter($task['reference']); //参考资料
        $task['dept'] = $this->mBasic->getName("edu_majors", $student['dept_id']); //学院
        $task['major'] = $this->mBasic->getName("edu_majors", $student['speciality_id']); //专业班级
        $task['t_name'] = $teacher['name']; //教师姓名
        $task['t_title'] = $this->mBasic->getName("parameters", $teacher['title_id']); //教师职称
        $task['signpic'] = $teacher['signpic']; //教师签名
        $task['secTeacher_name'] = $topic['secTeacher']; //校外教师职称
        $task['secTitle'] = $topic['secTitle']; //校外教师职称
        $task['secSign'] = $topic['secSign']; //校外教师签字
//            $student['dept'] = $this->mBasic->getName('edu_majors', $student['dept_id']);
//            $student['major'] = $this->mBasic->getName('edu_majors', $student['speciality_id']);
        $task['student_name'] = $student['name']; //学生姓名
        $task['student_sn'] = $student['sn']; //学生
        $task['major_sign'] = $task['major_opinion'] == 1 ? $this->mBasic->getField("customers", array("find_in_set(4,roleid)", "speciality_id={$student['speciality_id']}"), "signpic") : '';
        $task['major_agree'] = $task['major_opinion'] == 1 ? $this->mBasic->getField("customers", array("find_in_set(4,roleid)", "speciality_id={$student['speciality_id']}"), "agreepic") : '';
        $task['leader_sign'] = $task['dept_opinion'] == 1 ? $this->mBasic->getField("customers", array("find_in_set(5,roleid)"), "signpic") : '';
        $task['leader_agree'] = $task['dept_opinion'] == 1 ? $this->mBasic->getField("customers", array("find_in_set(5,roleid)"), "agreepic") : '';
        $task['student_sign'] = $task['studentSignDate'] != '0000-00-00' ? $this->mBasic->getField("customers", array("id={$student['id']}"), "signpic") : ''; //学生签字
        $task['teacher_sign'] = $task['teacherSignDate'] != '0000-00-00' ? $this->mBasic->getField("customers", array("id={$teacher['id']}"), "signpic") : ''; //教师签字

        if ($task['studentSignDate'] != '0000-00-00') {
            //学生签字日期
            $task['student_Y'] = date("Y", strtotime($task['studentSignDate']));
            $task['student_m'] = date("m", strtotime($task['studentSignDate']));
            $task['student_d'] = date("d", strtotime($task['studentSignDate']));
        } else {
            $task['student_Y'] = '__';
            $task['student_m'] = '__';
            $task['student_d'] = '__';
        }

        if ($task['teacherSignDate'] != '0000-00-00') {
            //老师签字日期
            $task['teacher_Y'] = date("Y", strtotime($task['teacherSignDate']));
            $task['teacher_m'] = date("m", strtotime($task['teacherSignDate']));
            $task['teacher_d'] = date("d", strtotime($task['teacherSignDate']));
        } else {
            $task['teacher_Y'] = '__';
            $task['teacher_m'] = '__';
            $task['teacher_d'] = '__';
        }

        if ($task['starttime'] == "0000-00-00" || $task['log']['endtime'] == "0000-00-00") {
            $start = '';
            $end = '';
        } else {
            $start = $task['starttime'];
            $end = $task['endtime'];
        }
        //开始时间
        $task['start_Y'] = date("Y", strtotime($start));
        $task['start_m'] = date("m", strtotime($start));
        $task['start_d'] = date("d", strtotime($start));
        $task['start'] = $start;
        //结束时间
        $task['end_Y'] = date("Y", strtotime($end));
        $task['end_m'] = date("m", strtotime($end));
        $task['end_d'] = date("d", strtotime($end));
        $task['end'] = $end;
        return $task;
    }

    /**
     * 过滤富文本标签
     * @param $data
     * @return string
     */
    public function filter($data)
    {
        if ($data == null) {
            return null;
        }
        $content_01 = $data;//从数据库获取富文本content
        $content_02 = htmlspecialchars_decode($content_01); //把一些预定义的 HTML 实体转换为字符
        $content_03 = str_replace(" ", "", $content_02); //将空格替换成空
        $content_04 = str_replace(" ", "", $content_03); //将空格替换成空
//        $content_04 = str_replace(" ", "", $content_04); //将空格替换成空
//        $contents= strip_tags($content_04);
        $content_05 = explode('<br />', $content_04); //将段落分割
//        $content_05 = preg_split('/[<br />]+/is', $content_04);; //将段落分割
        foreach ($content_05 as &$item) {
            $contents[] = strip_tags($item); //函数剥去字符串中的 HTML、XML 以及 PHP 的标签,获取纯文本内容
        }
        return $contents;
    }

  

posted @ 2019-12-19 15:05  北满  阅读(705)  评论(0)    收藏  举报