user1539793
user1539793

Reputation: 77

Force download (rather than display) of PDF file from link in HTML page

I have written an HTML page with a link to a PDF file, and have mounted it on a remote server. On clicking on the linked text the PDF file is opening in the browser itself. However as the PDF is large I would like to force it to be downloaded instead. Is there any way to do this?

Upvotes: 6

Views: 11811

Answers (4)

Kristian
Kristian

Reputation: 288

You don't need javascript to do this. In a modern browser you can simply do <a href="somepathto.pdf" download="filename">

Related post:

Upvotes: 9

Abomusab Revo
Abomusab Revo

Reputation: 78

try this

> <FilesMatch "\.(?i:pdf)$">   ForceType application/octet-stream  
> Header set Content-Disposition attachment </FilesMatch>

Upvotes: 0

David Hellsing
David Hellsing

Reputation: 108520

Use the download attribute:

<a download="file.pdf">Link</a>

https://html.spec.whatwg.org/multipage/semantics.html#attr-hyperlink-download

Upvotes: 3

FLX
FLX

Reputation: 2679

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)

Upvotes: 5

Related Questions