ps:前段时间项目有这个需求,在网上看到的做法 这里整理记录下
准备:
- composer 安装 PhpOffice/PhpWord
- 一份word 模板(提前写好变量占位)
(我理解的)原理:其实就是加载模板 替换变量 输出/保存
例图:
代码:
use PhpOffice\PhpWord\TemplateProcessor;
public function dayin(){
$id = $this->request->param('id', 0, 'intval');
$report_info =Db::name('project_report')->where("id", $id)->find();
if(empty($report_info)){
return json(['code'=>0,'msg'=>'网络延迟,请稍后重试']);
}
$tpl = public_path() . 'static/项目打印模板.docx';
$doc = new TemplateProcessor($tpl);//打开模板
$doc->setValue('project_no', $report_info['project_no']);
$doc->setValue('project_name', $report_info['project_name']);
$doc->setValue('create_time', date('Y-m-d',$report_info['create_time']));
$doc->setValue('industry_type', $report_info['industry_type']);
$doc->setValue('proposed_location', $report_info['proposed_location']);
$doc->setValue('area_requirement', $report_info['area_requirement']);
$doc->setValue('national_policy', $report_info['national_policy']);
$doc->setValue('market_expectation', $report_info['market_expectation']);
$doc->setValue('fixed_investment', $report_info['fixed_investment']);
$doc->setValue('estimated_annual_tax', $report_info['estimated_annual_tax']);
$doc->setValue('project_desc', $report_info['project_desc']);
$doc->setValue('legal_person', $report_info['legal_person']);
$doc->setValue('corporate_reputation', $report_info['corporate_reputation']);
$doc->setValue('industry_position', $report_info['industry_position']);
$doc->setValue('technological_innovation', $report_info['technological_innovation']);
$doc->setValue('enterprise_desc', $report_info['enterprise_desc']);
$doc->setValue('first_docking_people', $report_info['first_docking_people']);
$doc->setValue('first_docking_people_mobile', $report_info['first_docking_people_mobile']);
$doc->setValue('project_housekeeper', $report_info['project_housekeeper']);
$doc->setValue('enterprise_principal', $report_info['enterprise_principal']);
$doc->setValue('enterprise_principal_mobile', $report_info['enterprise_principal_mobile']);
$doc->setValue('project_class', $report_info['project_class']);
$doc->setValue('progress', $report_info['progress']);
$doc->setValue('exist_problem', $report_info['exist_problem']);
$doc->setValue('project_promotion_plan', $report_info['project_promotion_plan']);
$deptA=$deptB=$deptC=$deptD=$deptE=$deptF = '□';
switch ($report_info['project_status']){
case 1:
//$deptA = '☑';
$deptA = '√';
break;
case 2:
$deptB = '√';
break;
case 3:
$deptB = '√';
break;
case 4:
$deptB = '√';
break;
case 5:
$deptB = '√';
break;
case 6:
$deptB = '√';
break;
default:
break;
}
$doc->setValue('deptA', $deptA);
$doc->setValue('deptB', $deptB);
$doc->setValue('deptC', $deptC);
$doc->setValue('deptD', $deptD);
$doc->setValue('deptE', $deptE);
$doc->setValue('deptF', $deptF);
// $doc->saveAs('test.docx');//另存为
// return json(['code'=>1]);
$filepath = $doc->save();
$fileName = "项目基本情况.docx";
// 下载文件
ob_clean();
header('Pragma: public');
header("Access-Control-Expose-Headers: Content-Disposition");
header("Accept-Ranges: bytes");
header('ResponseType: blob');
header('Last-Modified:' . gmdate('D, d M Y H:i:s') . 'GMT');
header('Cache-Control:no-store, no-cache, must-revalidate');
header('Cache-Control:pre-check=0, post-check=0, max-age=0');
header('Content-Transfer-Encoding:binary');
header('Content-Encoding:none');
header('Content-type:multipart/form-data');
header("Content-Disposition:attachment; filename=" . urlencode($fileName));//设置下载的默认文件名
header('Content-length:' . filesize($filepath));
$fp = fopen($filepath, 'r');
while (connection_status() == 0 && $buf = @fread($fp, 8192)) {
echo $buf;
}
fclose($fp);
@unlink($filepath);
}