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

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

Open
JackyZhang027 opened this issue Nov 12, 2023 · 2 comments

Comments

@JackyZhang027
Copy link

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

@sibalonat
Copy link
Contributor

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

@lordkyle0
Copy link

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

`

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

No branches or pull requests

3 participants