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

Corrupted file after SaveAs from templateProcessor #2591

Open lordkyle0 opened 3 months ago

lordkyle0 commented 3 months ago

Describe the Bug

Get corrupted file after using TemplateProcessor.

Steps to Reproduce

I used TemplateProcessor to get my dotx file. and After getting the doc, I can't use the file generated in MS Word. When I try https://products.groupdocs.app/viewer/app, my file was opened and I've download it to pdf. Can PHPWORD make correct file for MS Word ???

try {
                Settings::setOutputEscapingEnabled(true);
                Settings::setCompatibility(false);
                // Settings::setZipClass(Settings::PCLZIP);
                $template = new TemplateProcessor(public_path('assets/templates/regidoc1.dotx'));
                $template->setMacroOpeningChars('{#');
                $template->setMacroClosingChars('#}');
                $nom1 = "FRANCIS";
                $nom2 = "ISASI";
                $nom3 = "LORD";
                $tous = 'Tous à Kinshasa';
                $to = 'DG REGIDESO';
                $date = 'KIN 26/03.2024';
                $ref = "Numéro de référence";
                $objet = "Objet de la demande";
                $body = "Ceci est le corps du document. Il peut contenir du texte enrichi, des images et des tableaux.";
                $fonction = "Votre fonction";
                $signataire = "Votre nom et votre signature";

                $template->setValue('{Nom1}', str_replace('FRANCIS', '<span style="background-color: transparent;">FRANCIS</span>', $nom1));
                $template->setValue('{Nom2}', $nom2);
                $template->setValue('{Nom3}', $nom3);
                $template->setValue('{Tous}', $tous);
                $template->setValue('{To}', $to);
                $template->setValue('{Date}', $date);
                $template->setValue('{Ref}', $ref);
                $template->setValue('{Objet}', $objet);
                $template->setValue('{Body}', $body);
                $template->setValue('{Fonction}', $fonction);
                $template->setValue('{Signataire}', $signataire);

                $filename = 'final.docx';
                $template->saveAs($filename);

                return response()->download($filename);
            } catch (\Throwable $th) {
                throw $th;
            }
<?php

Expected Behavior

Want to get coorect file from TemplateProcessor

Current Behavior

Get Corrupted file after saveAs

Context

Please fill in your environment information: