Search This Blog

Nov 24, 2010

Forcing PDF to Download through htaccess

My last post discuss on  how to change the favicon of you blogger site

The default behavior of all browsers when dealing PDF files is opening it in a new tab / self and allow user to view the files. This action is best for small PDF and for some that has a powerful machine. But it doesn’t seem better when dealing a huge PDF files which result in droop your site or lock up the browser.
As I research in internet to find some suitable solution, I found these finest tricks to do this. – by adding the following code into your httaccess file.

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

The code will match perfectly for both lower case and upper case PDF extension by using regular expression and OR operator in the following line.


<FilesMatch "\.(pdf|PDF)">

And that it… You have now the control in Forcing PDF to download.

6 comments:

Anonymous said...

You're amazing. Thanks. I looked for how to figure this out and ... well, this works seamlessly. And I can take the taget="_blank" off now as well! Hooray!

Janz said...

Thank you for your compliments. Yes you can now kick off that target ="_blank" code.

Anonymous said...

it's showing Internal Server Error ... anyone help..?

Anonymous said...

it' showing Internal Server Error.. anyone help?

Anonymous said...

Internal server error for me, too, looking for an other solution.

Anonymous said...

I figured out the internal server error: with this code, it works, no internal server error:

ForceType application/octet-stream
Header set Content-Disposition attachment