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

Version 1.2.0 - Xml parsing error because of "&" char #2553

Closed
helderneves91 opened this issue Jan 17, 2024 · 2 comments
Closed

Version 1.2.0 - Xml parsing error because of "&" char #2553

helderneves91 opened this issue Jan 17, 2024 · 2 comments

Comments

@helderneves91
Copy link

Describe the Bug

I have a word that has variables to be replaced. If the text has an &, the word is created with errors, giving the error xml parsing error.
xml parser

Steps to Reproduce

Please provide a code sample that reproduces the issue.

<?php
// Load template into PhpOffice\PhpWord
$template = Storage::disk('orbiapps_sgc')->path('templates/'.$template->file);
$template = new TemplateProcessor($template);

// Replace data
$template->setValue('myname', 'test1 & test2');

// Save a worked copy of the template
$template->saveAs(storage_path('temporary/'.$exportable_file_name));

Context

Please fill in your environment information:

  • PHP Version: 8.1
  • PHPWord Version: 1.2.0
@romcrew
Copy link

romcrew commented Jan 19, 2024

You need to enable the escape characters feature. Don't know why it is not enabled by default.
In vendor\phpoffice\phpword\src\PhpWord\Settings.php set outputEscapingEnabled to true and it should work.

@helderneves91
Copy link
Author

helderneves91 commented Jan 19, 2024

You need to enable the escape characters feature. Don't know why it is not enabled by default. In vendor\phpoffice\phpword\src\PhpWord\Settings.php set outputEscapingEnabled to true and it should work.

Ahh ok, I get it. After searching the docs I found that it's desabled by default for backward compatibility.
We should not mess with vendor folders.

Fixed by using:

// Load template into PhpOffice\PhpWord
$template = Storage::disk('orbiapps_sgc')->path('templates/'.$template->file);
\PhpOffice\PhpWord\Settings::setOutputEscapingEnabled(true);
$template = new TemplateProcessor($template);

Thanks for the guidance!

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

No branches or pull requests

3 participants