在同一文档中重复使用模板 [PHPword]
reuse template in the same document [PHPword]
我在我的 Codeigniter 项目中使用 PHPWord 作为库。在我的项目中,我想生成一个使用已经可用的模板的 .docx 文件。在该模板中有一个 Sticker,它将通过查询数据库中的数据来填充。由于我想在一个文档中生成多个贴纸,我必须复制我已经使用的第一个模板并添加一个分页符。但它不起作用。我无法打开文档。它说,"problem with its content".
我在这里使用示例模板:
这是我的代码:
public function Export(){
$this->load->library('PHPWord');
$this->load->model('person_model');
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$persons= $this->person_model->DataSticker();
$document = $PHPWord->loadTemplate('application/controllers/Sticker.docx');
$counter=1;
foreach($persons as $row){
$this->Doc($document,$counter,$row);
$counter+=1;
if($counter>6){
$counter=1;
$section->addObject($document);
$section->addPageBreak();
$document = $PHPWord->loadTemplate('application/controllers/Sticker.docx');
$this->Doc($document,$counter,$row);
}
}
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$filename = 'nameOfFile.docx';
$objWriter->save( $filename);;
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
unlink($filename);
}
public function Doc($document,$counter,$row){
$document->setValue('Fname'.$counter, $row->fname);
$document->setValue('Mname'.$counter, $row->mname);
$document->setValue('Lname'.$counter, $row->lname);
$document->setValue('DOB'.$counter, $row->dob);
$document->setValue('POB'.$counter,$row->pob);
$document->setValue('Age'.$counter, $row->age);
$document->setValue('School'.$counter, $row->school);
}
非常感谢您的帮助。
此答案与 0.12.0 版本相关(可能也适用于 0.13.0)...您可能正在使用一些旧版本,因为您正在使用已弃用的 loadTemplate 函数。
为了回答您最新的问题,我认为您不能将模板与 addObject 一起使用(好吧,不是 100% 肯定,但引用某些)。但是一种可行的方法是使用单个模板文件,其中定义了贴纸的一个页面模板(包括分页符)并用模板块标记包裹起来,即:
${CLONEBLOCK}
Here is your sticker page definition (6 stickers + pagebreak)...
${/CLONEBLOCK}
然后在您的代码中,您可以根据需要多次克隆此块:
// the class path is probably something different with your codeigniter
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('application/controllers/Sticker.docx');
$templateProcessor->cloneBlock('CLONEBLOCK', count($persons)/6);
您可以非常相似地分配模板值:
// note! add the limit 1 as the third parameter (otherwise it will
// write the this value on all of the pages)
$templateProcessor->setValue('Mname'.$counter, $row->mname, 1);
我知道这是一个老问题,但我一直在寻找类似的答案并找到了缺失的 link 来使这个答案有效,所以我想与您分享。
ejuhjav 的回答原则上是正确的,但对于今天的当前版本 (v0.16.0) 这将有效。
在您的word模板中,您有以下代码块:
${CLONEME}
1. ${Title}
${/CLONEME}
("title" 被格式化为带编号的标题)
现在您想动态插入 x 个标题,对吧?所以你做的很简单。您将块克隆 x 次,然后为每次迭代设置值。其他答案中的错误是 setValue 方法用作简单的搜索和替换。所以基本上你总是希望替换它找到的 first ${Title} 占位符并移动到下一个 - 但你必须确保搜索和替换仅限于第一个发现像这样:
$templateProcessor->setValue('Title', "Testtitel 1", 1);
然后基本上会遍历每个克隆的标题。
这很完美,当然这不是动态完成的,原因是虚拟的:
// Template processor instance creation
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Sample_23_TemplateBlock.docx');
$templateProcessor->cloneBlock('CLONEME', 3);
$templateProcessor->setValue('Title', "Testtitel 1", 1);
$templateProcessor->setValue('Title', "Testtitel 2", 1);
$templateProcessor->setValue('Title', "Testtitel 3", 1);
$templateProcessor->saveAs('ClonedHeadings.docx');
我希望它对以后的人有所帮助。
安德烈亚斯
我在我的 Codeigniter 项目中使用 PHPWord 作为库。在我的项目中,我想生成一个使用已经可用的模板的 .docx 文件。在该模板中有一个 Sticker,它将通过查询数据库中的数据来填充。由于我想在一个文档中生成多个贴纸,我必须复制我已经使用的第一个模板并添加一个分页符。但它不起作用。我无法打开文档。它说,"problem with its content".
我在这里使用示例模板:
这是我的代码:
public function Export(){
$this->load->library('PHPWord');
$this->load->model('person_model');
$PHPWord = new PHPWord();
$section = $PHPWord->createSection();
$persons= $this->person_model->DataSticker();
$document = $PHPWord->loadTemplate('application/controllers/Sticker.docx');
$counter=1;
foreach($persons as $row){
$this->Doc($document,$counter,$row);
$counter+=1;
if($counter>6){
$counter=1;
$section->addObject($document);
$section->addPageBreak();
$document = $PHPWord->loadTemplate('application/controllers/Sticker.docx');
$this->Doc($document,$counter,$row);
}
}
// Save File
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$filename = 'nameOfFile.docx';
$objWriter->save( $filename);;
header('Content-Description: File Transfer');
header('Content-type: application/force-download');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($filename));
readfile($filename);
unlink($filename);
}
public function Doc($document,$counter,$row){
$document->setValue('Fname'.$counter, $row->fname);
$document->setValue('Mname'.$counter, $row->mname);
$document->setValue('Lname'.$counter, $row->lname);
$document->setValue('DOB'.$counter, $row->dob);
$document->setValue('POB'.$counter,$row->pob);
$document->setValue('Age'.$counter, $row->age);
$document->setValue('School'.$counter, $row->school);
}
非常感谢您的帮助。
此答案与 0.12.0 版本相关(可能也适用于 0.13.0)...您可能正在使用一些旧版本,因为您正在使用已弃用的 loadTemplate 函数。
为了回答您最新的问题,我认为您不能将模板与 addObject 一起使用(好吧,不是 100% 肯定,但引用某些)。但是一种可行的方法是使用单个模板文件,其中定义了贴纸的一个页面模板(包括分页符)并用模板块标记包裹起来,即:
${CLONEBLOCK}
Here is your sticker page definition (6 stickers + pagebreak)...
${/CLONEBLOCK}
然后在您的代码中,您可以根据需要多次克隆此块:
// the class path is probably something different with your codeigniter
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('application/controllers/Sticker.docx');
$templateProcessor->cloneBlock('CLONEBLOCK', count($persons)/6);
您可以非常相似地分配模板值:
// note! add the limit 1 as the third parameter (otherwise it will
// write the this value on all of the pages)
$templateProcessor->setValue('Mname'.$counter, $row->mname, 1);
我知道这是一个老问题,但我一直在寻找类似的答案并找到了缺失的 link 来使这个答案有效,所以我想与您分享。
ejuhjav 的回答原则上是正确的,但对于今天的当前版本 (v0.16.0) 这将有效。
在您的word模板中,您有以下代码块:
${CLONEME}
1. ${Title}
${/CLONEME}
("title" 被格式化为带编号的标题)
现在您想动态插入 x 个标题,对吧?所以你做的很简单。您将块克隆 x 次,然后为每次迭代设置值。其他答案中的错误是 setValue 方法用作简单的搜索和替换。所以基本上你总是希望替换它找到的 first ${Title} 占位符并移动到下一个 - 但你必须确保搜索和替换仅限于第一个发现像这样:
$templateProcessor->setValue('Title', "Testtitel 1", 1);
然后基本上会遍历每个克隆的标题。
这很完美,当然这不是动态完成的,原因是虚拟的:
// Template processor instance creation
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('Sample_23_TemplateBlock.docx');
$templateProcessor->cloneBlock('CLONEME', 3);
$templateProcessor->setValue('Title', "Testtitel 1", 1);
$templateProcessor->setValue('Title', "Testtitel 2", 1);
$templateProcessor->setValue('Title', "Testtitel 3", 1);
$templateProcessor->saveAs('ClonedHeadings.docx');
我希望它对以后的人有所帮助。
安德烈亚斯