您好,欢迎访问本站博客!联系QQ:1343013953
  • 四象限时间管理法则:①重要且紧急的事;②重要但不紧急的事;③不重要但紧急的事;④不重要且不紧急的事

thinkphp3.2使用PhpOffice以word为模板替换相关内容生成新word

Thinkphp wujinhua 2023-06-29 175 次浏览 0个评论
网站分享代码
 $tempPath='./doc_zs/';
        //模板文件
        $templetFile=$tempPath.'zydjzs.docx';
        //临时文件
        $saveFile=$tempPath.'zydjzs_'.\session('person_id').'.docx';
        //加载PHPWord
        require_once('./ThinkPHP/Library/Vendor/phpword/PhpWord.php');
        require_once('./ThinkPHP/Library/Vendor/phpword/Settings.php');
        require_once('./ThinkPHP/Library/Vendor/phpword/TemplateProcessor.php');
        require_once('./ThinkPHP/Library/Vendor/phpword/Shared/ZipArchive.php');
        require_once('./ThinkPHP/Library/Vendor/phpword/Shared/Text.php');
        //引入模板  ThinkPHP/Library/Org/Util目录下面
        $document=new \PhpOffice\PhpWord\TemplateProcessor($templetFile);
        //替换变量
        $fzrq=date('Y-m-d',$list['fzrq']);
        $document->setValue('name',$list['name']);
        $document->setValue('sfz',$list['sfz']);
        $document->setValue('bh',$list['bh']);
        $document->setValue('fzrq',$fzrq);
        $document->setValue('zjlx','居民身份证');
        $document->setValue('zy',$list['zy']);
        $document->setValue('jb',$list['jb']);
        $document->setValue('zsbh',$list['zsbh']);

        $document->setImageValue('zp',[
        	'path'=>'http://www.ndxlpx.com/'.$zp['zp'],
            'width'=>145,
        	'height'=>165,
        ]);
        /*
        $document->setValue('sex',$row['person_sex']);
        $document->setValue('time',\date('Y-m-d H:i:s'));
        //替换图片
        
        */
        //保存临时文件
        $document->saveAs($saveFile);
        //下载(直接输出到浏览器)
        ob_clean();
        ob_start();
        $fp = fopen($saveFile,"r");
        $file_size = filesize($saveFile);
        header("Content-type:application/octet-stream");
        header("Accept-Ranges:bytes");
        header("Accept-Length:".$file_size);
        header("Content-Disposition:attchment; filename=".$list['name'].'职业技能等级证书.docx');
        $buffer = 1024;
        $file_count = 0;
        while (!feof($fp) && $file_count < $file_size){
        	$file_con = fread($fp,$buffer);
        	$file_count += $buffer;
        	echo $file_con;
        }
        fclose($fp);
        ob_end_flush();

附近下载地址:http://mxqcseo.com/PhpOffice.tar.gz

已有 175 位网友参与,快来吐槽:

发表评论

站点统计