gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:02:18 26 4
gpt4 key购买 nike

我是 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com