PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

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

Closed helderneves91 closed 5 months ago

helderneves91 commented 5 months ago

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:

romcrew commented 5 months ago

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 commented 5 months ago

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!