laravel 5.4,phpword:如何在不强制下载的情况下显示具有只读权限的单词doc?
I'm new in laravel and I use phpword to edit a word doc file.
I want once I save it I can display it in read only permission.
I want to display it directly without forcing download.
I tried this code but it forces the download.
Here is my code :
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.'"'
]);
}
i tried also
return response()->file(storage_path('helloWorld.docx'));
but always the same result.
what should I do, and how can i disply it in read only permission?
我是laravel的新手,我使用 phpword strong>编辑word doc文件。 p>
我希望保存后我可以用只读权限 strong>显示它。 p>
我想直接显示它 没有强制下载 strong>。 p>
我尝试了这段代码,但它强制下载。 p>
这是我的代码: p>
我也试过 p>
但结果总是相同。 p> \ n
我应该怎么做,如何以只读权限显示它? strong> p>
div>
public function create()
{
$ phpWord = new \ PhpOffice \ PhpWord \ PhpWord();
$ section = $ phpWord-> addSection(); \ n // Ajouter l'image
$ section-> addImage(
'C:\ wamp \ www \ Stage_2 \ public \ images \ mobilis256.png',
array(
'width'=> 100) ,
'height'=> 100,
'marginTop'=> -1,
'marginLeft'=> -1,
'wrapingStyle'=>'后面'
)); \ n $ objWriter = \ PhpOffice \ PhpWord \ IOFactory :: createWriter($ phpWord,'Word2007');
尝试{
$ objWriter-> save(storage_path('helloWorld.docx) '));
} catch(Exception $ e)
{}
$ filename ='helloWorld.docx';
$ path = storage_path($ filename);
返回Response :: make(file_get_contents($ path),200,[
'Content-Type'=> 'application / docx',
'Content-Disposition'=> '排队; filename =“'。$ filename。'”'
]);
}
code> pre>
return response() - > file(storage_path('helloWorld.docx'));
code> pre>
I got the solution finally so what I did was:
I saved the document as html file like this :
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'HTML');
try {
$objWriter->save(storage_path('helloWorld.html'));
}catch(Exception $e)
{}
Then using dompdf : https://github.com/barryvdh/laravel-dompdf I convert the html file to pdf :
return PDF::loadFile(storage_path('helloWorld.html'))->save(storage_path('helloWorldPdf.html'))->stream('download.pdf');
So like this I can preview the file without forcing the downloading and to not redo the work.
The final code look like this:
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');
}
Just if you want to use domppdf to do this conversion do not do that :
After updating composer add the following lines to register provider in >bootstrap/app.php
$app->register(\Barryvdh\DomPDF\ServiceProvider::class);
To change the configuration, copy the config file to your config folder and >enable it in bootstrap/app.php:
$app->configure('dompdf');
Here the explication of the kind of error you will get . https://github.com/barryvdh/laravel-dompdf/issues/192