2017-09-13 35 views
1

我是laravel中的新成員,我想使用wordphp包編輯word文檔。如何解決警告laravel 5.4中的非法字符串偏移量?

要試試我保持一個現有的代碼來自互聯網,但一旦我運行它,我得到這個錯誤:

(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')); 
} 

這個錯誤是什麼意思,我該如何解決?

回答

7

供應商/ 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(), 
)); 

這是爲我工作

相關問題