Skip to content

Template Processor strips formatting with dollar signs present #1179

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

Closed
cbloss opened this issue Oct 31, 2017 · 3 comments
Closed

Template Processor strips formatting with dollar signs present #1179

cbloss opened this issue Oct 31, 2017 · 3 comments

Comments

@cbloss
Copy link

cbloss commented Oct 31, 2017

Hello!

I've recently updated to PHP 7.1 and upgraded the PHPoffice/phpword package to: "^0.12.1|^0.13.0", I'm suddenly having issues with the Template Processor. I have a Word doc that features dollar signs that are NOT variables. I'm using, of course, setValue to update the variables and if it's near another $, it strips it. I've been experiencing it stripping tables and new lines. I was trying to follow the suggestion in this thread: #1022 but it doesn't work.

Is this a known deal?

@FBnil
Copy link

FBnil commented Nov 1, 2017

@cbloss Yes, known deal. There are several competing patches being offered that fix the problem.
Why not try my version? All you need is the TemplateProcessor.php, which you need to replace in your vendor folder. (drop-in replacement for 0.13.0, but if you can test it with 0.12.0 that would be nice)
#1147

The solution I came up with to the ${} in function fixBrokenMacros() is to only check inside paragraphs.

// In order to limit side-effects, we limit matches to only inside (inner) paragraphs

This way, if there is damage, it is local and your document can still be opened.
Also, because we write ${}, and then go back into the braces to write the macro, we mostly have that ${ is very near, so sometimes you need to rewrite your macro to get it accepted again.

edit: just updated to the last version I use.
Note: It is a complete rewrite, so there are lots of goodies (segments! with callback functions!) and extra useful features (setBlock() ).
The thing that is lacking is image support (planned, but as a sub-class) but there is this replace-the-existing-image-and-keep-all-image-properties-like-dimension

$template->zipAddFromString("word/media/image1.jpg", file_get_contents($file));

@cbloss
Copy link
Author

cbloss commented Nov 1, 2017

Hey @FBnil !

I've been bit before with replacing vendor files so I already was extending your TemplateProcessor. I needed the zipClass to be public. I moved over the fixBroeknMacros from the new TemplateProcessor you linked me too. It works like a charm!

I also had a a similar setImageValue function that worked a lot like the zipAddFromString that was suggested by another user. It's definitely a well requested feature.

Thank you for saving the day!! 👍

@cbloss cbloss closed this as completed Nov 1, 2017
@brainwood
Copy link

This problem is cause by the fixBrokenMacros matching dollar signs to curly braces that they shouldn't be matched to. The function is in the TemplateProcessor class. A quick fix without editing any library files is to extend the TemplateProcessor class and override the fixBrokenMacros function as follows then use your extended class instead of the TemplateProcessor class as you normally would. Here is the code that I used:

protected function fixBrokenMacros($documentPart)
    {
        $fixedDocumentPart = $documentPart;

        $fixedDocumentPart = preg_replace_callback(
            '/\$(?:\{|[^{$]*\>\{)[^}$]*\}/U',
            function ($match) {
                $ret = strip_tags($match[0]);
                return $ret;
            },
            $fixedDocumentPart
        );

        return $fixedDocumentPart;
    }

This changes the regex to match only opening curly braces that occur after a greater than symbol (outside of a xml tag). Admittedly there is probably further work/improvements to the regex that could be done. This regex might be even better

I am mainly trying to ensure that the pattern doesn't match curly braces that are within attributes on xml tags (such as my issue) and that it won't grab onto the next variables curly braces either. This issue caused several pages of a document to be matched and stripped of formatting for me and caused the docx to be un-openable with the following error:

The open xml file ... cannot be opened because there are problems with the contents or the file
name might contain invalid characters
Unspecified error
Location: 2

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

3 participants