Skip to content
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() misses a single replacement variable from a DOCX template in about 10% of the cases?! #2408

Open
crazyserb opened this issue Jun 7, 2023 · 3 comments

Comments

@crazyserb
Copy link

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?

@abvgmbh
Copy link

abvgmbh commented Mar 26, 2024

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.

@crazyserb
Copy link
Author

Oh, so I am not crazy and it does happen to others as well.... phew.

@abvgmbh
Copy link

abvgmbh commented Mar 27, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants