How to open a PDF files in web browser using PHP? Last Updated : 16 Sep, 2024 Comments Improve Suggest changes Like Article Like Report Opening a PDF file in a web browser using PHP involves serving the PDF directly to the client’s browser. This is achieved by setting the appropriate HTTP headers (Content-Type and Content-Disposition) in PHP, which instructs the browser to display the PDF instead of downloading it.Note: PHP doesn't read PDFs but passes them to the browser. If stored in XAMPP's htdocs, no file path is needed.Example 1: This PHP script serves a PDF file to the browser by setting headers for content type and disposition. It uses @readfile($file) to pass the PDF for inline display or download. php <?php // Store the file name into variable $file = 'filename.pdf'; $filename = 'filename.pdf'; // Header content type header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="' . $filename . '"'); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); // Read the file @readfile($file); ?> Output:Example : This PHP script displays a PDF in the browser by specifying its server path. It sets the Content-type to application/pdf and Content-Length headers, then uses readfile() to output the file. php <?php // The location of the PDF file // on the server $filename = "/path/to/the/file.pdf"; // Header content type header("Content-type: application/pdf"); header("Content-Length: " . filesize($filename)); // Send the file to the browser. readfile($filename); ?> Output:PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. Comment More infoAdvertise with us Next Article How to open a PDF files in web browser using PHP? A ashishsaini3 Follow Improve Article Tags : Web Technologies PHP PHP Programs Similar Reads How to make PDF file downloadable in HTML link using PHP ? In web development, it is common to provide users with downloadable resources, such as PDF files. If you want to create a downloadable PDF link using HTML and PHP, this article will guide you through the process of making a PDF file downloadable when the user clicks on a link. ApproachCreate an HTML 3 min read Upload pdf file to MySQL database for multiple records using PHP We will upload multiple records to the database and display all the records from the database on the same page. In this article, we will see how we can upload PDF files to a MySQL database using PHP. Approach: Make sure you have XAMPP or WAMP installed on your machine. In this tutorial, we will be 7 min read How to read data from a file stored in XAMPP webserver using PHP ? We have given a file stored on XAMPP server and the task is to read the file from server and display the file content on the screen using PHP. We use some PHP functions to solve this problem. File: A file is set of data stored in a disk in different formats. For example - .txt, .exe, .pdf etc fopen 2 min read How to execute PHP code using command line? PHP is mainly used for creating dynamic websites, but you can also run PHP scripts without a web server. This can be done using the PHP Command Line Interface (PHP CLI), which allows you to execute PHP scripts directly from the command line. This method is commonly used for testing, automation, or r 3 min read How to Convert File Content to Byte Array in PHP ? Converting file content to a byte array in PHP is a useful technique for various applications, including file manipulation, data processing, and when working with binary files like images or PDFs. PHP offers multiple ways to read file content and convert it into a byte array, providing flexibility t 5 min read Like