How to fix - "Illegal string offset 'w:compatSetting'"

July 21, 2017 | Category : Laravel

Yesterday, i was working on my php laravel 5 application and i installed PHPOffice/PHPWord package and trying to use it in controller. But when i run my project and see i got following error :

"Illegal string offset 'w:compatSetting'"

Even i generate docx file, i thought why it's come event file is created successfully. I try to understand deeply but i didn't get well. However i found temporary solution using try catch. So you can solve by following way:

Fixed:

$phpWord = new \PhpOffice\PhpWord\PhpWord();

$section = $phpWord->addSection();

$section->addText("test");

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');

try {

$objWriter->save(storage_path('helloWorld.docx'));

} catch (Exception $e) {

}

return response()->download(storage_path('helloWorld.docx'));

I hope you found your best solution