2013-03-22 1 views
-1

사용자가 양식을 성공적으로 제출하면 PDF 파일을 다운로드 할 수있게하려고합니다.PHP : PDF 파일을 다운로드 가능하게하려고 시도합니다.

나는 this question의 코드를 사용했으나 pdf 파일의 내용은 다운로드 대화 상자 대신 gebrish 문자로 출력됩니다. 다운로드 코드가 기능이

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

+0

여기에 사용중인 코드가 표시됩니다. – Cfreak

+0

콘텐츠 유형 헤더를 보내거나 잘못 보내는 것을 잊어 버린 것처럼 보입니다. 예를 들어,'header ("Content-Type : application/octet-stream");'코드를 보여줄 수 있습니까? 특히 파일 상단과'header (...)'호출 사이에 모든 행 (공백)을 표시하는 것이 중요합니다. – jedwards

+0

이미 출력 되었기 때문에 귀하의 헤더를 보낼 수 없다고 생각합니다 ... 그리고 역시 PDF 후에 HTML을 볼 수 있습니까? – Wrikken

답변

0

내에서 호출되는

그냥 직감이지만, 당신의 PHP에서 HTML 코드를 모두 제거하십시오 :

<!-- [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> 

이런 내용을 추가하면 클라이언트가 HTML이 PDF 파일의 일부라고 생각하게됩니다. PDF 파일을 압축하면 비표준 데이터로 인해 결국 전체 내용이 횡설수설하게됩니다.

브라우저가 자동으로 당신이 고맙다을 원한다면, 당신은 PHP 페이지를 설정할 수 있습니다,

따라서 (파일이 자동으로 다운로드하도록 설정되어있는 경우) 파일을 다운로드하기 전에에 있던 원래의 페이지로 다시 이동합니다 "Thankyou"라고 말한 다음 브라우저를 실제 다운로드 PHP 페이지로 리디렉션합니다. 다운로드가 발생하면 감사 표시가 계속 표시됩니다.

저는 확실히 PHP 프로가 아니므로, 제가 틀렸다면 알려주십시오. 대답을 삭제하겠습니다.

파일을 다운로드하려고하기 전에 PHP 파일이 PDF 파일 인 것처럼 가장하게하려고하는 것이 좋습니다. 그렇게하면 잘못 될 수있는 일이 줄어 듭니다. PHP에서 pdf 파일을 출력하면 기존 헤더에 적절한 헤더 만 추가하면 자동으로 다운로드 할 수 있습니다.

0

입력 해 주셔서 감사합니다. 제출 후 다른 페이지로 리디렉션하고 해당 리디렉션 페이지 내에 다운로드 코드를 넣음으로써이를 해결할 수있었습니다.

관련 문제