Quantcast
Channel: DeveloperSide.NET » WAMP Developer Server
Viewing all articles
Browse latest Browse all 108

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

$
0
0

If you click on a PDF or DOC link on a website, your Browser will either:

  1. Download the PDF or DOC (with or without prompting a Save-As).
  2. Open the PDF or DOC via Adobe Reader or Microsoft Word – in the Browser’s window/tab.

Most visitors come to just read the docs, preferably and automatically in 1 step. They probably don’t really want to download those docs, and then go through the process of manually opening the downloaded file in Adobe or Office.

Whether the Browser opens or downloads the file depends on the:

To force the 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
</LocationMatch>

As an alternative, to rather force downloads (as opposed to inline viewing/opening), use:

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

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

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

Alternative – Downloading of PDF (IE 9):
download-save-pdf

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:

  • Your PDF and other document files might be generated or transferred by PHP – which will not be detectable through Apache’s Files directives.
  • Of the way Apache applies and merges Directory, Files, and Location sections – with Location sections taking effect with precedence over the Files sections (that might be causing your current issue).

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

<IfModule !mod_headers.c>
    LoadModule headers_module modules/mod_headers.so
</IfModule>

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 . '"');
readfile($fullpath);

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.


Viewing all articles
Browse latest Browse all 108

Trending Articles