PHPWord中的操作模板 [英] manipulating template in PHPWord

查看:639
本文介绍了PHPWord中的操作模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP的Word文档生成器作为我正在开发的Web应用程序的报告模块.我之所以选择PHPWord,是因为PHPDocX的免费版本功能非常有限,而且页脚处还只是一个免费版本.我有客户提供的模板.我想要的是加载模板并向其中添加动态元素,例如其他文本或表格.我的代码在这里:

I am using a word document generator for PHP for the reports module of the web-app i am developing. I choose PHPWord because the free version of PHPDocX has very limited functionality plus it has a footer that it is only a free version. I have a template given by the client. What I want is I want to load the template and add dynamic elements to it like additional text or tables. My code is here:

<?php
require_once '../PHPWord.php';

$PHPWord = new PHPWord();

$document = $PHPWord->loadTemplate('Template.docx');
$document->setValue('Value1', 'Great');

$section = $PHPWord->createSection();
$section->addText('Hello World!');
$section->addTextBreak(2);

$document->setValue('Value2', $section);

$document->save('test.docx');
?>

我尝试创建一个新部分,并尝试将其分配给template(Value2)中的一个变量,但出现此错误:

I tried to create a new section and tried to assign it to one variable in the template(Value2) but this error appeared:

[28-Jan-2013 10:36:37 UTC] PHP Warning:  utf8_encode() expects parameter 1 to be string, object given in /Users/admin/localhost/PHPWord_0.6.2_Beta/PHPWord/Template.php on line 99

推荐答案

setValue期望第二个参数为纯字符串.无法提供section对象.

setValue expects the second parameter to be a plain string. It's not possible to provide a section object.

我已经深入研究代码,并且没有一种简单的方法来让section对象返回setValue函数可以使用的值.

I've dived into the code and there's not an easy way to have a section object returning a value that could be used by the setValue function.

由于我遇到了同样的问题,我为Template.php文件编写了一个补丁,使您可以在用setValue替换表行之前克隆表行.每行都有一个唯一的ID,可让您识别每行的模板标签.

As I had the same issue, I've wrote a patch for the Template.php file that allows you to clone table rows before replacing their tags with setValue. Each row gets an unique id, allowing you to identify the template tags for each different row.

这是它的工作方式:

将此功能添加到您的Template.php文件(位于PHPWord目录中)

Add this function to your Template.php file (found inside the PHPWord directory)

public function cloneRow($search, $numberOfClones) {
    if(substr($search, 0, 2) !== '${' && substr($search, -1) !== '}') {
        $search = '${'.$search.'}';
    }
    $tagPos      = strpos($this->_documentXML, $search);
    $rowStartPos = strrpos($this->_documentXML, "<w:tr", ((strlen($this->_documentXML) - $tagPos) * -1));
    $rowEndPos   = strpos($this->_documentXML, "</w:tr>", $tagPos) + 7;

    $result = substr($this->_documentXML, 0, $rowStartPos);
    $xmlRow = substr($this->_documentXML, $rowStartPos, ($rowEndPos - $rowStartPos));
    for ($i = 1; $i <= $numberOfClones; $i++) {
        $result .= preg_replace('/\$\{(.*?)\}/','\${\\1#'.$i.'}', $xmlRow);
    }
    $result .= substr($this->_documentXML, $rowEndPos);
    $this->_documentXML = $result;
}

在模板文件中,将每一行用作模板行.假设您在此行中添加了标签$ {first_name}.

In your template file add to each table one row that you will use as a template row. Let's assume you've added a tag ${first_name} in this row.

要获取具有3行的表,请调用: $ document-> cloneRow('first_name',3);

To get a table with 3 rows call: $document->cloneRow('first_name', 3);

模板的工作副本现在已更新为包含3行的表.该行内的每个标签都附加了#和行号.

The working copy of your template is now updated with a table containing 3 rows. Each tag inside the row has been appended with a # and row number.

要设置您的值,请使用setValue $ document-> setValue('first_name#1','第一行上的名称'); $ document-> setValue('first_name#2','第二行上的名称'); $ document-> setValue('first_name#3','第三行上的名称');

To set your values, use setValue $document->setValue('first_name#1', 'Name on the first row'); $document->setValue('first_name#2', 'Name on the second row'); $document->setValue('first_name#3', 'Name on the third row');

我希望这是有用的!我将在此处保留代码和文档的更新版本: http://jeroen.是/phpword-templates-with-repeating-rows/

I hope this is useful! I'll keep an updated version of the code and documentation here: http://jeroen.is/phpword-templates-with-repeating-rows/

这篇关于PHPWord中的操作模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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