-
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
setValue Multi-Line Value #268
Comments
You're welcome, @martindavis. I'm glad that PHPWord can helps you. I haven't used template processor too much because, until now, I use PHPWord to create documents from scratch. I will start using it. In the meantime, I hope others can help answer your question. |
Hi @ivanlanin I have solved it! In
Interestingly enough, Word 2010 and Word 2003 do not have a problem if I just use I think the decision from here is whether we use Let me know which expression is better and I'll gladly fork to add the feature! |
Thanks @martindavis. @RomanSyroeshko, perhaps you have some view about this? Thanks. |
Hi, guys. The problem is the similar to the one described here. Briefly, I can say the following. Hardcoding XML tags in such way is a bad design. We need to let TemplateProcessor load documents as a bunch of objects (it deals with XML for now), but we have no volunteers who can suggest good implementation. I would like to implement this, but have no time because of job search. :( |
Hi, ON Function : protected function setValueForPart($documentPartXML, $search, $replace, $limit) AFTER : $replace = htmlspecialchars($replace); Will do the trick. |
I have used @martindavis solution and it works like a charm. A real improvement over current situation in my opinion. |
Both of these lines worked for me. $replace = preg_replace(' However, I had to add into TemplateProcessor.php at line 352 because of a later version. Thanks Andy |
Not working for me! Any suggestions? protected function setValueForPart($documentPartXML, $search, $replace, $limit)
And i'm using it: $document->setValue("hello", "hello \n world")); |
Did you get it to work? Where did you put the code? |
@maninderx No yet =( |
@kuamatzin
` |
@smandpartners-sipuni Thank you. This works.
|
I also found that this line works in setValueForPart():
I put the line at the beginning of the function in v0.13. Is there a reason that this hasn't been imported into the actual codebase? |
@mhollander In version 0.13.0, the setValue('label',"value\nwith\nmultiline") works for me (linux PHP7 0.13.0 Libreoffice). Can you verify or explain to me what does not work? |
@FBnil Thank you for looking into this and picking off some of the issues with this project. I'm using 0.13.0 with PHP Version 7.0.22-0ubuntu0.16.04.1 I just reverted my version of PHPWord to not have my addtion and this still doesn't work. I am doing a setValue(setValue('label',"value\nwith\nmultiline") and I get "value\nwith\nmultiline" in the actual substitution. If I have literal newlines in my text string, I have the same problem. |
@mhollander Ok, you are right, it is not PHPWord that add the w:br, it is LibreOffice that automatically fixes it after opening it. I'll do some tests if MSOffice doesn't mind the absence of revision id's (https://blogs.msdn.microsoft.com/brian_jones/2006/12/11/whats-up-with-all-those-rsids/).
I use arrays a lot, say:
Addendum: Do not confuse a newline inside a text, which converts to a new paragraph. You need to handle that with
with a shift-Enter, which allows, for example:
Even though visually, in most cases, they look the same. |
Had exactly the same problem Solutions provided here do the trick, see attached file for a simple extension class that applies the fix |
It seems the solution of @marios88 was not implemented yet, but the error still exists. After adding the code from the Phptemplate_withnewline.php.txt it works like a charm for me. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. |
I used the solution from @FBnil which seems to work fine. Below is the extended class I use : use PhpOffice\PhpWord\TemplateProcessor;
class VTemplateProcessor extends TemplateProcessor
{
protected function setValueForPart($search, $replace, $documentPartXML, $limit)
{
// Shift-Enter
if (is_array($replace)) {
foreach ($replace as &$item) {
$item = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $item);
}
} else {
$replace = preg_replace('~\R~u', '</w:t><w:br/><w:t>', $replace);
}
return parent::setValueForPart($search, $replace, $documentPartXML, $limit);
}
} |
Thank you for the amazing library! This has made my life much easier!
I am using your library to automatically fill out a packing slip. In order to make it easier to maintain and change, I am loading a Word Document as a Template, and then using
setValue()
to update variables. The template I am using has a lot of formatting and I would really prefer to avoid doing it in code if at all possible. It would also make it practically impossible for non-coders to update the template.The issue I'm having is that I need to insert a multi-line replacement in several places (address, order notes, etc). Are there any plans to implement this in the future? Or am I missing another way to solve this problem, such as XSLT?
I could extend the Template Class with a
setValueForPartMulti()
and if I understand Word's Format correctly, my xml would need to look like the following:Could I simply have my custom function
preg_replace('\r\n', '<w:br/>', $value)
? Would any issues arise from doing this?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: