Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix warning Illegal string offset in laravel 5.4?

Tags:

php

laravel

I'm new in laravel and I want to edit a word document using wordphp package.

To try it i kept an existing code from the internet but once I run it i got this error:

(1/1) ErrorException
Illegal string offset 'w:compatSetting'
in Settings.php (line 173)
at HandleExceptions->handleError(2, 'Illegal string offset \'w:compatSetting\'', 'C:\\wamp\\www\\Stage_2\\vendor\\phpoffice\\phpword\\src\\PhpWord\\Writer\\Word2007\\Part\\Settings.php', 173, array('compatibility' => object(Compatibility)))
in Settings.php (line 173)

here is the function edit in my class editWordController :

public function edit(){

  $phpWord = new \PhpOffice\PhpWord\PhpWord();
  $section = $phpWord->addSection();
  $section->addText(
    '"Learn from yesterday, live for today, hope for tomorrow. '
    . 'The important thing is not to stop questioning." '
    . '(Albert Einstein)'
    );

$section->addText(
  '"Great achievement is usually born of great sacrifice, '
  . 'and is never the result of selfishness." '
  . '(Napoleon Hill)',
  array('name' => 'Tahoma', 'size' => 10)
  );

$fontStyleName = 'oneUserDefinedStyle';
$phpWord->addFontStyle(
  $fontStyleName,
  array('name' => 'Tahoma', 'size' => 10, 'color' => '1B2232', 'bold' => true)
  );

$section->addText(
  '"The greatest accomplishment is not in never falling, '
  . 'but in rising again after you fall." '
  . '(Vince Lombardi)',
  $fontStyleName
  );

// Adding Text element with font customized using explicitly created font style object...
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Tahoma');
$fontStyle->setSize(13);
$myTextElement = $section->addText('"Believe you can and you\'re halfway there." (Theodor Roosevelt)');
$myTextElement->setFontStyle($fontStyle);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
try {
  $objWriter->save(storage_path('helloWorld.docx'));
}catch(Exception $e)
{}
return reponse()->download(storage_path('helloWorld.docx'));
}

what does this error mean an how can I fix it?

like image 508
Zina Taklit Avatar asked Dec 13 '22 20:12

Zina Taklit


2 Answers

vendor/phpoffice/phpword/src/PhpWord/Writer/Word2007/Part/Settings.php

$this->settings['w:compat']['w:compatSetting'] = array('@attributes' => array(
   'w:name'    => 'compatibilityMode',
   'w:uri'     => 'http://schemas.microsoft.com/office/word',
   'w:val'     => $compatibility->getOoxmlVersion(),
));

replace with this code

$this->settings['w:compat'] = array('@attributes' => array(
   'w:name'    => 'compatibilityMode',
   'w:uri'     => 'http://schemas.microsoft.com/office/word',
   'w:val'     => $compatibility->getOoxmlVersion(),
));

that's work for me

like image 139
Maulik Avatar answered Dec 26 '22 09:12

Maulik


It's a bug in the WordPHP package. You need to use the develop branch of the code until the fix is released in 0.14

composer require phpoffice/phpword:dev-develop

Reference: https://github.com/PHPOffice/PHPWord/issues/927

like image 23
roktechie Avatar answered Dec 26 '22 09:12

roktechie