jasonc Posted October 1, 2009 Share Posted October 1, 2009 i wish for my visitors to click a hyperlink to download a PDF file instead of the right click and 'save as' or open PDF and then click save in the PDF window. how can i do this. Link to comment https://forums.phpfreaks.com/topic/176229-how-to-click-hyperlink-to-download-a-pdf-file/ Share on other sites More sharing options...
saltedm8 Posted October 2, 2009 Share Posted October 2, 2009 well, its not html but here is one answer, my php is not too good, but this looks pretty easy <?php // outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?> http://php.net/manual/en/function.header.php#AEN123154 Link to comment https://forums.phpfreaks.com/topic/176229-how-to-click-hyperlink-to-download-a-pdf-file/#findComment-928794 Share on other sites More sharing options...
haku Posted October 2, 2009 Share Posted October 2, 2009 This is the best forced download script I have found: http://w-shadow.com/blog/2007/08/12/how-to-force-file-download-with-php/ Link to comment https://forums.phpfreaks.com/topic/176229-how-to-click-hyperlink-to-download-a-pdf-file/#findComment-928825 Share on other sites More sharing options...
jasonc Posted October 2, 2009 Author Share Posted October 2, 2009 well, its not html but here is one answer, my php is not too good, but this looks pretty easy <?php // outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?> http://php.net/manual/en/function.header.php#AEN123154 well, its not html but here is one answer, my php is not too good, but this looks pretty easy <?php // outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="downloaded.pdf"'); // The PDF source is in original.pdf readfile('original.pdf'); ?> http://php.net/manual/en/function.header.php#AEN123154 out of the other option showning in other replies i opted for this one seems easy enough. this is the begining of my file that shows the link that is to be clicked to get the download to start. <?php if (isset($_GET['a'])) { $articledate = stripstring($_GET['a']); $filename = "agendas/RRA - " . substr($articledate, 0, 2) . "-" . substr($articledate, 2, 2) . "-" . substr($articledate, 4, 4) . " - Agenda.pdf"; } elseif (isset($_GET['m'])) { $articledate = stripstring($_GET['m']); $filename = "minutes/RRA - " . substr($articledate, 0, 2) . "-" . substr($articledate, 2, 2) . "-" . substr($articledate, 4, 4) . " - Minutes.pdf"; } if ($_GET['d'] == "1" && (isset($_GET['a']) || isset($_GET['m'])) ) { // outputting a PDF header('Content-type: application/pdf'); // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="<?=$filename;?>"'); // The PDF source is in original.pdf readfile('<?=$filename;?>'); } ?> i have placed the code given in the start of my file but it seems to have the visitor download the webpage '.../index.php' itself and not the PDF Link to comment https://forums.phpfreaks.com/topic/176229-how-to-click-hyperlink-to-download-a-pdf-file/#findComment-929133 Share on other sites More sharing options...
jasonc Posted October 2, 2009 Author Share Posted October 2, 2009 ok apart from a slight eror in my code being... header(Content-Disposition: attachment; filename="<?=$filename;?>"'); // The PDF source is in original.pdf readfile('<?=$filename;?>'); should be // It will be called downloaded.pdf header('Content-Disposition: attachment; filename="'.$filename.'"'); // The PDF source is in original.pdf readfile("'".$uploaddir.$filename."'"); the following happens... in IE the page should all ok with the PDf imbeded in the page in the left hand side of my two table set. and the download link shows and when clicked i get a download box to download my web page file in view not the PDF file. in FireFox the reverse happens, the PDF does not show in the table (the PDF is open in a PDF viewer, acrobat) and the download box shows the correct file to be downloaded being the actual PDF file. i realy do not understand why this happens, anyone got any ideas ? Link to comment https://forums.phpfreaks.com/topic/176229-how-to-click-hyperlink-to-download-a-pdf-file/#findComment-929151 Share on other sites More sharing options...
jasonc Posted October 2, 2009 Author Share Posted October 2, 2009 ok i have tried and tried to get this to work and still not working the same in both FireFox and Internet Explorer. in IE the page should all ok with the PDf imbeded in the page in the left hand side of my two table set. and the download link shows and when clicked i get a download box to download my web page file in view not the PDF file. in FireFox the reverse happens, the PDF does not show in the table (the PDF is open in a PDF viewer, acrobat) and the download box shows the correct file to be downloaded being the actual PDF file. here is the page that i am working from... www dot rra dot realistic (takeout space) hostings dot com can anyone see why i have both browsers doing different things. IE is doing what i want to have happen in both if i can. it alows you to click the download link to get the download box to the correct file the PDF file. but FF seems to open the PDF in its own PDF viewer acrobat, how can i get the file to forcely open in the tables. Link to comment https://forums.phpfreaks.com/topic/176229-how-to-click-hyperlink-to-download-a-pdf-file/#findComment-929332 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.