PHP: stream remote pdf to client browser
Asked Answered
L

2

11

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.

Locative answered 30/5, 2013 at 22:41 Comment(0)
D
21

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;
Domino answered 30/5, 2013 at 22:45 Comment(4)
this is what I used instead of of fopen("php://");Locative
It is better to use readfile rather than loading the PDF file into memory php.net/manual/en/function.readfile.phpLimnology
What's the difference between 'application/octet-stream" and "application/pdf"?Lordinwaiting
@Lordinwaiting application/octet-stream is for attachment while application/pdf is for display inline (without download)Madewell
S
-1

Even this is an old question but I found it when looking for something else, so, my response is for next visitors

To render a pdf file as stream use the Header:

header("Content-disposition: inline; filename=YOURFILE.pdf");

ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition

Syndetic answered 3/10, 2023 at 16:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.