2017-09-15 171 views
0

我是laravel中的新成員,我使用phpword來編輯word doc文件。laravel 5.4,phpword:如何在不強制下載的情況下顯示帶有隻讀權限的word文檔?

我想要一次保存它我可以在中顯示它只讀權限

我想直接顯示它而不強制下載

我試過這段代碼,但它強制下載。

這裏是我的代碼:

public function create() 
    { 
    $phpWord = new \PhpOffice\PhpWord\PhpWord(); 
    $section = $phpWord->addSection(); 
    //Ajouter l'image 
    $section->addImage(
     'C:\wamp\www\Stage_2\public\images\mobilis256.png', 
     array(
     'width'   => 100, 
     'height'  => 100, 
     'marginTop'  => -1, 
     'marginLeft' => -1, 
     'wrappingStyle' => 'behind' 
     )); 
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); 
    try { 
     $objWriter->save(storage_path('helloWorld.docx')); 
    }catch(Exception $e) 
    {} 
    $filename = 'helloWorld.docx'; 
    $path = storage_path($filename); 
    return Response::make(file_get_contents($path), 200, [ 
     'Content-Type' => 'application/docx', 
     'Content-Disposition' => 'inline; filename="'.$filename.'"' 
     ]); 
    } 

我也嘗試

return response()->file(storage_path('helloWorld.docx')); 

但總是相同的結果。

我該怎麼辦,我該如何顯示它在只讀權限?

+1

您不能在瀏覽器中顯示文檔,除非他們有某種插件來處理該文檔。 – Samsquanch

+0

那麼解決方案是什麼? –

+2

以另一種方式做。 PDF或其他東西。 – Samsquanch

回答

1

我得到了解決終於所以我所做的就是:

我保存文檔作爲HTML文件這樣的:

$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); 
    try { 
     $objWriter->save(storage_path('helloWorld.html')); 
    }catch(Exception $e) 
    {} 

然後使用DOMPDFhttps://github.com/barryvdh/laravel-dompdf轉換html文件 至pdf

return PDF::loadFile(storage_path('helloWorld.html'))->save(storage_path('helloWorldPdf.html'))->stream('download.pdf'); 

所以像這樣,我可以預覽文件而不強制的下載,而不是重做工作。

最後的代碼如下所示:

use PDF; 
    public function create() 
    { 
    $phpWord = new \PhpOffice\PhpWord\PhpWord(); 
    $section = $phpWord->addSection(); 
    //Ajouter l'image 
    $section->addImage(
    'C:\wamp\www\Stage_2\public\images\mobilis256.png', 
    array(
    'width'   => 100, 
    'height'  => 100, 
    'marginTop'  => -1, 
    'marginLeft' => -1, 
    'wrappingStyle' => 'behind' 
)); 
    // Saving the document as HTML file... 
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML'); 
    try { 
    $objWriter->save(storage_path('helloWorld.html')); 
    }catch(Exception $e) 
    {} 
    return PDF::loadFile(storage_path('helloWorld.html'))->save(storage_path('helloWorldPdf.html'))->stream('download.pdf'); 
    } 

只是,如果你想使用domppdf做這種轉換不這樣做:

更新作曲家以下行添加到後註冊提供商> bootstrap/app.php

$app->register(\Barryvdh\DomPDF\ServiceProvider::class);

要更改配置,配置文件複製到你的config文件夾和>在引導/ app.php啓用:

$app->configure('dompdf'); 這裏樣的錯誤,你會得到的解釋。 https://github.com/barryvdh/laravel-dompdf/issues/192

相關問題