{"id":1567,"date":"2011-04-23T00:41:24","date_gmt":"2011-04-23T05:41:24","guid":{"rendered":"http:\/\/ahmeddirie.com\/?p=1567"},"modified":"2012-11-26T17:07:14","modified_gmt":"2012-11-26T22:07:14","slug":"forcing-pdf-files-to-download","status":"publish","type":"post","link":"https:\/\/ahmeddirie.com\/blog\/web-development\/forcing-pdf-files-to-download\/","title":{"rendered":"Forcing PDF files to download"},"content":{"rendered":"<p>Recently, I was dealing with a rather dumb issue between various browsers and how they display\/download PDF files. What was dumb was that one browser would display an error when the file originates from a CDN, while when locally available on the same server, no issues. Let&#8217;s not forget, visa versa for the other browser.<br \/>\n<!--more--><br \/>\nFirst thing I managed to do was reach out to the browser and CDN in question on twitter, hoping this was a known issue. With my luck, no go. So, let&#8217;s just force a file download and avoid the headache all in all. We&#8217;ll call this getfile.php.<\/p>\n<p>You first need to create a page that will be referenced, which will initiate the file download. In this example, we pass in the name of the file without the extension.<\/p>\n<p>http:\/\/domain.com\/downloads\/getfile.php?file=(name_of_file)<\/p>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;?php\r\n    header(&quot;Content-Type: application\/octet-stream&quot;);\r\n\r\n    $file = &quot;http:\/\/domain.com\/downloads\/&quot;.$_GET[&quot;file&quot;].&quot;.pdf&quot;;\r\n    $name = $_GET[&quot;file&quot;].&quot;.pdf&quot;;\r\n    header(&quot;Content-Disposition: attachment; filename=&quot;.urlencode($name));\r\n    header(&quot;Content-Type: application\/force-download&quot;);\r\n    header(&quot;Content-Type: application\/octet-stream&quot;);\r\n    header(&quot;Content-Type: application\/download&quot;);\r\n    header(&quot;Content-Description: File Transfer&quot;);            \r\n    header(&quot;Content-Length: &quot;.filesize($file));\r\n\r\n    flush();\r\n\r\n    $fp = fopen($file, &quot;r&quot;);\r\n    while (!feof($fp)) {\r\n        echo fread($fp, 65536);\r\n        flush();\r\n    }\r\n\r\n    fclose($fp);\r\n?&gt;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Recently, I was dealing with a rather dumb issue between various browsers and how they display\/download PDF files. What was dumb was that one browser would display an error when the file originates from a CDN, while when locally available on the same server, no issues. Let&#8217;s not forget, visa versa for the other browser.<\/p>\n","protected":false},"author":2,"featured_media":2937,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[326,22,152],"_links":{"self":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/1567"}],"collection":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/comments?post=1567"}],"version-history":[{"count":8,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/1567\/revisions"}],"predecessor-version":[{"id":3066,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/1567\/revisions\/3066"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media\/2937"}],"wp:attachment":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media?parent=1567"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/categories?post=1567"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/tags?post=1567"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}