2017-09-15 165 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

相关问题