PHPOffice / PHPWord

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

TemplateProcessor generates an invalid Word file #2320

Open alarai opened 2 years ago

alarai commented 2 years ago

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

<?php
require __DIR__ . '/vendor/autoload.php';

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor("absence.docx");
$templateProcessor->saveAs('absence_after.docx');

Expected Behavior

The saved file should open properly in Word.

Current Behavior

The file does not open in Word. image

Context

Please fill in your environment information:

beejaz commented 1 year 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.

risingphoenix commented 1 year ago

@alarai the end of tag (curly bracketclosed} in in a equation object image

@beejaz changing the & with & does not corrupt, so you could try to format the strings with "htmlentities"

BahaaEldeenOsama commented 10 months ago

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);

Screenshot from 2024-01-18 15-59-46

Laxeer0 commented 1 week ago

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.

BahaaEldeenOsama commented 1 week ago

Hi @Laxeer0, take a screenshot of issue.

Laxeer0 commented 1 week ago

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...