简体   繁体   中英

Error. Failed to load pdf document in laravel

I want to load pdf file in html but i got an error.

here is my function

 public function getDocument($file){
      $filePath = 'app/final/attachments/AA-19-4-2019-18123/'.$file;
      $type = Storage::mimeType($filePath);
      $pdfContent = Storage::get($filePath);

      return Response::make($pdfContent, 200, [
         'Content-Type'        => $type,
         'Content-Disposition' => 'inline; filename="'.$file.'"'
      ]);
 }

here is my route

 Route::get('/documents/pdf-document/{file}', 'inboxController@getDocument');

and here is my code in blade

 <embed src="{{ action('inboxController@getDocument', ['file'=> basename($attach)]) }}" style="width:100%;height:auto;overflow: hidden;" frameborder="0" allowfullscreen>

it seems like, the error is because of the filename of the file. When i changed it to asdf.pdf, it loaded the file, but when i change its filename i wont loaded anymore. Images doesnt have really a problem. only pdf files. Please help me

edit

when i tried to use this static code, then remove {file} from route and also in blade, then pdf will loaded. i cant figure it out why.

 public function getDocument(){
      $filePath = 'app/final/attachments/AA-19-4-2019-18123/my.pdf';
      $type = Storage::mimeType($filePath);
      $pdfContent = Storage::get($filePath);

      return Response::make($pdfContent, 200, [
         'Content-Type'        => $type,
         'Content-Disposition' => 'inline; filename="'.$file.'"'
      ]);
 }

You can do it this way :

php artisan storage:link

Next Go to the storage folder under 'public', and create a Folder 'FOLDER_NAME'

Your function :

 public function getDocument($filename){

    return response()->file('storage/FOLDER_NAME/'.$filename);
}

In your routes, web.php :

Route::get('/pdf/{filename}', ['as' => 'filename', 'uses' => 'ControllerName@getDocument' ]);

Then you can call it from your blade :

<a href="/pdf/{{ 'name_of_your_file' }}" target="_blank">See PDF File:</a>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM