我可以使用PHPWord生成下载从头开始生成的word文档: 创建PhpWord对象:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
添加部分和行(省略)并创建标题:
$datetime = date('Y_M_d_G_i');
$filename = 'receipt_' . $datetime . '.docx';
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="' . $filename . '"');
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
从phpWord对象中创建一个编写器:
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
写入输出:
$xmlWriter->save("php://output");
我未能做的是从服务器上的模板下载文档: 创建TemplateProcessor工作原理:没有错误,PHP将$ templateProcessor识别为该类的对象:
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor(asset_url() . 'templates/receipt_template.docx');
但我无法弄清楚如何写输出。如果我可以生成PhpWord对象,那么我可以使用IOFactory :: createWriter方法,但TemplateProcessor没有返回PhpWord对象的方法。
我能得到的最接近的是尝试从IOFactory :: load中创建一个$ phpWord文档。但这只会创建一个空白文档
$phpWord = \PhpOffice\PhpWord\IOFactory::load($templateProcessor->save());
答案 0 :(得分:1)
<?
require_once "../include/PHPWord-develop/bootstrap.php";
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('contrato01.docx');
$templateProcessor->setValue('variable01', 'Sun');
$templateProcessor->setValue('variable02', 'Mercury');
//#####################################################
// Save File
//#####################################################
//#####################################################
header("Content-Disposition: attachment; filename='ejemplo.docx'");
$templateProcessor->saveAs('php://output');
//#####################################################
//#####################################################
?>