Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prompt user to download PDF file instead of opening

In my project site, if I click on a link, the PDF opens in a new or parent window. Well I want a box to appear that prompts the user to download the file instead of opening it.

Does anyone know of a simple JavaScript onClick event that will do this, in all browsers, with default settings?

My server is PHP based.

like image 491
Jitendra Vyas Avatar asked Nov 26 '09 15:11

Jitendra Vyas


People also ask

Why is Chrome asking me to save PDF instead of opening?

If your PDFs are downloading instead of opening automatically in Chrome, Chrome PDF viewer could be turned off. On your computer, open Chrome. At the top right, click More Settings. At the bottom, click Show advanced settings.

How do I force a file to download instead of open in browser?

Click on "Settings" and you'll see a new page pop up in your Chrome browser window. Scroll down to Advanced Settings, click Downloads, and clear your Auto Open options. Next time you download an item, it will be saved instead of opened automatically.

How do I make a PDF download automatically?

On the right, go to the Content section, and click on Additional content settings. Click on PDF documents. On the next page, turn on (enable) the Download PDF files instead of automatically opening them in Chrome option. You are done.


1 Answers

Since your edit states that you're using PHP, here's how to do the same in PHP:

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="downloaded.pdf"');
readfile('original.pdf');
?>
like image 195
David Hedlund Avatar answered Oct 21 '22 12:10

David Hedlund