Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

chim3y's avatar

Preview doc, ppt and pdf

Hello, please can you suggest me any package or ways to preview ppt, doc and pdf online using laravel. Thank you soo much in advance for your time.

0 likes
9 replies
chim3y's avatar

@jlrdw thank you for your suggestion. I could upload files (pdf, doc, ppt, etc..) in laravel but now i want to open this uploaded file and view it online.

jlrdw's avatar

Another post recently discussed same subject. It was the conclusion that something like Google Docs was probably the best bet. Short of that an end user would have to have the software to open such files. No big deal, but hard to get everyone to install it. In other words not everyone will go out of their way to install the free Word viewer.

1 like
bunnypro's avatar

you can preview it by passing Content-Type headear depend on you file mime type.

e.g. for pdf you can pass header Content-Type: application/pdf

return response()->file(<your file path>, [
    'Content-Type' => 'application/pdf'
]);

you can get the file mime type using Storage facade

Storage::mimeType(<your file path>);
1 like
gustav1105's avatar

All modern browsers have pdf reading capabilities.

1 like
chim3y's avatar

@jlrdw @ershakti @bunnypro @gustav1105 thank you for your suggestion and advises.

I learnt that inorder to preview doc, ppt and pdf and other formats i can use google doc viewer for which my application should be in production however i want to implement it in local host.

I could only preview pdf, docs are automatically downloaded:

if($document_name){ $file = base_path().'/public/uploads/'.$document_name; if (file_exists($file)){

               $ext =File::extension($file);
              
                if($ext=='pdf'){
                    $content_types='application/pdf';
                   }elseif ($ext=='doc') {
                     $content_types='application/msword';  
                   }elseif ($ext=='docx') {
                     $content_types='application/vnd.openxmlformats-officedocument.wordprocessingml.document';  
                   }elseif ($ext=='xls') {
                     $content_types='application/vnd.ms-excel';  
                   }elseif ($ext=='xlsx') {
                     $content_types='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';  
                   }elseif ($ext=='txt') {
                     $content_types='application/octet-stream';  
                   }

return response(file_get_contents($file),200) ->header('Content-Type',$content_types);

            }
            else{
             exit('Requested file does not exist on our server!');
            }

       }else{
         exit('Invalid Request');
       } 
}

Unfortunately only pdf can be previewed, other formats are downloaded automatically.

1 like
Memoryme's avatar

The best possible solution for this problem could be this premium javascript package.

https://www.kukudocs.com/jsDemo

In the demo, you can upload the Docx, Xlsx, Pptx or pdf file then view the document. However, I have used this javascript to view the local document for the intranet web application.

Please or to participate in this conversation.