At my Austin WordPress Meetup last night, one of the participants asked if there was a way to force PDFs to download from a page, rather than display in the browser. This is easily done, but will require a bit of explanation.
Today most web sites are using Apache Web Servers. Unfortunately, if your hosting company is not using Apache, this solution will not work for you.
When a user goes to a web page, the browser sends a request to the web server, which then collects up all the pieces of that page and sends them back to the browser. Early on, browsers knew how to handle anything with a .html extension, but other file types were a bit of a mystery. To deal with this, standards were written for adding helper applications to browsers that did know what to do with these foreign file types. To complete the system, the standards also included information the web server would include with a web page to let the browser know what types of files were being sent, such as images, videos, pdfs, audio files, Word or Excel files, etc., and recommendations on how to handle them.
As the site owner, we have some control of what instructions are sent to the browser. In the world of Apache Web Servers, this can be done through a file that has .htaccess as a name.
Here is the code we will add to our .htaccess file:
<IfModule mod_headers.c> <FilesMatch ".(pdf)$"> ForceType application/octet-stream Header set Content-Disposition "attachment" Allow from all </FilesMatch> </IfModule>
The server checks to see if the
mod_headers module is enabled, it just ignores anything inside the IfModule tags if it is not. If this code does not work, check with your hosting company to make sure mod_headers is activated.
If a file with the extension .pdf is encountered, the server will perform the follow directives:
ForceType application/octet-stream Header set Content-Disposition "attachment" Allow from all
This tells the browser that the .pdf file is to be handled by the octet-stream helper, and that it should be saved as an attachment. The “Allow from all” directive ensures that security settings on your site do not prevent this file from downloading.
This can be extended to virtually any file type. The FileMatch directive be extended, as in the following example, with each file type separated with the pipe “|” character:
As you can see, it is quite easy to force any type of file to download.
For the Beginners
Editing your .htaccess file is generally not something I recommend for beginners, since even a small mistake can make your site completely unaccessible.
Before doing anything, make sure you have FTP access to your root directory, or that you have tech support from your hosting company standing-by to undo these additions if things go badly.
If you don’t want to edit your .htaccess file directly, you can install WP htaccess Control. This plugin added a new option in the Settings panel. There you will find a sub-panel for “Custom htaccess”. Enter the code from above into that box:
WP htaccess Control
Once you save this, go and test your PDF pages. The files should all download instead of display.
Leave a Reply