Skip navigation.
Related Links
Force a PDF to download

Force a PDF to download

06
Aug
2007

Apache HTTP Feather LogoI recently needed to force a PDF to download using Apache. The default behaviour for most browsers is to try to open the PDF inside the browser itself. This is fine for a small PDF or for powerful machines - but a large PDF on even a modest machine can often lock the browser up. This needed fixing!

After 20 minutes of perusing the Apache documents, I happened across the FilesMatch option which takes Regular Expressions. Regular Expressions are cool things which pattern match; you give it a rather complicated (yet logical) pattern and it matches it for you. Initially I used something like this...

<Files *.pdf>
  ForceType application/pdf
  Header set Content-Disposition attachment
</Files>

This worked PERFECTLY - except some files had upper-case extensions and some had lower and I could see situations in the future where combinations of upper and lower case would be used too - just to piss me off! Because of this, not even this would work...

<FilesMatch "\.(pdf|PDF)$">
  ForceType application/pdf
  Header set Content-Disposition attachment
</FilesMatch>

That would match perfectly - as long as it was an EXACT match on upper OR lower case.

I was reaching the end of my patience - that is until I read the Using Character Classes on PerlDoc.

This showed me that I could force the RegEx (short for Regular Expressions) to match in a case-insensitive manner. This lead me to the following...

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

However this only worked in proper browser - and the bulk of the world are sadistic enough  to use Internet Explorer based ones. For some readon - if Internet Explorer sees the content type "Application/PDF" it will simply open it up in the reader. The solution? Why not pretend its a bog standard Octet Stream - just like a Zip file? After all - that's basically all it is, a binary file... A steam of bytes.

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

And there you have it... A perfectly working modification to force all PDF files to download - this will work for any file extensions you chose to put into the FilesMatch argument!

17:40
15
Sep
2007

thanks

by oli (not verified)

Hey! That worked perfectly, thanks! You might indicate that one needs mod_headers to use Header set.

16:08
16
Sep
2007

mod_headers, nice tip!

by Nick

Thanks for that... I didn't have a problem as my Apache configs tend to have everything enabled by default ;-)

15:57
9
Nov
2007

Where to put it?

by Nathan (not verified)

Do I put this code in the .htaccess file or what?

10:29
10
Nov
2007

It can go anywhere

by Nick

You can put this code in either the htaccess or the vhost configuration for your server.

You can read more about FilesMatch at the Apache Document page.

04:20
5
Dec
2007

Hey, great explanation. I'd

by Anonymous (not verified)

Hey, great explanation. Thanks for the well written explanation and for the code sample(s).

14:03
18
Mar
2008

Force PHP Download

by Squonk (not verified)

Really Interesting document, but how would you use that if apache is a proxyserver?

12:03
11
Apr
2008

Hi, I can't get this to

by Anonymous (not verified)

Hi,

I can't get this to work in .htaccess, I asked around and someone said that the regex was wrong.

Any idea how I can get it to work?

10:55
4
May
2008

For this technique applied

by Sybren (not verified)

For this technique applied to other web servers and languages such as PHP, check out http://www.stuvel.eu/pdfdownload ;-)

It'd be nice if you asked before taking stuff from my site. Contact me at webmaster [at] thingy - ma - jig . co . uk

This site was based on the Cobalt 2.0 Theme for phpBB written by Jakob Persson

Free MiniMac

Free MiniMac

Social Statistics
Search
Google



Weblinks

Add to Technorati Favorites

TGC Webring

CMS Drupal Showcase

Feedburner for ThingyMaJig

View Nicholas Thompson's profile on LinkedIn

My Twitter
  • Loading Twitter