【问题标题】:reuse template in the same document [PHPword]在同一文档中重用模板 [PHPword]
【发布时间】:2016-01-19 22:45:03
【问题描述】:

我在我的 Codeigniter 项目中使用 PHPWord 作为库。在我的项目中,我想生成一个 .docx 文件,该文件使用已经在工作的模板。该模板中有一个贴纸,将通过查询数据库中的数据来填充。由于我想在一个文档中生成多个贴纸,我必须复制我已经使用的第一个模板并添加分页符。但它不起作用。我无法打开文档。它说,“内容有问题”

我在这里使用示例模板:

这是我的代码:

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); 

}

我们将非常感谢您的帮助。

【问题讨论】:

  • 试试这个\$section->addPageBreak();
  • @Abdulla,顺便说一句,$section->addObject($document); 正确吗?

标签: codeigniter templates phpword


【解决方案1】:

我知道这是一个老问题,但我一直在寻找类似的答案,发现缺少的链接可以使这个答案起作用,所以我想与你分享。

ejuhjav 的回答原则上是正确的,但是对于今天的当前版本 (v0.16.0),这将起作用。

在您的 word 模板中,您有以下代码块:

${CLONEME}
1.  ${Title}
${/CLONEME}

(“标题”被格式化为编号标题)

现在您想动态插入 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');

我希望它对将来的人有所帮助。

安德烈亚斯

【讨论】:

    【解决方案2】:

    此答案与 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);
    

    【讨论】:

    • 谢谢,我试过了,但它没有克隆贴纸,它仍然显示${cloneblock}
    猜你喜欢
    • 1970-01-01
    • 2019-06-13
    • 2013-01-11
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多