there's a pdf generated on an external service and I would like to stream the pdf to the browser in my php server while streaming to client so that I don't need to download the pdf from the remote file and then start initializing download. I would just have the file immediately download or stream to the client requesting it.
Asked
Active
Viewed 2.8k times
1 Answers
20
Assuming that the generated pdf is in http://bar.com/foo.pdf, you could do:
$data = file_get_contents("http://bar.com/foo.pdf");
header("Content-type: application/octet-stream");
header("Content-disposition: attachment;filename=YOURFILE.pdf");
echo $data;

Filippos Karapetis
- 4,367
- 21
- 39
-
this is what I used instead of of `fopen("php://");` – KJW May 31 '13 at 00:06
-
2It is better to use `readfile` rather than loading the PDF file into memory http://www.php.net/manual/en/function.readfile.php – Ronni Egeriis Persson May 07 '14 at 15:51
-
7What's the difference between 'application/octet-stream" and "application/pdf"? – Alan Sep 10 '14 at 06:22
-
4@Alan application/octet-stream is for attachment while application/pdf is for display inline (without download) – redochka Mar 15 '17 at 14:42