Open dompdf files in the browser instead of downloading

I use dompdf in a number of sites to produce PDF documents from HTML and CSS. It works brilliantly most of the time, but one slight annoyance I’ve experienced recently is that the file is always downloaded by my browser. If I’m working on and testing a site, this can result in hundreds of PDF files littering my downloads folder, with no automated way to clean them up.

Fortunately there’s a simple way to suggest to the browser that it should display the PDF file inline instead of downloading it, by passing an additional argument to the stream method:

$dompdf->stream(
  'file.pdf',
  array(
    'Attachment' => 0
  )
);

This isn’t guaranteed to work as it’s merely a hint to the browser, but it does the trick in Google Chrome.

3 Comments

  1. La Ode Muh. Farhan Fauzan

    it happen to me too, my dompdf keep download the file instead of opening it in my chrome browser.. i try the code above, and it didn’t work as well, any other sugestion ??

    and also why my downloaded file (HTML file) didn’t match the current file ? i had been edited the file and dompdf keep downloading the first form of it.

    • Paul

      Are you using the latest version of dompdf and Chrome? You might also want to look at the HTTP response headers using the Chrome developer tools, and also your Chrome preferences to see if it is setup to automatically download files regardless.

  2. Fredrick

    That really worked for me, thanks a million Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.