-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
addText() function and text formatting (new line "\n") #553
Comments
Hi, you maybe misunderstood how phpword works. addText is for adding a paragraph with text. The text is not seperated by line breaks. If you would like to add line breaks then use addTextRun. With a textrun you can add text and textbreaks within one paragraph. $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);
} |
Thanks for your interest on that. |
How to apply this in the template? |
I second morrido. How do you do it in a template? |
This also works (I just used it) : $text = "foo\nbar\nfoobar";
$textlines = explode("\n", $text);
for ($i = 0; $i < sizeof($textlines); $i++) {
$section->addText($textlines[$i]);
} |
I used |
Awesome @RKaczmarek, exactly what I needed. Thank you. The solution works for addListItemRuns as well as that is what I was using. |
@RKaczmarek & @BafS write in word: |
replace simple quotes by double quotes :
|
As @BafS answered, add After that, go to your This worked perfectly for me in LibreOffice and Word. |
Here's the true answer to "How to add newline while template processing" You can't just replace \n with The real solution is to always use a complex type instead of using strings. So in my function instead of returning a string I do this:
and in the template processor driver I use:
this will result in newlines and tab stops and other paragraph related things are preserved in the next lines! This allows you to create basic tables with tab stops and not worry about them breaking after the new lines. |
setComplexValue($key, $textrun) throws an error when there are multiple lines. Apparently setComplexBlock($key, $textrun) should be used instead, but that deletes all the other text present in the same line. |
What is the error thrown? I'm using it in production right now and I'm not having any problems. |
I get "Trying to access array offset on value of type bool" in line 277 of TemplateProcessor.php. So if the "Value" flavor also deletes all other text, what's the difference between the "Value" and "Block" flavors ? |
I'll take that back, the "Value" flavor does work in most cases, but there is just one situation in my application where it throws the error. I need to find out still. |
That's the same error I have, but in PhpSpreadsheet instead. |
Are you sure ? Not in my case. That would be the difference between the Block and Value flavors. |
Ah, in my case it's text on the left that is preserved. |
This happens when the ${macro} is not present in the template. Maybe a bug, because this should not generate an error. |
@jjdejong @Pocciox what versions of PHP are you using? |
I am using version 7.4 :)
Il mer 13 gen 2021, 13:22 Art Geigel <[email protected]> ha scritto:
… @jjdejong <https://github.com/jjdejong> @Pocciox
<https://github.com/Pocciox> what versions of PHP are you using?
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#553 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHXWIFVLYIQIUYW6PVF3AWDSZWGBTANCNFSM4BIEI6LA>
.
|
7.4 too |
I'm on 7.4 too. |
Hi,
I have a problem regarding formatting text. Actually, it seems, all
\n
are not interpreted when opening file with Word 2007. Here are some screenshots :This is how it appears in LibreOffice :
![lo](https://cloud.githubusercontent.com/assets/154323/8132080/6b71b434-111f-11e5-9962-4d6d9eab66e8.png)
And how it (sadly) appears in Word 2007 :
ANy help would be welcome ! I use last stable version - 0.12.0. Thanks !
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
The text was updated successfully, but these errors were encountered: