QuizCure
Find Available Solution Here!

Php header PDF Open in Browser

Deepak Nov 10, 2024

Before going to learn the use of the PHP header function for a pdf file, we need to understand the header function, its properties, and how it works in short. The header function is basically used to send raw HTTP header to the 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 a required param to pass.

Example :

  • Location: http://www.anyWebPage.com
  • 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 the 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.

Example:

  • 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 the 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 the browser without downloading PHP with related concepts and example codes.

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
@readfile($file);

Php header PDF Open in Browser

Explanation:

$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).

How to view pdf file in a new tab?

We already know how to view pdf views on the browser as detailed here. But, this will render the pdf on the same page we requested.

Therefore there are cases when we need to open a pdf on another tab rather than the same page. In such cases we can use link target="_blank" .

A href target attribute describes where the linked document will open.

Let's explore with following cases with example:

Open pdf file in new tab which is stored on web server

<a href="path/filename.pdf" target="_BLANK">Click here to open the file</a>

Explanation : Where file path/filename.pdf is a location of a pdf file on a web server. Once clicked on link text it will open a new tab and render a pdf file.

Generate pdf and open on browser

In such cases we can define a href path for script where we have written pdf generation code. For example

<a href="renderPdf.php" target="_BLANK">Click here to open the file</a>

Explanation : Provided file name renderPdf.php which is on the root folder. We can place scripts anywhere on the server and provide paths.

renderPdf.php file must define php header and required code changes to make pdf view on browser. For code samples we can refer to this.

Was this post helpful?

Send Feedback

Practice Multiple-Choice Questions

  • ?
    Content-Disposition: inline used for?
    Options are:
  • ?
    Content-Type Directives are?
    Options are:
    Try your hand at more Multiple-choice Exercises

Connect With QuizCure


Follow Us and Stay tuned with upcoming blog posts and updates.

Contributed By

Deepak

Deepak

QC STAFF
51 Posts
  • PHP
  • JAVA
  • PYTHON
  • MYSQL
  • SEO

You May Like to Read

Scroll up