PDF, Content-Disposition, inline and filename
January 22, 2010
Here’s an odd quirk I came across when trying to save PDFs displayed inline in the browser. It seems that you cannot specify the filename using the Content-Disposition header inline with the Adobe PDF broswer plugin. It completely ignores it and uses what’s after the last slash in the URL. So if your url is like this http://www.mysite.com/makepdffile.php the filename of the PDF would be makepdffile.php when you clicked on save. Fortunately I have found a solution.
Our solution is to use mod-rewrite to make a friendlier url with the filename after the last slash.
So lets make a example mod rewrite.
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^id/([^/]+)/piptype/([^/]+)/EXT/([^/]+) pip_maker_single.php?id=$1&piptype=$2&EXT=$3 [NC]</IfModule>
I suggest you google to learn more about mod-rewrite. It’s a great tool and if you are an admin you will need to learn how to use this.
In any case in my example. The RewriteRule line does all the magic. I’m converting pip_maker_single.php?id=$1&piptype=$2&EXT=$3 to a pretty url. to the url. /id/1/pitptype/full/EXT/mypdffilename
Now when a user clicks save on the in line displayed pdf the filename I want the pdf saved as is mypdffilename instead of pip_maker_single.php.
Now you can probably bypass all of this hassle if you use the header Content-Disposition attachment. That will properly work and use the filename you specified. But if you want to display the pdf in the browser then this is the way to go.
Got something to say?
You must be logged in to post a comment.


