How to view docx and pdf directly in the browser?

Asked

Viewed 343 times

2

I have a file registration system (docx, pdf) in php, currently these files are saved in a directory, and the path is saved in the database (mysql), I display these files through a href, giving to lower them.

How do I open/view these files directly in the user’s browser instead of downloading them?

Code:

<td><a style='color: Blue' href='uploads_psi/janeiro/".$cont['janeiro']."' download target='_blank'>".$cont['janeiro']."</a></td>

3 answers

3


Nothing guarantees that it will be viewed, even if you force via .htaccess all documents of the type docx using this:

<FilesMatch "\.(?i)(doc|doc|xls|xlsx)$">
Header set Content-Disposition "inline"
</FilesMatch>

Just make sure you informed him that you want inline, but if the user defaults to browser "his" to download this type, then you have no control over it, after all it has no sense to control the client’s machine, it decides what it wants to do with the HTTP download (I speak download the HTTP response, not to download the file to a folder).

Something else very important, the browser does not view stand-alone Office files, who does this are third party plugins, so some office program install plugins in your browser, formerly browsers did not prevent this, but today browsers are smarter, soon if a program like Msoffice tries to install a plugin/add-on/extension in the user’s browser it is quite capable of this being blocked.

Sometimes the browser detects things like office and can do the "bind" between browser and office, but this is not the rule and is not the obligation of the browser, some do it simply for convenience.

So to summarize, even if the user has Office installed on his PC does not mean that the browser will be able to read the document on the screen, which causes the direct download, since files not recognized by the browser are always downloaded.

So even if you remove the attribute download the tag <a></a> and try to adjust the Content-Disposition: inline with htaccess nothing will guarantee that your users will be able to view.

What is the alternative solution? You (hypothetically) ask me.

The answer is, use a format supported by various browsers and operating systems, including even cell phones tablets, and this format is the:

PDF - Portable Document Format

Note that PDF will work in most browsers and mobile phones, since seven o Content-Disposition: inline, something like:

<FilesMatch "\.(?i)(pdf)$">
Header set Content-Disposition "inline"
</FilesMatch>

But this is only because fortunately modern browsers see with built-in PDF reader, if it is a very alternative browser or is an older browser then it is likely that it does not view, in the case of Firefox, Edge and Chrome, all have built-in PDF viewer.

  • @Samuelverissimo ver este https://answall.com/a/100798/3635

  • 1

    @Samuelverissimo If Poser is difficult for you try this https://answall.com/a/176004/3635 - the Iframes part, so you don’t need to convert, but I don’t know if these services are available. Create a new php page, put it in the URL like this ver.php?documento=http://meusite.com/local/doc.docx (is an example) E in the view.php content puts: <iframe src="https://view.officeapps.live.com/op/embed.aspx?src=<?php echo urlencode($_GET['document']); ?>"></iframe>

  • 1

    If it doesn’t work try this: <iframe src="http://docs.google.com/gview?url=<?php echo urlencode($_GET['document']); ?>&amp;embedded=true"></iframe>

  • @Samuelverissimo the chat takes to get the messages in the Inbox, only comes fast if it is open. I will delete the messages above.

0

You will need to change the header and use the function readfile(). If you are doing your project with some framework, advise you to create a route, if you do not use any, you can create a file (view-file.php) to do the file treatment and display it.

Basically you’ll do the following:

$filePath = 'arquivo.pdf';
$fileName = 'arquivo.pdf';
$mimeType = 'application/pdf'; // Esse valor deve ser dinâmico, você deve pegar ele de acordo com o tipo de arquivo, PDF, DOCX e etc.

header("Content-type: {$mimeType}");
header('Content-Disposition: inline; filename="' . $fileName . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($filePath));
header('Accept-Ranges: bytes');

@readfile($fileName);
  • 2

    Thank you for the reply, that 'arquivo.pdf' where is it coming from? I replace it with what?

  • It will be a parameter that you pass via $_GET.

  • 2

    Hmmmm, I’m not sure how to apply the code... list_reportario_psi.php how are: <td><a style='color: Blue' href='exibir-arquivo.php".$cont['janeiro']."' target='_blank'>".$cont['janeiro']."</a></td> what should I do?

-1

Hello,

Try to remove from the tag the download target, just like this:

<td><a style='color: Blue' href='uploads_psi/janeiro/".$cont['janeiro']."'>".$cont['janeiro']."</a></td>

Note: Depending on the file type the browser will not be able to display it.

  • 2

    It doesn’t work, as I’ve already commented... the files are docx and even so the download happens.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.