ThinkPHP3使用PHPWord修改模板并下载


下载(直接覆盖到/ThinkPHP目录)

https://files.cnblogs.com/files/sunr/ThinkPHP_PHPWord.zip?t=1662682605

PHP

$row=(new PersonModel())->find(/session('person_id'));
//模板及临时文件目录
$tempPath='./doc_templet/';
//模板文件
$templetFile=$tempPath.'resume1.docx';
//临时文件
$saveFile=$tempPath.'resume_'./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');
//引入模板
$document=new /PhpOffice/PhpWord/TemplateProcessor($templetFile);
//替换变量
$document->setValue('name',$row['person_name']);
$document->setValue('sex',$row['person_sex']);
$document->setValue('time',/date('Y-m-d H:i:s'));
//替换图片
$document->setImageValue('photo_',[
	'path'=>$row['person_photo_'],
	'width'=>120,
	'height'=>180,
]);

//保存临时文件
$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="./site('name').'_简历_'.$row['person_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();
//删除文件
///path_delete($saveFile);

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/288427.html

(0)
上一篇 2022年9月9日
下一篇 2022年9月9日

相关推荐

发表回复

登录后才能评论