When sending headers to download a PDF, Safari appends .html
Asked Answered
A

4

7

Here is the request and response headers

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

Basically, the response headers are sent by DOMPDF's stream() method.

In Firefox, the file is prompted as File #1.pdf. However, in Safari, the file is saved as File #1.pdf.html.

Does anyone know why Safari is appending the html extension to the filename?

I'm also using Kohana 3, serving the PDF from a controller method.

Anaesthesia answered 29/4, 2010 at 2:27 Comment(1)
Could it be related to the Content-Type being set to text/html?Bruin
I
10

From what i see the content type is incorrect, i believe if that is fixed, your problem will be solved.

Incorporation answered 29/4, 2010 at 2:29 Comment(1)
Totally missed that! I guess I'll set it to application/pdf then.Anaesthesia
B
3

I've fixed it by adding die(); after streaming it

$dompdf = new DOMPDF();
$dompdf->set_paper("a4", "portrait"); 
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream($invoice.".pdf");
die();
Bibliographer answered 1/10, 2015 at 9:4 Comment(1)
This worked for me using League\Csv library with Laravel 4.2. Appearantly headers are added or changed after calling $csv->output('file.csv');, confusing Safari. Just add die; after calling output.Acanthoid
H
2

Because you're telling it that it's HTML. Fix your MIME type.

Content-Type: text/html; charset=utf-8
Huppert answered 29/4, 2010 at 2:30 Comment(0)
A
2

You can change how Kohana 3 sends headers like so...

$this->request->headers['Content-Type'] = File::mime($file);
Anaesthesia answered 29/4, 2010 at 5:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.