重用模板在同一个文档[PHPword] [英] reuse template in the same document [PHPword]

查看:1663
本文介绍了重用模板在同一个文档[PHPword]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的Codeigniter项目中使用PHPWord作为库。在我的项目中,我想生成一个使用已经工作的模板的.docx文件。在那个模板中是一个Sticker,它将通过查询数据库中的数据来填充。因为我想在一个文档中生成多个贴纸,我必须复制我已经使用的第一个模板,并添加一个分页。但它不工作。我无法打开文档。

I am using PHPWord as a library in My Codeigniter Project. In my project, I want to generate a .docx file that uses a template which is already working. In that template is a Sticker that will be filled up by querying the data from the database. Since I want to generate multiple stickers in one document, I have to copy the first template that I have already used and add a pagebreak. But it is not working. I can't open the document. It says, "problem with its content".

我在这里使用示例模板:

I have here my working the sample template:

这是我的代码:

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

}

我们非常感谢您的帮助。

Your help will be highly appreciated.

推荐答案

这个答案与0.12.0版本相关(可能也适用于0.13.0)可能使用一些旧版本,因为你使用已弃用的loadTemplate函数。

This answer is related to 0.12.0 version (probably applies as well to 0.13.0 as well)... You might be using some older version as you are using the deprecated loadTemplate function.

为了回答你最新的问题,我不认为你可以使用模板与addObject (确定,不是100%确定,但报价确定)。但是一个有效的方法是使用单个模板文件,其中定义了您的贴纸的一个页面模板(包括您的pagebreak)并用模板块标签包装,例如:

To answer you latest question, I don't think that you can use the templates with the addObject (ok, not 100% sure about it, but quote certain). But a working approach would be to use single template file where you have one page template of your stickers defined (including your pagebreak) and wrapped around with template block tags, i.e:

${CLONEBLOCK}
Here is your sticker page definition (6 stickers + pagebreak)...
${/CLONEBLOCK}

然后在你的代码中,你可以根据需要多次克隆这个块:

And then in your code you would just clone this block as many times as needed:

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

您可以非常类似地分配模板值:

And you would assign your template values quite similarly:

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

这篇关于重用模板在同一个文档[PHPword]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆