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.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
For correct work, you need to add a text block:
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 ofaddText
andaddLink
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 anaddLineBreak
method (similar way as it is in the section class) and then modify classBase.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:instead of simply doing
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:and in
Base.php
in the_writeTextRun
method at the end of this method add this condition:use PHP_EOL for line break it is the only solution for line break in PHPWord
I don't know if a PR from the accepted answer was incorporated or it was in fact already possible in 2013, but regardless, since 2015 if not earlier, the correct way to insert line breaks within a paragraph is indicated in the initial response to #553 on GitHub:
TextRun
for the paragraph;TextRun
by calling theaddTextBreak()
method of theTextRun
itself.Here's a function that will take care of this for a complete paragraph of text in a string containing literal line-breaks (CR-LF [
"\r\n"
], LF ["\n"
] or CR ["\r"
]):PHPWord (0.14.0) currently discards line breaks when reading Word2007 documents – hopefully that will be fixed before 1.0 – but the output from the above is correct when opened in Word.
You can have a text and add
\n
where ever you want to have line breaks, and do the rest like this:It's easy: just start a new textrun, and before it add textbreak to the section, like this (tested with docx format):