Deekshith's avatar

How to preview .doc file in laravel

i am not able to find how to preview or embed .doc or .docx file in laravel

i have controller code like below,

$subpath = 'manuscripts/'.$uuid->uuid.'/'.$manuscript->file_name;
    $path = storage_path('app/'.$subpath);

    if (file_exists($path)) {

      $pdfContent = Storage::get($subpath);
      // for pdf, it will be 'application/pdf'
      $type       = Storage::mimeType($subpath);

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

same code working fine for pdf file. but I want to preview doc file.

0 likes
10 replies
Deekshith's avatar

yes i checked they suggested to use google doc to view the local file but here I am using a private server with authentication which is showing login page code. sample code.

<iframe src="https://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>

also In PHP word i am not able to find code to preview the doc file.

laracoft's avatar

@deekshith

Try HTML

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src=http://remote.url.tld/path/to/document.doc' width='1366px' height='623px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

You have to replace http://remote.url.tld/path/to/document.doc with your own DOC URL And you must make your DOC file publicly available on the this URL.

Deekshith's avatar

Ya but i have doc file stored in storage/app/manuscript and I have endpoint to get doc file like below,

$subpath = 'manuscripts/'.$uuid->uuid.'/'.$manuscript->file_name;
    $path = storage_path('app/'.$subpath);

    if (file_exists($path)) {

      $docContent = Storage::get($subpath);
      $type       = Storage::mimeType($subpath);

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

if I access this controller endpoint it is downloading directly and I passed in iframe like below,

<?php $fullurl = url('admin/manuscript-preview/'.$uuid->uuid.'/'.$manuscript->file_submit_id.'/doc'); ?>
 <iframe src='https://view.officeapps.live.com/op/embed.aspx?src={{ $fullurl }}' width='1366px' height='623px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

But this one not displaying error like below,

File not found
The URL of the original file is not valid or the document is not publicly accessible.
Verify the URL is correct, then contact the document owner.

i tried to add a sample doc file In the public folder and used the below code,

<?php $fullurl = url('Manuscript_1618489590.doc'); ?>
                    <!-- <a href="{{ $fullurl }}"</a> -->
                      <iframe src='https://view.officeapps.live.com/op/embed.aspx?src={{ $fullurl }}' width='1366px' height='623px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

this one working fine. i am not getting how to display from the storage folder. please help me with this.

laracoft's avatar

@deekshith

  1. Open a new Incognito window in Chrome
  2. Load the URL as defined by url('admin/manuscript-preview/'.$uuid->uuid.'/'.$manuscript->file_submit_id.'/doc');
  3. Does it prompt for password?

If yes, you need to remove the password If not, can you share the URL?

Deekshith's avatar
http://journal.dsf.uhm.mybluehost.me/admin/manuscript-preview/Sbo50akS/b8Qb8Abd/doc

i have made this URL public

you can check this. as it is downloading directly. my controller code is,

$docContent = Storage::get($subpath);
      // for pdf, it will be 'application/pdf'
    $type       = Storage::mimeType($subpath);

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

iframe code is,

<?php $fullurl = url('admin/manuscript-preview/'.$uuid->uuid.'/'.$manuscript->file_submit_id.'/doc'); ?>

<iframe src='https://view.officeapps.live.com/op/embed.aspx?src={{ $fullurl }}' width='1366px' height='623px' frameborder='0'>This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

Deekshith's avatar

thank you i tried to hard refresh the page and it is working fine when I removed auth checker middleware. but now I have enabled it again. it is still working fine. this problem is because of auth middleware?

i am trying this in admin dashboard with auth middleware.

i think this is the security issue because I tried to run this URL directly,

http://journal.dsf.uhm.mybluehost.me/admin/manuscript-preview/Sbo50akS/b8Qb8Abd/doc

it is redirecting to login page which is correct.

but when I tried to run below URL,

https://view.officeapps.live.com/op/embed.aspx?src=http://journal.dsf.uhm.mybluehost.me/admin/manuscript-preview/Sbo50akS/b8Qb8Abd/doc

it is working fine. if the user adds other user files by tweaking the URL It is still working.

Deekshith's avatar

this is the generated iframe code,

<iframe src="https://view.officeapps.live.com/op/embed.aspx?src=http://journal.dsf.uhm.mybluehost.me/admin/manuscriptpreview/Sbo50akS/b8Qb8Abd/doc" width="1366px" height="623px" frameborder="0">This is an embedded <a target='_blank' href='http://office.com'>Microsoft Office</a> document, powered by <a target='_blank' href='http://office.com/webapps'>Office Online</a>.</iframe>

it is not working because I have auth middleware where to access below URL user should log in as admin.

http://journal.dsf.uhm.mybluehost.me/admin/manuscriptpreview/Sbo50akS/b8Qb8Abd/doc

any suggestion?

laracoft's avatar

@deekshith

Previewing a DOC file in browser requires the help of https://view.officeapps.live.com/op/embed.aspx, and they have to pull your file from a public unprotected link.

Your only option here is to provide the DOC file on a public unprotected link.

Other alternatives is to

  1. Code your own DOC file previewer
  2. OR convert your DOC file to PDF and preview the PDF.

Please or to participate in this conversation.