如何修复警告laravel 5.4中的非法字符串偏移?

时间:2017-09-13 16:41:09

标签: 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'));
}

这个错误意味着我该如何解决?

2 个答案:

答案 0 :(得分:9)

供应商/ phpoffice / phpword / SRC / PhpWord /作家/ Word2007的/部件/ 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(),
));

这对我有用

答案 1 :(得分:1)

这是WordPHP包中的一个错误。您需要使用代码的develop分支,直到修复版本在0.14

中发布
np.vectorize

参考:https://github.com/PHPOffice/PHPWord/issues/927