Forcing a PDF or DOC to Open in Browser Rather Than Downloading

When you click on a PDF or DOC link your Browser will either:

A) Download the PDF or DOC (with or without prompting a Save-As).

B) Open the PDF or DOC in the same window/tab via its integrated Adobe Reader or Microsoft Word viewer.

Most visitors will primarily want to read the document in the current window, preferably via a 1-click on the supplied link, without having to download it (and then opening that file in Adobe or MS Office).

If your Browser opens or downloads the file depends on 2 factors:

And also possibly some Browser and OS settings.

To force inline viewing of PDF files, edit the website’s HTTP and HTTPS VirtualHost files, and inside the VirtualHost block add in configuration:

<LocationMatch "\.(?i:pdf)$">
    ForceType application/pdf
    Header set Content-Disposition inline

As a counter-example, to rather force downloads as opposed to inline viewing/opening them, use configuration:

<LocationMatch "\.(?i:pdf)$">
    ForceType application/octet-stream
    Header set Content-Disposition attachment

Save file(s). Restart Apache for changes to take effect.

Result – Automatic (no download or prompt) in-browser viewing of PDF (IE 9):

Alternative – Downloading of PDF (IE 9):

You will need to clear your Browser’s cache (files, cookies, and history – yes, everything) and close it, every time you test this. Otherwise, you’ll keep getting the previous behavior. If the old result persists because the Browser’s PDF or Office Word plugin is caching the data itself, try renaming the file.

Normally, Apache and WAMP servers (such as WampDeveloper Pro) have a MIME-type-to-file-extension association file (Config\Apache\mime.types) that sets the correct “Content-type” Header for PDF and DOC files. But sometimes, depending on the website’s configuration and where and how the PDF files are located and/or generated, the above Header configuration is required.

It is also very important to use “LocationMatch” instead of “FilesMatch” because:

  • PDF documents can be generated by (or transferred via) PHP – and these outputs (files) will not be detectable through the use of Apache’s Files directives.
  • Apache applies and merges Directory, Files, and Location sections in a specific order with the Location sections taking precedence over the Files sections – and this can cause issues.

Note: Apache’s “Header” directive requires mod_headers, which is usually loaded by WAMP, otherwise…

<IfModule !mod_headers.c>
    LoadModule headers_module modules/

Transferring Document Files Through PHP

If you are transferring the document files through PHP, chances are the unwanted behavior is happening due to this code…

header('Content-Type: application/octet-stream');
header('Content-Length: ' . filesize($fullpath));
header('Content-Disposition: attachment; filename="' . $file . '"');

In this case, you can either search your code-base for some partials of the above strings, and comment out or update the lines with the proper Content-Type and Content-Disposition values, or use the provided Apache header configuration to post-process and replace the PHP generated headers.

6 thoughts on “Forcing a PDF or DOC to Open in Browser Rather Than Downloading”

    1. Per the last section of the above howto, did you search your PHP script files for string “Content-Type” – to make sure that the script is not setting this header and overriding the config?

  1. I have the above problem. Until recently my links opened and delivered my docs/PDFs to my browser(Firefox) and Acrobat on top of my website. Now it just downloads the docs/PDFs…I assume a weird change at my Apache driven server which needs to be corrected back but they seem quite useless, Blue Host.
    Even though I am not an IT specialist I can follow these great instructions and can use my File Manager on my host server but I cannot locate the HTTP and HTTPS VirtualHost files. Help would be appreciated with an idiot guide please…

  2. i have successfully viewing pdf file on browser but i am unable to view word document on browser using php. can any body suggest me some solution for that…
    i have used iframe for that to view it but an error occurred here is my code for that..

    i have also use google docs but the is occurred something that google docs is not connect..
    here is my code
    echo “”;
    i have also tried this code but it actually download the original file i only want to show it on browser..
    header(‘Content-disposition: inline’);
    header(‘Content-type: application/msword’);
    can anybody suggest me some solution.. i am getting stuck on it…

  3. What will be the content-type and content-dispositon for ‘docs’ Microsoft document file to open it in browser instead of download ?

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>