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

TemplateProcessor generates an invalid Word file #2320

Open
alarai opened this issue Oct 28, 2022 · 3 comments
Open

TemplateProcessor generates an invalid Word file #2320

alarai opened this issue Oct 28, 2022 · 3 comments

Comments

@alarai
Copy link

alarai commented Oct 28, 2022

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:

  • PHP Version: tested with 7.31 and 8.1.11
  • PHPWord Version: 0.18.3
@beejaz
Copy link

beejaz commented Mar 3, 2023

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
Copy link

@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
Copy link

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

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

No branches or pull requests

4 participants