LoginSignup
16
23

More than 5 years have passed since last update.

PDFをブラウザで表示

Posted at

ソース

$path_parts = pathinfo($pdf_path);

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename="' . $path_parts['basename'] . '"');
header('Content-Length: ' . filesize($pdf_file));
readfile($pdf_path);

ポイント

Content-Dispositioninlinefilenameを設定してください。
これを設定しないとブラウザのPDFビューワーのダウンロードボタンで保存されるファイル名がデフォルトのものになってしまいます。

(ChromeとFirefoxは問題ありませんでしたが、これを設定しても、IE11はdownload.pdf、Edgeは無題.pdfになってしまいました。)
IEとEdgeの方のPDFビューワーからダウンロードする際の名前を設定するのは厳しいようです。

関連

Content-Disposition: attachment; filename="ファイル名.pdf"でダウンロードさせる場合にIEは日本語ファイル名が文字化けする現象がありましたが、以下で対応できるとのことです。

16
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
23