I am getting server side URL to my HTML,on click of that URL PDF file is opening in browser itself,Since the PDF is larger Size I want to force the user to download it(saveas pop up) Please help me
Asked
Active
Viewed 1.2k times
5
-
1Use `Content-Disposition: attachment; ...` header. – Ram Nov 05 '14 at 16:06
4 Answers
8
You don't need javascript to do this. In a modern browser you can simply do <a href="somepathto.pdf" download="filename">
Related post:
-
1this is not working in safari. in safari pdf file open in tab can help me to resolved this? – Renish Khunt Feb 23 '15 at 05:52
5
You can use the HTML5 attribute download :
<a href="link" download="filename"/>
You can also make a PHP page that will read the PDF and force the download, by using these functions :
header('Content-Type: application/pdf');
header('Content-disposition: attachment; filename=filename.pdf');
exit(readfile(yourPDF));
(PHP is useful if you want to prevent the users to delete the download attribute, but it may be too much)

FLX
- 2,626
- 4
- 26
- 57
3
Use the download
attribute:
<a download="file.pdf">Link</a>
https://html.spec.whatwg.org/multipage/semantics.html#attr-hyperlink-download

David Hellsing
- 106,495
- 44
- 176
- 212
-
I tried above approach,but the pdf file rendering in the browser itself – user1539793 Nov 06 '14 at 03:13
0
try this
> <FilesMatch "\.(?i:pdf)$"> ForceType application/octet-stream
> Header set Content-Disposition attachment </FilesMatch>

Abomusab Revo
- 78
- 1
- 14
-
1Interesting answer ! But you should specify that it must be put in htaccess. Also, he's just talking about one PDF so I don't think that this is the best way – FLX Nov 05 '14 at 16:22