Skip to content

Commit bb51b5a

Browse files
Mellthasbsweeney
authored andcommitted
Fix filename with spaces for streamed output
Follow https://tools.ietf.org/html/rfc6266#appendix-D by not encoding the `filename` parameter. As a consequence, any double quotes need to be stripped, as the RFC also advices against including the backslash character. Addresses dompdf#1532.
1 parent 76af12d commit bb51b5a

File tree

3 files changed

+12
-12
lines changed

3 files changed

+12
-12
lines changed

lib/Cpdf.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3841,14 +3841,14 @@ function stream($options = '')
38413841

38423842
$attachment = $options["Attachment"] ? "attachment" : "inline";
38433843

3844-
// detect the character encoding of the incoming file
3844+
// detect the character encoding of the incoming filename
38453845
$encoding = mb_detect_encoding($filename);
38463846
$fallbackfilename = mb_convert_encoding($filename, "ISO-8859-1", $encoding);
3847-
$encodedfallbackfilename = rawurlencode($fallbackfilename);
3847+
$fallbackfilename = str_replace("\"", "", $fallbackfilename);
38483848
$encodedfilename = rawurlencode($filename);
38493849

3850-
$contentDisposition = "Content-Disposition: $attachment; filename=\"" . $encodedfallbackfilename . "\"";
3851-
if ($encodedfallbackfilename !== $encodedfilename) {
3850+
$contentDisposition = "Content-Disposition: $attachment; filename=\"$fallbackfilename\"";
3851+
if ($fallbackfilename !== $filename) {
38523852
$contentDisposition .= "; filename*=UTF-8''$encodedfilename";
38533853
}
38543854
header($contentDisposition);

src/Adapter/GD.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,14 +1026,14 @@ public function stream($filename, $options = null)
10261026
}
10271027
$attachment = (isset($options["Attachment"]) && $options["Attachment"]) ? "attachment" : "inline";
10281028

1029-
// detect the character encoding of the incoming file
1029+
// detect the character encoding of the incoming filename
10301030
$encoding = mb_detect_encoding($filename);
10311031
$fallbackfilename = mb_convert_encoding($filename, "ISO-8859-1", $encoding);
1032-
$encodedfallbackfilename = rawurlencode($fallbackfilename);
1032+
$fallbackfilename = str_replace("\"", "", $fallbackfilename);
10331033
$encodedfilename = rawurlencode($filename);
10341034

1035-
$contentDisposition = "Content-Disposition: $attachment; filename=\"" . $encodedfallbackfilename . "\"";
1036-
if ($encodedfallbackfilename !== $encodedfilename) {
1035+
$contentDisposition = "Content-Disposition: $attachment; filename=\"$fallbackfilename\"";
1036+
if ($fallbackfilename !== $filename) {
10371037
$contentDisposition .= "; filename*=UTF-8''$encodedfilename";
10381038
}
10391039
header($contentDisposition);

src/Adapter/PDFLib.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,14 +1302,14 @@ public function stream($filename, $options = null)
13021302
$filename = str_replace(array("\n", "'"), "", basename($filename, ".pdf")) . ".pdf";
13031303
$attachment = (isset($options["Attachment"]) && $options["Attachment"]) ? "attachment" : "inline";
13041304

1305-
// detect the character encoding of the incoming file
1305+
// detect the character encoding of the incoming filename
13061306
$encoding = mb_detect_encoding($filename);
13071307
$fallbackfilename = mb_convert_encoding($filename, "ISO-8859-1", $encoding);
1308-
$encodedfallbackfilename = rawurlencode($fallbackfilename);
1308+
$fallbackfilename = str_replace("\"", "", $fallbackfilename);
13091309
$encodedfilename = rawurlencode($filename);
13101310

1311-
$contentDisposition = "Content-Disposition: $attachment; filename=\"" . $encodedfallbackfilename . "\"";
1312-
if ($encodedfallbackfilename !== $encodedfilename) {
1311+
$contentDisposition = "Content-Disposition: $attachment; filename=\"$fallbackfilename\"";
1312+
if ($fallbackfilename !== $filename) {
13131313
$contentDisposition .= "; filename*=UTF-8''$encodedfilename";
13141314
}
13151315
header($contentDisposition);

0 commit comments

Comments
 (0)