5

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

user1539793
  • 67
  • 1
  • 1
  • 2

4 Answers4

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:

Community
  • 1
  • 1
Kristian
  • 278
  • 2
  • 6
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
0

try this

> <FilesMatch "\.(?i:pdf)$">   ForceType application/octet-stream  
> Header set Content-Disposition attachment </FilesMatch>
Abomusab Revo
  • 78
  • 1
  • 14
  • 1
    Interesting 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