setValue() misses a single replacement variable from a DOCX template in about 10% of the cases?!
I don't understand why and how this happens, but it seems that out of a bunch of setValue() replacements for this specific file this one for REGISTERNO fails in 10% of the cases somehow and I just end up with a blank value (even though the value for $registerno is clearly not blank, as per our debug logs).
And this doesn't happen for any other parameters and variables we have in the setValue() list, just this one, somehow.
How do we troubleshoot this or even begin to troubleshoot?
The $registerno value is just of a format AB-1234-5678, nothing special, not a funky charset of any sort or any weird characters in there, just letters and numbers.
$document = new \PhpOffice\PhpWord\TemplateProcessor('TEMPLATE.docx'); $document->setValue('REGISTERNO', $registerno); $document->setValue(....) $document->setValue(....) $document->setValue(....) $document->setValue(....) $document->saveAs('LETTER.docx');
and this is on the latest build, 1.1.0.
Then, if I rerun it on that same record with a blank value with the same parameters it works just fine on repeat attempts. As if it chokes somehow just on that one parameter, and just randomly, 1 out of 10 times or so.
Thoughts? Suggestions?
I'm having a similar issue. In a test document I'm replacing about twenty placeholders, some of them identical. They are delimited by {{ and }} for legacy reasons. Sometimes it works without issue, sometimes nothing is replaced at all, and sometimes only about two or three placeholders are replaced.
The weirdest thing is that the only changes I'm making shouldn't have any effect on this. To give an example: I replaced {{Gutachten_Nr}} with {{gnr}} in the template, and changed "Gutachten_Nr" to "gnr" in my PHP array. This fixed the issue that the value wasn't filled in for this particular placeholder but at the same time broke others again which were working previously. It feels like there's something esoteric happening in the docx file where the smallest change can have unforeseen consequences on the rest of the document.
Anyway, I've found out that you have to be careful when adding placeholders to the document, as in, you have to avoid that they are broken up into two or more text runs internally. So I'm extra careful when editing or adding them, and I can 100% guarantee that this issue is unrelated, because again: placeholders keep working and breaking in parts of the document that haven't been edited at all.
Oh, so I am not crazy and it does happen to others as well.... phew.
I found out what's happening, or at least part of it: my placeholders were all marked as typos, but just the word itself, not the curly braces surrounding them. You must not tell Word to ignore them, because it will add extra XML to the source, breaking the replacement mechanism.
I tested this by simply adding all "spelling errors" to the dictionary. When I re-ran the processing, every placeholder was filled except for a recently added one. I ran the spell check again, added the new one to the dictionary, then deleted and re-typed it. This fixed it.
I found out what's happening, or at least part of it: my placeholders were all marked as typos, but just the word itself, not the curly braces surrounding them. You must not tell Word to ignore them, because it will add extra XML to the source, breaking the replacement mechanism.
I tested this by simply adding all "spelling errors" to the dictionary. When I re-ran the processing, every placeholder was filled except for a recently added one. I ran the spell check again, added the new one to the dictionary, then deleted and re-typed it. This fixed it.
Unfortunately that didn't help on my end at all. None of my DOCX templates have any spelling errors or anything marked as a typo. There are a few grammar suggestions, but no spelling issues/typos though.
Anyone else, with any other ideas or fix suggestions for this, given that it might have something to do with spelling markup possibly?
I found something in another thread that fixed issues I had.
Add
ini_set("pcre.backtrack_limit", "23001337");
ini_set("pcre.recursion_limit", "23001337");
to the start of your PHP file; this will increase the RegExp limit. Worth a try I guess.
I found something in another thread that fixed issues I had.
Add
ini_set("pcre.backtrack_limit", "23001337");ini_set("pcre.recursion_limit", "23001337");to the start of your PHP file; this will increase the RegExp limit. Worth a try I guess.
Hmm, tried that, even changed the php.ini parameters directly, and this issue still persists...