MutantJohn Posted January 13, 2015 Share Posted January 13, 2015 (edited) Hey guys, So, I have some complicated code, kind of. I'm using a Java server to generate a PDF. The actual user interacts with a HTML/PHP page and they click a submit and everything is good so far. I generate a PDF to my /tmp directory (I'm using Linux) and it's a perfectly fine PDF. I can open it and read it and it's perfectly as it should be. But when I try to send it to the user's browser as a download, the PDF I get doesn't open. The error I get is "File type HTML document (text/html) is not supported" and that I'm unable to open the document. My biggest question is, why? Is it something to do with the permissions of my directories and files? I've had a lot of errors be caused because of permissions so it wouldn't surprise me. My output to the user page is : Successfully connected to Java server. Socket is closed. File to send to user : /tmp/27410c981769c34ea07c3575beebd2a2.pdf This is odd because I'm missing an echo statement. Is this because I'm not using output buffering? Here's the relevant code snippet : if (($sock = fsockopen($host, $port, $errno, $errstr, 3)) == false) echo "$errstr ($errno)"; else { echo "Successfully connected to Java server.\n<br/ >"; fwrite($sock, $data); $pdf_to_download = ""; while (!feof($sock)) $pdf_to_download = fgets($sock, 4096); fclose($sock); echo "Socket closed successfully\n<br/ >"; echo "File to send to user : ".$pdf_to_download."\n<br />"; echo "Attempting to send PDF...\n<br />"; header("Content-type: application/pdf"); header('Content-Disposition: attachment; filename = "flowers.pdf"'); readfile($pdf_to_download); } Edited January 13, 2015 by MutantJohn Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/ Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 http://php.net/manual/en/function.header.php Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file. You are echoing stuff before you send your headers. Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502837 Share on other sites More sharing options...
MutantJohn Posted January 13, 2015 Author Share Posted January 13, 2015 Right, I was starting to figure that. But even this doesn't work <?php // We'll be 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('/tmp/c51d96c8f5fe46d21fc7aa232c04d096.pdf'); ?> I can open this PDF fine (the /tmp/c51... file is good). But the browser seems to think it's a plain text file if I try to open downloaded.pdf. I'm using Chrome at the moment and I'll just be stuck loading it forever. Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502838 Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 header("Content-Type: application/octet-stream"); //try this instead of application/pdf Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502841 Share on other sites More sharing options...
MutantJohn Posted January 13, 2015 Author Share Posted January 13, 2015 No dice. One thing I noticed is that the file I'm sending is 0 bytes. My ls -l output is : -rw-r--r-- 1 my_username users 0 <---- Looks like 0 bytes are being sent... Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502842 Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 (edited) This worked for me in chrome/firefox and opened the file save dialog box. I was able to open/view the pdf when opening the downloaded file. $file = 'C:\test.pdf'; $download_filename = 'test2.pdf'; header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $download_filename . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file)); header('Accept-Ranges: bytes'); readfile($file); Edited January 13, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502845 Share on other sites More sharing options...
MutantJohn Posted January 13, 2015 Author Share Posted January 13, 2015 This worked for me in chrome/firefox and opened the file save dialog box. I was able to open/view the pdf when opening the downloaded file. $file = 'C:\test.pdf'; $download_filename = 'test2.pdf'; header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="' . $download_filename . '"'); header('Content-Transfer-Encoding: binary'); header('Content-Length: ' . filesize($file)); header('Accept-Ranges: bytes'); readfile($file); Scary, this isn't working for me either. Is there something wrong with my php.ini file? I'm trying to think of what I could be doing wrong and this is quite the head scratcher. Especially because it works for you. This must be something weird on my side. Apache is up and running and everything. Hmm... Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502846 Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 (edited) I'm sorry, I don't know what would cause an issue. It also worked fine in IE. Hopefully someone else can shine some light... Edited January 13, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502847 Share on other sites More sharing options...
CroNiX Posted January 13, 2015 Share Posted January 13, 2015 (edited) is it possible you don't have proper permissions to read the source file? You can test with is_readable($file); Edited January 13, 2015 by CroNiX Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502848 Share on other sites More sharing options...
MutantJohn Posted January 13, 2015 Author Share Posted January 13, 2015 This is my ls -l output : -rw-rw-rw- 1 http http 46991 Jan 13 10:44 /tmp/c51d96c8f5fe46d21fc7aa232c04d096.pdf User and group http can read/write and all other users can read/write as well. File size is 46,9991 bytes I have no idea why this is happening. ls -l output for /tmp folder : drwxrwxrwt 18 root root 840 Jan 13 11:47 tmp Is it because /tmp is owned by the root? I Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502849 Share on other sites More sharing options...
Solution CroNiX Posted January 13, 2015 Solution Share Posted January 13, 2015 Most likely due to root owning the dir which takes precedence. Try placing the file in your web root dir, or somewhere else where the apache user has permissions for. Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502850 Share on other sites More sharing options...
MutantJohn Posted January 13, 2015 Author Share Posted January 13, 2015 Yup. Son of a ... Okay. Okay, I'm calm. I'm cool. This is all cool. I put the pdf in the same directory as the PHP script which is all owned by http and yeah... It works now. *sigh* Thank you so much for your help. You're awesome. Quote Link to comment https://forums.phpfreaks.com/topic/293901-having-trouble-sending-a-pdf-to-users-browser/#findComment-1502851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.