Php header PDF Open in Browser

Before going to learn the use of the php header function for a pdf file, we need to understand about the header function, it’s properties and how it works in short. Header function is basically used to send raw http header to browser (client).

Header Syntax:

header(Param 1 , Param 2, Param 3)

Where :

Param 1 - This requires a param of type string. It represents the header string. It’s required param to pass.

Example :

  • Location:
  • HTTP/1.1 404 Not Found
  • Content-Type: application/pdf

and more...

Param 2 : It is an Optional param of boolean type. It indicates header replacement. Default value is true means it will replace previous.

Param 3: It is an Optional param of Integer type. It represents a response code.

Now let's understand how we can use the header function to force browsers to prompt save data sent from the server. We will require the following certain headers to accomplish the php header pdf open in browser.

Content Type : Content-Type header string required to signalize media type. It is used to tell browsers about the type of content being sent over.


  • Media type is image/png or image/jpg for image per image extension.
  • Media type is text/html to indicate an html file.
  • Media type is application/pdf to indicate a pdf file.

Therefore to tell about pdf file we need to use header like header('Content-Type: application/pdf');

Content Disposition: Content-Disposition header string used as inline to let browser know that content passed needs to be inline meaning that it should be part of a web page.

Content-Disposition header string with attachment option is used to prompt use of the "Save as" dialog box.

Therefore to display pdf file on browser we can use header as header('Content-Disposition: inline; filename="abc.pdf"');

Let’s explore the following useful ways to download or View pdf in browser without downloading php with related concepts and example codes.

1. How to view a pdf file in the browser using the php header function?

We already learn the use of header string Content-Type & Content-Disposition.Now we are going to learn How to view uploaded pdf files in PHP or PHP code to display PDF files in the browser from a file location on server.

$file = 'headerPdfFile.pdf';

$filename = 'IamPdfFile.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');  
// Read the file


$file -> Indicates pdf file path on server. We have placed pdfFile.pdf on the root folder therefore does not provide a full file path. Should be specified file location if pdf files are stored on another server location of server and make sure it is accessible.

It might raise an error Failed to load PDF document for wrong location specified.

$filename -> In case the user wants to download this pdf file. Downloaded pdf file will be named as given here.

Content-type Header -> used to tell the client (browser) about content. application/pdf for pdf file case.

Content-Disposition Header -> used for inline with user defined pdf file name.

Readfile -> Php readfile open in browser. It sends the pdf file to the browser (Client).