How to make PDF file downloadable in HTML link using PHP ?
To Download PDF from HTML link using PHP with the help of header() function in php. The header()function is used to send a raw HTTP header. Sometimes it wants the user to be prompted to save the data such as generated PDF.
Syntax:
header("Content-Type: application/octet-stream");header('Content-Disposition: attachment; filename="downloaded.pdf"');header("Content-Length: " . filesize("download.pdf"));readfile('original.pdf');.
Note: Remember that HTTP header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file or from PHP.
Example 1: Save below HTML file as htmllinkpdf.html and save PHP file as downloadpdf.php
- Below example to illustrate concept of downloading PDF file using HTML link.
- Here downloading file appears to be PDF format but without any content which shows error on opening in any application
- HTML code:
<!DOCTYPE html><html><head><title>Download PDF using PHP from HTML Link</title></head><body><center><h2style="color:green;">Welcome To GFG</h2><p><b>Click below to download PDF</b></p><ahref="gfgpdf.php?file=gfgpdf">Download PDF Now</a></center></body></html>chevron_rightfilter_none - PHP code:
<?php$file=$_GET["file"] .".pdf";// We will be outputting a PDFheader('Content-Type: application/pdf');// It will be called downloaded.pdfheader('Content-Disposition: attachment; filename="gfgpdf.pdf"');$imagpdf=file_put_contents($image,file_get_contents($file));echo$imagepdf;?>chevron_rightfilter_none - Output:

Below example illustrates the concept of downloading PDF file locally (i.e. read gfgpdf.pdf file from local ) using HTML link.
Example 2: Save HTML file as htmllinkpdf.html and save PHP file as downloadpdf.php
- HTML code:
<!DOCTYPE html><html><head><title>Download PDF using PHP from HTML Link</title></head><body><center><h2style="color:green;">Welcome To GFG</h2><p><b>Click below to download PDF</b></p><ahref="downloadpdf.php?file=gfgpdf">Download PDF Now</a></center></body></html>chevron_rightfilter_none - PHP code:
<?phpheader("Content-Type: application/octet-stream");$file=$_GET["file"] .".pdf";header("Content-Disposition: attachment; filename=". urlencode($file));header("Content-Type: application/download");header("Content-Description: File Transfer");header("Content-Length: ".filesize($file));flush();// This doesn't really matter.$fp=fopen($file,"r");while(!feof($fp)) {echofread($fp, 65536);flush();// This is essential for large downloads}fclose($fp);?>chevron_rightfilter_none - Output:


Recommended Posts:
- How to make a call-able link using HTML ?
- How to make whole row in a table clickable as link in Bootstrap ?
- HTML | <link> Tag
- HTML | <link> rel Attribute
- HTML | DOM Link rel Property
- HTML | DOM Link Object
- HTML | DOM Link type Property
- HTML | <link> href Attribute
- HTML | <body> link Attribute
- HTML | <link> sizes Attribute
- HTML | <link> target Attribute
- HTML | <link> media Attribute
- HTML | DOM Link hreflang Property
- HTML | DOM Link disabled Property
- HTML | DOM Link href Property
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



