Open alarai opened 2 years ago
I have the same problem but when using
use PhpOffice\PhpWord\TemplateProcessor;
$vars = ["name" => "Name & Breaking"];
$templateProcessor = new TemplateProcessor();
$templateProcessor->setValues($vars);
$templateProcessor->saveAs('file.docx');
The & char breaks the file when trying to opening it. If I remove &
then the file opens as usual.
@alarai the end of tag (curly bracketclosed} in in a equation object
@beejaz changing the & with & does not corrupt, so you could try to format the strings with "htmlentities"
Hi Alarai, I write this code to solve your problem with save temp files.
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
require '../vendor/autoload.php';
$templateProcessor = new TemplateProcessor('absence.docx');
$templateProcessor->setValues(
[
'name'=> 'Bahaa',
'address'=> 'Cairo',
]);
$FullFileName = 'absence.docs';
// Save the document to a temporary file
$filename = tempnam(sys_get_temp_dir(),'absence') . '.docx';
$templateProcessor->saveAs($filename);
// Set the appropriate headers for Word document download
header('Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
header('Content-Disposition: attachment;filename='.$FullFileName);
header('Cache-Control: max-age=0');
// Output the document content
readfile($filename);
// Delete the temporary file
unlink($filename);
Hi, i have the same issue when i try to replace a key with a value contains "&". Trying to opening the file with libreoffice he remove all the content after the key he try to replace.
Hi @Laxeer0, take a screenshot of issue.
hi, it's difficulty for me take screenshot of a crm, but u can reproduce the error passing an array (using setValues) and in some values of array, insert "&". I tried to use htmlentities with no result...
Describe the Bug
The following test file when saved is building a file that could not be opened back in Word.
Steps to Reproduce
Just trying to open/save the file with the Template Processor. Please use this sample file : absence.docx
Expected Behavior
The saved file should open properly in Word.
Current Behavior
The file does not open in Word.
Context
Please fill in your environment information: