How To Display PDF File in PHP on Browser

How To Display PDF File in PHP on Browser

In this tutorial, I will explain how to display PDF file in PHP on the browser. This is a very common requirement in the case of website development or any other web application. In order to do so, please follow the below example.

Also read, Upload multiple files in PHP with example

Before getting started, just locate the PDF file inside the root directory of your project folder along with the PHP file and understand the below functions.

  • filesize():- It returns the size of the file in bytes on success. It takes the file name as a parameter to check the path of the file.
  • readfile():- This function reads the file and writes it to the output buffer. It takes the filename as a parameter to read and return the number of bytes on success. Also, you can hide the error message by putting an @ in front of the readfile() function.

Example to show PDF file in PHP in HTML web page


$filename = "D:\wamp\www\myprojects\image1.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.

if you run the above file on the browser then you will see the PDF as shown below

How To Display PDF File in PHP on Browser

Conclusion:- I hope the above example will help you to understand the overview. If there is any doubt then please leave a comment below

Leave a Comment