Jeroen Moors

blog

PHPWord – Templates with repeating rows
Written on 13 March 2013

PHPWord is a great library if you want to manipulate Word documents from PHP.

One of the easiest ways to create a document is to use a existing document as a template and the setValue() function to replace tags inside this document.

Unfortunately currently one very useful thing is missing: dynamically adding rows to a table. I’m not a rock-star programmer, I know some of them in person, but I’m not. Still I wrote a quick hack to overcome this issue.

This is how it works:

  1. Create a template Word document, containing the tables you would like to use.
    For each table, add one row to use as a template with the tags you would like to replace. Your template would be something like this: 

    First name Last name
    ${first_name} ${last_name}
  2. Call the cloneRow($tag, $numberOfRows) function before setValue() to duplicate your template row. Provide any tag on that row as $tag and the required number of rows as $number.
    Calling cloneRow('first_name', 3) would transform the working copy of your template into something like this: 

    First name Last name
    ${first_name#1} ${last_name#1}
    ${first_name#2} ${last_name#2}
    ${first_name#3} ${last_name#3}
  3. Now call setValue($tag, $value) adding #rowNumber to the original tags.
    To finish the example: 

    Would result in:

    First name Last name
    Jeroen Moors
    John Doe
    Jane Roe

To get this thing working you’ll have to replace the Template.php file found in side the PHPWord directory by this one Template.php.
Warning: don’t copy-paste it from the block below, as this might result in syntax errors.

12.09.2013 – Thanks to Robert Pecha a bug has been fixed, please download the latest version of Template.php

Any feedback is welcome!