Force a PDF File to Open in Web Browser Instead of Downloading in PHP

Normally web browser decides a file to be downloaded or opened within a tab based on various types of headers a programming language passes. These headers define content composition in form of type, Disposition, Transfer-Encoding.

So PHP doesn’t actually read the file but sends signals for the web browser to render it. Here is how it looks when using PHP to display a PDF file on Chrome.

header('Content-type: application/pdf');  
header('Content-Disposition: inline; filename="my_file.pdf"'); 
header('Content-Transfer-Encoding: binary'); 
header('Accept-Ranges: bytes'); 


$filename = "/path/to/my_file.pdf";   
header("Content-type: application/pdf");  
header("Content-Length: " . filesize($filename)); 

Leave a Comment

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

Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.