-1

I'm trying to make a PDF file downloadable after user submits a form successfully.

I have used the code from this question, but the content of the pdf file gets outputted as gebrish characters instead of the download dialog to popup.

The download code is called from within a function

function phpfmg_thankyou(){
    phpfmg_redirect_js();

    //include('get_file.php');
    $pdf_file = "{$_SERVER['DOCUMENT_ROOT']}/secured_assets/CRE_White_Paper_Release_01-15-2013.pdf";
    if( file_exists( $pdf_file ) ){
        header("Content-Type: application/octet-stream");
        header("Content-Disposition: attachment; filename=" . Urlencode('CRE_White_Paper_Release_01-15-2013.pdf'));   
        header("Content-Type: application/force-download");
        header("Content-Type: application/download");
        header("Content-Description: File Transfer");            
        header("Content-Length: " . Filesize($pdf_file));
        flush(); // this doesn't really matter.
        $fp = fopen($pdf_file, "r");
        while (!feof($fp)){
            echo fread($fp, 65536);
            flush(); // this is essential for large downloads
        } 
        fclose($fp);

    }

?>

<!-- [Your confirmation message goes here] -->
    <br>

    <div style="padding: 1em; background: #CDD7B6;">    
        <b>Your inquiry has been received. Thank you!</b>
        <p><a title="FREE White Paper Commercial Real Estate Expectations" href="secured_assets/CRE_White_Paper_Release_01-15-2013.pdf">Click Here</a> to get your FREE copy of White Paper Commercial Real Estate Expectations</p>
    </div>

<?php



} // end of function phpfmg_thankyou()

enter image description here

Community
  • 1
  • 1
farjam
  • 2,089
  • 8
  • 40
  • 77
  • show the code you're using here. – Cfreak Mar 22 '13 at 19:12
  • Looks like you're forgetting to send / incorrectly sending the content-type header. For example, `header("Content-Type: application/octet-stream");` Can you show us your code? It's particularly important that you show us every line (even blank) between the top of the file and the `header(...)` call. – jedwards Mar 22 '13 at 19:13
  • I assume your headers can't be sent because there is already output... And do I see HTML after the pdf too? – Wrikken Mar 22 '13 at 19:27
  • Where is this function called? It's certainly not here that the file gets served. Also since the html suggest that it's linked to the correct filename that the user should click, why even serve the file through PHP at all? – baloo Mar 22 '13 at 19:28
  • Use *application/octet-stream* and *application/octet-stream* only. – Gumbo Mar 22 '13 at 19:33

2 Answers2

0

This is just a hunch, but try removing all of the HTML code in your PHP:

<!-- [Your confirmation message goes here] -->
    <br>

    <div style="padding: 1em; background: #CDD7B6;">    
        <b>Your inquiry has been received. Thank you!</b>
        <p><a title="FREE White Paper Commercial Real Estate Expectations" href="secured_assets/CRE_White_Paper_Release_01-15-2013.pdf">Click Here</a> to get your FREE copy of White Paper Commercial Real Estate Expectations</p>
    </div>

Adding content like this will (I think) make your client think that the HTML is part of the PDF file. PDF files being compressed, any non-standard data will end up turning the entire thing to gibberish as you describe.

Browsers will automatically take you back to the original page you were on before downloading a file (if the file is set to automatically download)

Therefore, if you want a thankyou, you can set up a PHP page which says "Thankyou" and then redirects your browser to the actual download PHP page. When the download happens, your Thankyou will still be displayed.

I am definitely not a PHP pro, so If I'm wrong about this, let me know, and I'll delete the answer.

Before trying to get the file to download, I would suggest just trying to get the php file to pretend it is a pdf file. That way there are fewer things that could be going wrong. Once you have the php outputting a pdf file, you need only add the proper headers to your existing code to make it automatically download.

Georges Oates Larsen
  • 6,812
  • 12
  • 51
  • 67
0

Thank you all for your inputs. I was able to solve this by redirecting to another page after the submission, and having the download code inside that redirect page.

farjam
  • 2,089
  • 8
  • 40
  • 77