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?
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