php - 如何修复 laravel 5.4 中的警告 Illegal string offset?

标签 php laravel

我是 laravel 的新手,我想使用 wordphp 包编辑一个 word 文档。

为了尝试它,我保留了互联网上的现有代码,但是一旦我运行它,我就得到了这个错误:

(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)

这是我的功能编辑 类 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'));
}

这个错误是什么意思,我该如何解决?

最佳答案

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(),
));

替换为这段代码

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

这对我有用

关于php - 如何修复 laravel 5.4 中的警告 Illegal string offset?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46202824/

相关文章:

PHP检查数据库表,如果值存在,运行循环生成唯一值

php - 重组数组以将元素从第三个位置连接到数组末尾并存储为第三个元素

PHP Mysqli 显示计数总和的结果

laravel - 如何在 Laravel 5 中保护图像不被公众看到?

php - Laravel SQLSTATE[42000] : Syntax error or access violation: 1055

php - Laravel pluck 和 findOrFail 没有返回正确的行

php - 寻求WordPress的代码高亮推荐

php - 在 docker php 容器中找不到 MySQLi

mysql - Laravel - 当前用户的总和

php - 事务在 laravel 中不起作用