【问题标题】:When sending headers to download a PDF, Safari appends .html发送标题以下载 PDF 时,Safari 会附加 .html
【发布时间】:2011-02-13 15:12:45
【问题描述】:

这是请求和响应标头

http://www.example.com/get/pdf

GET /~get/pdf HTTP/1.1
Host: www.example.com
User-Agent: Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Referer: http://www.example.com
Cookie: etc 

HTTP/1.1 200 OK
Date: Thu, 29 Apr 2010 02:20:43 GMT
Server: Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By: Me
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Pragma: no-cache
Cache-Control: private
Content-Disposition: attachment; filename="File #1.pdf"
Content-Length: 18776
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=utf-8
----------------------------------------------------------

基本上,响应头是由 DOMPDF 的stream() 方法发送的。

在 Firefox 中,文件提示为File #1.pdf。但是,在 Safari 中,文件保存为File #1.pdf.html

有谁知道为什么 Safari 将 html 扩展名附加到文件名?

我也在使用 Kohana 3,通过控制器方法提供 PDF。

【问题讨论】:

  • 会不会和 Content-Type 设置为 text/html 有关?

标签: php pdf http-headers kohana dompdf


【解决方案1】:

我已经通过添加 die(); 来修复它。流式传输后

$dompdf = new DOMPDF();
$dompdf->set_paper("a4", "portrait"); 
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($invoice.".pdf");
die();

【讨论】:

  • 这适用于我使用 Laravel 4.2 的 League\Csv 库。显然,在调用$csv->output('file.csv'); 后添加或更改了标题,这让 Safari 感到困惑。只需在调用输出后添加die;即可。
【解决方案2】:

您可以像这样更改 Kohana 3 发送标头的方式...

$this->request->headers['Content-Type'] = File::mime($file);

【讨论】:

    【解决方案3】:

    因为您告诉它它是 HTML。修正您的 MIME 类型。

    Content-Type: text/html; charset=utf-8
    

    【讨论】:

      【解决方案4】:

      从我看到的内容类型不正确,我相信如果它被修复,你的问题就会得到解决。

      【讨论】:

      • 完全错过了!我想我会把它设置为application/pdf
      猜你喜欢
      • 1970-01-01
      • 2019-04-02
      • 1970-01-01
      • 1970-01-01
      • 2014-11-16
      • 2017-03-23
      • 2014-01-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多