Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHPWord how to add text break / new line while in a text run

How can I add a text break or go to the next line/row while in a text run? I tried to just do $section->addTextBreak(2); while in the text run but it just added the breaks to the section after the text run. I also tried $textrun->addTextBreak(2); but it gave me a fatal error. Any responses would be greatly appreciated.

like image 995
user2579723 Avatar asked Jul 17 '13 00:07

user2579723


4 Answers

Try this one:

    str_replace("\n", '</w:t><w:br/><w:t xml:space="preserve">', $yourData );

or this one

    str_replace("\r\n", '</w:t><w:br/><w:t xml:space="preserve">', $yourData );
like image 52
Eduardo Romero Avatar answered Nov 02 '22 07:11

Eduardo Romero


The question was asked 3 years ago but I have the same problem and I found a solution. Maybe this can help new users of PHPWord.

To add a crlf in Word document the tag can help :

$section->addText('Some text <w:br/> another text in the line ');

I found the solution here : http://jeroen.is/phpword-line-breaks/

like image 15
gaelle3182 Avatar answered Nov 02 '22 08:11

gaelle3182


I'm afraid that this will not be possible with current version. I don't have deep understanding of this library, but from looking at the code, I found out that the textRun class consist only of addText and addLink methods.

But I also need this feature along with several others, so I'm going to write it myself and create a pull request to get it included in the next release (if there will be any).

Basically it can be done by modifying the textRun class, adding an addLineBreak method (similar way as it is in the section class) and then modify class Base.php to create proper elements in final document.

In Docx xml, those line brakes are similar to the html br tag, but previous text must be closed and reopened after using break like this:

<w:r>
  <w:t>This is</w:t>
  <w:br/>
  <w:t xml:space="preserve"> a simple sentence.</w:t>
</w:r>

instead of simply doing

<w:r>
  <w:t>This is<w:br /> a simple sentence</w:t>
</w:r>

So in base.php, you'll need to edit behavior to create this block of code.

Hope this was useful!

EDIT

I have figured out that implementing this is very simple. In textRun.php just add this method:

/**
* Add a TextBreak Element
*
* @param int $count
*/
public function addTextBreak($count = 1) {
    for($i=1; $i<=$count; $i++) {
        $this->_elementCollection[] = new PHPWord_Section_TextBreak();
    }
}

and in Base.php in the _writeTextRun method at the end of this method add this condition:

elseif($element instanceof PHPWord_Section_TextBreak) {
    $objWriter->writeElement('w:br');
}
like image 7
j0hny Avatar answered Nov 02 '22 06:11

j0hny


You can have a text and add \n where ever you want to have line breaks, and do the rest like this:

$text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);

$textrun = $section->addTextRun();
$textrun->addText(array_shift($textlines));

foreach($textlines as $line) {
    $textrun->addTextBreak();
    // maybe twice if you want to seperate the text
    // $textrun->addTextBreak(2);
    $textrun->addText($line);
}
like image 5
Maryam Homayouni Avatar answered Nov 02 '22 08:11

Maryam Homayouni