PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

Word found unreadable content in "file name" after saveAs from TemplateProcessor

Open JackyZhang027 opened this issue 1 year ago • 2 comments

Describe the Bug

I have Word docx template file, i need to use this template and replace some text like ${firstname}. but after saveAs from templalte Processor, the word file become corrupted, and showing "Word found unreadable content in file_name"

Steps to Reproduce

$templatePath = public_path('files/template.docx');
$outputPath = public_path('files/test.docx');
$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($templatePath);
$templateProcessor->saveAs($outputPath);  

Expected Behavior

The word file should not be corrupted

Current Behavior

the file is corrupted and showing : Word found unreadable content in "file name"

Context

  • PHP Version: 8.2.9
  • PHPWord Version: 1.1

really apreciate if you can help on this

JackyZhang027 avatar Nov 12 '23 04:11 JackyZhang027

Can you extend the workflow? It might be that you haven't changed the values. It might be the case that first you have to change the values with $templateProcessor->setValue('firstname', 'John Doe'); and then save it. And you can save at the same file the changes. Since as i understand that is the most common use case

sibalonat avatar Dec 08 '23 12:12 sibalonat

in laravel I've same issue : `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', $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';
            $temp_filename = $template->save();

            if (file_exists($filename)) {
                unlink($filename);
            }

            rename($temp_filename, $filename);
            unlink($temp_filename);

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

`

lordkyle0 avatar Mar 26 '24 15:03 lordkyle0