大概步骤如下
1 tp框架导入phpoffice/phpword包
composer require phpoffice/phpword
2 利用word模板导出生成word文档,其中包含文字、图片、多段落、循环试题
$path = "./storage/file/7月份驾驶员培训结业报告.docx"
$templatePath = "./storage/template/driver_report_template.docx";
$tmp = new TemplateProcessor($templatePath);
$tmp->setValue('username', $data['username']);
$tmp->setValue('password', $data['password']);
$tmp->setImageValue("picture_url",
['path' => "." . $data['picture_url'],
'width' => 100, 'height' => 140]);
$content = str_replace(["\r\n", "\r", "\n"], '<w:br />', $data['content']);
$tmp->setValue('content', $content);
$total = count($test);
$tmp->cloneRow('num', $total);
$num = 1;
foreach ($test as $key => $val ) {
$tmp->setValue('num#' . $num, $num);
$tmp->setValue('title#' . $num, $val['title']);
$tmp->setValue('a#' . $num, $val['a']);
$tmp->setValue('b#' . $num, $val['b']);
$tmp->setValue('c#' . $num, $val['c']);
$tmp->setValue('d#' . $num, $val['d']);
$tmp->setValue('res#' . $num, strtoupper($val['res']));
$tmp->setValue('answer#' . $num, strtoupper($val['answer']));
$num += 1;
}
$tmp->saveAs($filePath);
|