• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar

HANDS ON WORDPRESS

Making a Living with WordPress

  • Home
  • Blog
  • About
    • About Hands On WordPress
  • The Austin WordPress Community
  • Contact
  • Show Search
Hide Search
You are here: Home / WordPress / Forcing PDF Downloads

Forcing PDF Downloads

Nick Batik · August 19, 2014 · Leave a Comment

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.

HTACCESS Directives

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>

Explanation

<IfModule mod_headers.c>

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.

<FilesMatch ".(pdf)$">

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.

Going Further

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:

<FilesMatch ".(ico|pdf|flv|jpg|jpeg|mp3|mpg|mp4|mov|wav|wmv|png|gif|swf|css|js)$">

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:

htaccess-file-download

WP htaccess Control

Once you save this, go and test your PDF pages. The files should all download instead of display.

Filed Under: WordPress Tagged With: WordPress Tutorials

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About Sandi Batik

About Sandi Batik

Introverted Freelancer, WordPress trainer, consultant, curricula developer, author, unapologetic geek, unrepentant capitalist, lucky enough to do what I love … more about me about About Sandi Batik

  • Twitter

Search

Introverted Freelancer

Traits of Successful Introverted Freelancers

Traits of Successful Introverted Freelancers

2017 Business Check-up Workshop

2017 Business Check-up Workshop

Expanding Your Business With Automated Marketing Funnels

Expanding Your Business With Automated Marketing Funnels

How to Use Permission Marketing to Build Your WordPress Business

How to Use Permission Marketing to Build Your WordPress Business

How Much Should I Charge for Building or Designing a WordPress Website?

How Much Should I Charge for Building or Designing a WordPress Website?

Project Management

Keeping Scope Creep From Killing Your Schedule and Profit Margin

Keeping Scope Creep From Killing Your Schedule and Profit Margin

Project Management for WordPress Freelancers

Project Management for WordPress Freelancers

WordPress

Securing and Maintaining Your WordPress Site

Securing and Maintaining Your WordPress Site

How The WordPress Media Library Works — 2018

How The WordPress Media Library Works — 2018

How To Build an Information Structure for Your WordPress Site

How To Build an Information Structure for Your WordPress Site

How WordPress Themes Really Work

How WordPress Themes Really Work

How to Create and Manage eMail Newsletters from Your WordPress Site

How to Create and Manage eMail Newsletters from Your WordPress Site

How to Secure and Maintain Your WordPress Site

How to Secure and Maintain Your WordPress Site

Copyright © 2010-2023 Hands On WordPress · All Rights Reserved