2010-06-22 159 views
1

我有一個php add,它調用LaTeX然後將PDF傳遞給瀏覽器。由於我的用戶將爲此服務付費,我想確保他們可以選擇保存PDF,而不是一次又一次地打開我的服務器。在客戶端缺少Content-Disposition頭文件

exec("cd tex && latex {$_SESSION['sen_id']}.tex && pdflatex {$_SESSION['sen_id']}.tex", $output); 
$pdf = substr($file,0,-3).'pdf'; 
if (file_exists($pdf)) { 
    //header('Content-Description: File Transfer'); 
    header('Content-Type: application/pdf'); 
    //header('Content-Length: ' . filesize($pdf)); 
    header('Content-Disposition: attachment;filename='.date('Ymd-His').'-'.basename($pdf)); 
    header('Content-Transfer-Encoding: binary'); 
    header('Expires: 0'); 
    header('Cache-Control: no-cache'); 
    header('Pragma: no-cache'); 
    ob_clean(); 
    flush(); 
    readfile($pdf); 
    exit; 
} else { 
    echo '<h1>No PDF Produced</h1>'; 
    echo nl2br(print_r($output,true)); 
} 

使用Wireshark,我注意到Content-Disposition頭沒有設置或沒有到達客戶端。

HTTP/1.1 200 OK\r\n 
Date: Tue, 22 Jun 2010 14:15:10 GMT\r\n 
Server: Apache/2.0.55 (Ubuntu) mod_jk/1.2.14 mod_python/3.1.4 Python/2.4.3 PHP/5.1.2 mod_ssl/2.0.55 OpenSSL/0.9.8a mod_perl/2.0.2 Perl/v5.8.7\r\n 
X-Powered-By: PHP/5.1.2\r\n 
Set-Cookie: SESS0d6c65b0599f5b70f6bbc50cfc5b2f94=2b23ba1f74f5f1f641365e9fbb45870d; expires=Thu, 15 Jul 2010 17:48:30 GMT; path=/; domain=.<domain removed>\r\n 
Content-Transfer-Encoding: binary\r\n 
Expires: 0\r\n 
Cache-Control: no-cache\r\n 
Pragma: no-cache\r\n 
Connection: close\r\n 
Transfer-Encoding: chunked\r\n 
Content-Type: application/pdf\r\n 
\r\n 

到目前爲止,我已經找到了提示說「使用八位蒸」,「不使用八位字節流」,「把空格冒號後」,「爲每個字」和「包裝文件名用引號「。我想有很多幸運的人發佈的錯誤信息。

回答

1

我不知道爲什麼標題被剝離,但this page描述瞭如何編碼內容處置標題中的文件名及其對瀏覽器互操作性的影響。

在這些情況下,我選擇了我在PHP手冊頁here(參見第一個示例)中編寫的解決方案。

your case中,您使用的標題無效,但應與所有主流瀏覽器配合使用。