How to open PDF online using laravel 5.2? -
if clicked link url : website.com/open/pdf/document_one_drag_3.pdf, open pdf in browser instead of download. code in route.php
route::get('/open/pdf/{filename}', function($filename) { // check if file exists in app/storage/file folder $file_path = public_path('uploads/docs/'.$filename); if (file_exists($file_path)) { return response::make(file_get_contents($file_path), 200, [ 'content-type' => 'application/pdf', 'content-disposition' => 'inline; '.$filename, ]); } else { // error exit('requested file not exist on our server!'); } });
the pdf file still downloading , not opened in browser. wrong ?
considering laravel 5.2, download method may used generate response forces user's browser download file @ given path.
$pathtofile = public_path(). "/download/filename.pdf"; return response()->download($pathtofile);
the download method accepts file name second argument method, determine file name seen user downloading file. finally, may pass array of http headers third argument method:
return response()->download($pathtofile, $name, $headers);
so, may use
$headers = ['content-type' => 'application/pdf'];
as third parameter.
this information might useful too:
note: symfony httpfoundation, manages file downloads, requires file being downloaded have ascii file name.
to open in browser
<a href="{{url('/your/path/to/pdf/')}}" target="_blank" >my pdf </a>
Comments
Post a Comment