带有PHPWord加载模板的Codeigniter 4

ix0qys7i  于 2022-12-07  发布在  PHP
关注(0)|答案(2)|浏览(129)

我在使用PHPWord在Codeigniter中加载模板文档时遇到问题。下面的代码可以工作,但下载的word文件是空的。我认为问题是模板的位置不正确。谁能告诉我如何正确定位模板文件?模板文件位于public/template/ folder中。

$phpWord  = new \PhpOffice\PhpWord\PhpWord();
$document = $phpWord->loadTemplate(./template/filedoc.docx);
$document->setValue('fullName',  'John Doe');
$document->setValue('date', date("F j Y"));
$xmlWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$xmlWriter->save("php://output");
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="output.docx"');
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');

更新:loadTemplate现在工作正常,没有错误,现在的问题是,为什么下载的doc文件是空的。有人能帮忙吗?

$document = new \PhpOffice\PhpWord\TemplateProcessor(template/filedoc.docx)
$document->setValue('fullName',  'John Doe');
$document->setValue('date', date("F j Y"));
$document->saveAs('output.docx');

更改基于当前PHPWord文档的代码以进行模板处理。上面代码的输出仍然是相同的,空文档或MSWord无法读取。帮助...

uurity8g

uurity8g1#

常数APPPATH
app目录的路径。
代替:

// ...
$document = $phpWord->loadTemplate(./template/filedoc.docx); ❌
// ...

使用此选项:

// ...
$document = $phpWord->loadTemplate(APPPATH . "public/template/filedoc.docx"); ✅
// ...
fcg9iug3

fcg9iug32#

对我有用

$phpWord = new \PhpOffice\PhpWord\PhpWord();

        $section = $phpWord->addSection();

        $titleText = $section->addTextRun(array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::END));
   
        $titleText->addText('helooo ', array('size' => 12, 'rtl' => true));
        $section->addTextBreak(0.5);

 

        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
       
        $objWriter->save("demo.docx'");

其在公用文件夹中创建文件

相关问题