Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PDF to download, not link to view/download

Tags:

html

css

I am trying to make it so when you click "download here" a pdf file will download to the user's desktop. How should I go about doing this?


2 Answers

Use the Content-Disposition header.

But, please: Only do this if it is really necessary for the user to download the PDF and not open it (so that it’s just an aid for the user that he does not have to explicitly select “save file”). Don’t use it to force the user to download it although he just wants to view it.

like image 88
Marian Avatar answered Jan 25 '26 03:01

Marian


code for a.php:

<?php
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename=file.pdf')
echo file_get_contents("file.pdf");
?>

link it in web:

 <a href="a.php">Download</a>
like image 24
al bid Avatar answered Jan 25 '26 01:01

al bid