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?
-
related: http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php – Ciro Santilli OurBigBook.com Nov 07 '14 at 12:02
-
Hmm - anyone know about doing the opposite - like an extension to FORCE pdfs to open in a new tab? I hate being forced to download. – Danny Staple Aug 15 '18 at 13:04
10 Answers
use the download attribute inside your <a>
tag
<a href="content/file.pdf" download > pdf link </a>
<a href="content/file.doc" download > doc link </a>

- 836
- 7
- 7
-
4Used this and tested it in Chrome, Firefox and IE8. It all works! Simple and easy. Note: if you use `download="filename"` you can even change the name of the download! – Marja Oct 29 '14 at 13:04
-
1Darn...I must correct myself! It does not work on IE. I tested it on a virtual machine, in which no pdf reader was installed yet. In that case the file is always downloaded. – Marja Oct 31 '14 at 11:32
-
This is a good solution for people who don't want to make changes to their server config. – ecnepsnai Jul 26 '15 at 01:48
-
1The [download](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes) attribute was added in HTML5. – Suncat2000 Jun 03 '19 at 11:42
Set Content-Disposition in your HttpResponse header:
Content-Disposition = 'attachment; filename=filename.pdf'

- 1,129
- 7
- 10
-
1The `; filename=
` part [is optional](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition#Syntax). – Jonas Malaco Jan 17 '17 at 22:12
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.

- 1,082,665
- 372
- 3,610
- 3,555
For IIS:
Put all files you want to force to download in their own folder.
Then in IIS go that folder and double click HTTP Response Headers.
Add a new header with the following info:
Name: content-disposition
Value: attachment
All files in that folder, when accessed, should prompt the save as dialog box for the appropriate browser.

- 904
- 9
- 10
-
1Works really well and don't have to create a page specifically for forcing files to download. – Edyn Nov 03 '13 at 03:54
You need to send HTTP headers ( Content-disposition
) in order to do this. You cannot do this on the client side.

- 161,348
- 33
- 346
- 320
Yes it can be done in JSP page... By giving a Download link in One JSP page on which goes to new script page...and download the PDF file as follows
DownloadPage.JSP code :-
<a href='downloadPdf.jsp?file=FILE.pdf' >Download PDF File</a>
downloadPdf.JSP code :-
<%@ page import="java.util.*,java.io.*"%>
<%
File f = new File ("E:/PDFfiles/Downloads/" + request.getParameter("file") );
response.setContentType ("application/pdf");
response.setHeader ("Content-Disposition", "attachment; filename=""+request.getParameter("file")+""");
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
}
catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>
Source (this is my blog): http://bloggerplugnplay.blogspot.in/2012/05/how-to-create-force-download-link-for.html

- 137,073
- 23
- 153
- 219

- 9,626
- 4
- 66
- 46
<?php
header('Content-disposition: attachment; filename=filename.pdf');
header('Content-type: application/pdf');
readfile('path/to/filename.pdf');

- 138
- 1
- 9
<?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;

- 9,232
- 18
- 64
- 115
From vb asp net code found on the internet i made this simple c# download.aspx page. You can use with the file url passed as "f" querystring parameter. (/folder/download.aspx?f=/folder1/example.pdf).
<!DOCTYPE html>
<html>
<head><title></title>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
String strRequest = "";
try
{
strRequest = Request.QueryString["f"].ToString();
}
catch
{ }
if (strRequest.Length > 0)
{
String path = Server.MapPath(strRequest);
System.IO.FileInfo File = new System.IO.FileInfo(path);
if (File.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + File.Name);
Response.AddHeader("Content-Length", File.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(File.FullName);
Response.End();
};
}
}
</script>
</head>
<body></body>
</html>

- 67
- 5