Search code examples
phpphpword

PHPOffice Word generator problem when opening file


Whenever I try to download a file generated by PHPWord, this error pops up

enter image description here


Solution

  • You can download source from here - "https://github.com/PHPOffice/PHPWord" or also prefar this one 'https://buildmedia.readthedocs.org/media/pdf/phpword/latest/phpword.pdf'

    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpword, 'Word2007');
    $filename = 'helloWorld'.date("h:i:s").'.docx';
    $objWriter->save($filename);
    

    May be issue with your ms word. try with online view or other way.

    Try also below one

    1.Add setCompatibility() before createWriter()

    \PhpOffice\PhpWord\Settings::setCompatibility(false);
    

    2. Add exit; after the save()

    // XML Writer compatibility
    \PhpOffice\PhpWord\Settings::setCompatibility(false);
    
        // Saving the document as OOXML file...
        $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        ob_clean();
        $objWriter->save('php://output');
    
        exit;
    

    Just add ob_clean(); before output it!