Jump to content

Having trouble sending a PDF to user's browser...


MutantJohn
Go to solution Solved by CroNiX,

Recommended Posts

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 by MutantJohn
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by CroNiX
Link to comment
Share on other sites

 

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...

Link to comment
Share on other sites

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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.