Php view pdf in browser

I want to display a PDF file on browser which is store on our server. Here is my code :-

$path = $_SERVER['DOCUMENT_ROOT'].folder_name.'/resources/uploads/pdf/pdf_label_'.$order['id'].'.pdf';

$filename = 'pdf_label_'.$order['id'].'.pdf';

$file = $path;
$filename = $filename;

  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Accept-Ranges: bytes');
  @readfile($file);

But it returns below output on browser :

%PDF-1.4 % 5 0 obj >stream

PDF File to show :

Php view pdf in browser

I am doing this on view of codeignter.

What I am doing wrong ? Please help me on this. thanks in advance

EDIT :-

Im doing something like below :

foreach($orders as $order){

$path = $_SERVER['DOCUMENT_ROOT'].folder_name.'/resources/uploads/pdf/pdf_label_'.$order['id'].'.pdf';

$filename = 'pdf_label_'.$order['id'].'.pdf';

$file = $path;
$filename = $filename;

  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');
  header('Accept-Ranges: bytes');
  echo file_get_contents($file);

  }

So how can I show more than one file on browser ?

Edit 2 :-

So as per the answer below I have to use phpmerger to merge multiple PDF file and display into the browser. I gone through this website http://pdfmerger.codeplex.com/ but unable to use into codeignter. Can anyone please help me to use this phpmerger inside my codeingter

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 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?
  • How to view pdf file in a new tab?
  • To brush up on your knowledge on this subject, take the MCQ at the End of this Article.
  • Feedback: Your input is valuable to us. Please provide feedback on this article.

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 view pdf 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).

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.

Yes, it was beneficial.

Yes, it was helpful, however more information is required.

It wasn't helpful, so no.

Send Feedback

How do I open PDF in browser instead of download in PHP?

At the bottom, click Advanced. Under "Privacy and security," click Content settings. Near the bottom, click PDF documents. Turn off Download PDF files instead of automatically opening them in Chrome.

How do I display a PDF in my browser?

Internet Explorer.
Open Internet Explorer, and choose Tools > Manage Add-ons..
Under Add-on Types, select Toolbars and Extensions..
In the Show menu, select All Add-ons. ... .
In the list of add-ons, select Adobe PDF Reader. ... .
Click the Enable or Disable button (it toggles depending on the status of the selected add-on):.

How do I display PDF in browser instead of downloading?

At the top right, click More Settings. At the bottom, click Show advanced settings. Under “Privacy”, click Content settings. Under “PDF Documents," check the box next to "Open PDF files in the default PDF viewer application.” (Uncheck this box if you want PDFs to open automatically when you click them.)

Can we read PDF in PHP?

php“. Include it in the required web page using PHP. Create an HTML form, in which we can choose a PDF file from your computer and also check whether its file extension is PDF or not. Approach: Make sure you have a XAMPP server or WAMP server installed on your machine.