模板文件填充 然后生成新文件
//调用PHPword
require_once(ROOTPATH . "inc/vendor/autoload.php");
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('../upfiles/word/template.docx');//模板文件
$templateProcessor->setValue('name', '100');
$templateProcessor->saveAs('../upfiles/word/'.$file.'.docx');//生成新文件
模板文件
${name} 可代替名称
替换完成后的新文件
浏览器下载文件
//将服务器保存的文件下载
output_for_download('../upfiles/word/'.$file.'.docx','预览.docx');
//浏览器下载文件方法
function output_for_download($filename, $title)
{
$file = fopen($filename, "rb");
Header( "Content-type: application/octet-stream ");
Header( "Accept-Ranges: bytes ");
Header( "Content-Disposition: attachment; filename= $title");
while (!feof($file)) {
echo fread($file, 8192);
ob_flush();
flush();
}
fclose($file);
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章