code_junkie

How to make PDF file downloadable in HTML link?

Nov 12th, 2011
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. <a href="pdf_server.php?file=pdffilename">Download my eBook</a>
  2.  
  3. header("Content-Type: application/octet-stream");
  4.  
  5. $file = $_GET["file"] .".pdf";
  6. header("Content-Disposition: attachment; filename=" . urlencode($file));
  7. header("Content-Type: application/force-download");
  8. header("Content-Type: application/octet-stream");
  9. header("Content-Type: application/download");
  10. header("Content-Description: File Transfer");
  11. header("Content-Length: " . filesize($file));
  12. flush(); // this doesn't really matter.
  13. $fp = fopen($file, "r");
  14. while (!feof($fp))
  15. {
  16. echo fread($fp, 65536);
  17. flush(); // this is essential for large downloads
  18. }
  19. fclose($fp);
  20.  
  21. header("Content-Type: application/octet-stream");
  22.  
  23. def index
  24.  
  25. respond_to do |format|
  26. format.html # Your HTML view
  27. format.pdf { render :layout => false }
  28. end
  29. end
Add Comment
Please, Sign In to add comment