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