php 在 PHPWord 中操作模板

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14560142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:32:18  来源:igfitidea点击:

manipulating template in PHPWord

phpms-wordphpword

提问by John Micah Fernandez Miguel

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 的 word 文档生成器。我选择 PHPWord 是因为 PHPDocX 的免费版本功能非常有限,而且它有一个页脚,表明它只是一个免费版本。我有客户提供的模板。我想要的是我想加载模板并向其中添加动态元素,如附加文本或表格。我的代码在这里:

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

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

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

[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

回答by Jeroen Moors

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

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.

我已经深入研究了代码,没有一种简单的方法可以让部分对象返回一个可由 setValue 函数使用的值。

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 文件编写了一个补丁,允许您在用 s​​etValue 替换标签之前克隆表行。每行都有一个唯一的 id,允许您识别每个不同行的模板标签。

This is how it works:

这是它的工作原理:

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

将此函数添加到您的 Template.php 文件(在 PHPWord 目录中找到)

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

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.

在您的模板文件中,向每个表添加一行,您将用作模板行。假设您已在此行中添加了一个标签 ${first_name}。

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

要获得 3 行的表格,请调用: $document->cloneRow('first_name', 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.

模板的工作副本现在更新为包含 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');

要设置您的值,请使用 setValue $document->setValue('first_name#1', 'Name on the first row'); $document->setValue('first_name#2', '第二行的名字'); $document->setValue('first_name#3', '第三行的名字');

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/

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

回答by Daniel Cisneros Medina

According to the documentation, you can't add content to the file when you are working with templates.

根据文档,在使用模板时不能向文件添加内容。

It is not possible to add new PHPWord elements to a loaded template file.

无法向加载的模板文件中添加新的 PHPWord 元素。

Documentation

文档

回答by Korimizu

Totally new version CloneRow and setValue

全新版本 CloneRow 和 setValue

Now you can clone merged cells. Many bugs with OOXML tags was fixed.

现在您可以克隆合并的单元格。与 OOXML 标签的许多错误是固定的。

And new method setValue - now ignoring trash-tags inside your pattern. Like

还有新方法 setValue - 现在忽略模式中的垃圾标签。喜欢

{My<trash ooxml tags>Pattern}

You can find code, docs and examples here: https://github.com/Arisse/PHPWord_CloneRow

您可以在此处找到代码、文档和示例:https: //github.com/Arisse/PHPWord_CloneRow