Is there any way to force the user's download-manager to start a download for .PDF instead of showing the .PDF in a new window/tab ? I've seen this function already several times, hope somebody knows a solution
You need to send HTTP headers ( Content-disposition
) in order to do this. You cannot do this on the client side.
Set Content-Disposition in your HttpResponse header:
Content-Disposition = 'attachment; filename=filename.pdf'
<?php
// required for IE, otherwise Content-disposition is ignored
if(ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
}
$reader = fopen($filename, "r");
$contents = fread($reader, filesize($filename));
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($filename));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));
ob_end_clean();
echo $contents;
This needs to be done in the server side. You can't do this at the client side.
How to do it depends on the server side language in question.
PHP:
header('Content-Disposition: attachment; filename="' . $filename . '"');
Java:
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
.NET:
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
If there's no means of any server side code which streams the PDF file, then you need to configure it at webserver level. In for example Apache HTTPD, you can place/expand a .htaccess
file in the PDF folder's root with the following entry:
<Files *.pdf>
Header set Content-Disposition attachment
</Files>
or configure it globally in httpd.conf
file. Similar approach exist for IIS with web.config
file.
Hi,
I'm looking for the web.config (IIS) equivalent for this .htaccess:
<Files *.pdf>
Header set Content-Disposition attachment
</Files>
Somebody can help me? Thanks