Phpword: New line with templateProcessor

Created on 23 Mar 2018  路  17Comments  路  Source: PHPOffice/PHPWord

This is:

  • [x] a bug report
  • [ ] a feature request
  • [ ] not a usage question (ask them on https://stackoverflow.com/questions/tagged/phpword)

Expected Behavior

$myString = "Test-X n Test-Y"
`Test-X'
'Test-Y'

Current Behavior

$myString = "Test-X n Test-Y"
`Test-XTest-Y'

838 <w:br/>\n and </w:t><w:br/><w:t> don't work. the problem is the same.

Failure Information

When I use the template processor, I can't insert a new line with a string.

How to Reproduce.

<?php
$templateProcessor->setValue('txt_agent_detail_evalB#'.$i.'', "test1 \n test2");
?>

Context

  • PHP version: 7
  • PHPWord version: 0.14

Most helpful comment

Here is my solution:

$templateProcessor->setValue('foo', 'Lorem.${newline}Ipsum....');

$new_line = new \PhpOffice\PhpWord\Element\PreserveText('</w:t><w:br/><w:t>');

$templateProcessor->setComplexValue('newline', $new_line);

I hope it helps.

All 17 comments

+1

Hi,

Any news about it ?

Thanks

+1
Also the </w:t><w:br/><w:t> solution doesn't seem to work with
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
since the tags are escaped then.

+1

+1, any news ?

Did anyone came up with a fix or workaround for this already?

Since I need this urgently I came up with a dirty workaround for this problem.

As I see it the exact problem is the following:

  1. I want to add newlines to stuff I add via TemplateProcessor (which works by adding </w:t><w:br/><w:t>).
  2. But I also want to use outputEscapingEnabled = true in phpword.ini so a simple & sign in the input doesn't break my word file.

Yet, output escaping uses htmlspecialchars and therefore also escapes and breaks the newlines.

I tried to fix this problem in Phpword/Escaper/Xml.php:

There I changed the function escapeSingleValue
FROM:

    protected function escapeSingleValue($input)
    {
        // todo: omit encoding parameter after migration onto PHP 5.4
        return htmlspecialchars($input, ENT_QUOTES, 'UTF-8');
    }

TO:

    protected function escapeSingleValue($input)
    {
        $escaped = htmlspecialchars($input, ENT_QUOTES, 'UTF-8');

        // we don't want to escape the newline code, so we replace it with html tag again
        $escaped_but_newlines_allowed = str_replace('&lt;/w:t&gt;&lt;w:br/&gt;&lt;w:t&gt;', '</w:t><w:br/><w:t>', $escaped);
        return $escaped_but_newlines_allowed;
    }

So after the input is escaped with htmlspecialchars I simply revert these changes for the newlines. Not very elegant but does the job for now.

Any news on this issue?

I'm also interested by having a cleaner solution ! Some news ?

Same issue
Any news?

+1
Also the </w:t><w:br/><w:t> solution doesn't seem to work with
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
since the tags are escaped then.

+1

help please, +1

+1

+1

Here is my solution:

$templateProcessor->setValue('foo', 'Lorem.${newline}Ipsum....');

$new_line = new \PhpOffice\PhpWord\Element\PreserveText('</w:t><w:br/><w:t>');

$templateProcessor->setComplexValue('newline', $new_line);

I hope it helps.

I added for a new paragraph and it is working well.

Here is the templatewords and printscreen of the word document:

${alternatif_block}
Alternatif-${altId}
${alternatif}
${/alternatif_block}

image

Here is the codes:

        $replacements = array(
            array('altId' => '1', 'alternatif' => 'Lorem </w:t><w:p/><w:t> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
'),            
        );      

        $templateProcessor->cloneBlock('alternatif_block', 0, true, false, $replacements);

And here is the printscreen of output:

image

I added for a new paragraph and it is working well.

Here is the templatewords and printscreen of the word document:

${alternatif_block}
Alternatif-${altId}
${alternatif}
${/alternatif_block}

image

Here is the codes:

      $replacements = array(
          array('altId' => '1', 'alternatif' => 'Lorem </w:t><w:p/><w:t> ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
'),              
      );      

      $templateProcessor->cloneBlock('alternatif_block', 0, true, false, $replacements);

And here is the printscreen of output:

image

But it works for only at first paragraph...

Was this page helpful?
0 / 5 - 0 ratings