Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display PDF on 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 :

enter image description here

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

like image 483
Rakesh Shetty Avatar asked Nov 20 '14 12:11

Rakesh Shetty


People also ask

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


4 Answers

$filePath="file path here";
$filename="file name here";
header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
@ readfile($filePath);

please try this code i hope it will working as i try.

like image 138
arshad Avatar answered Oct 11 '22 13:10

arshad


Use exit after readfile:

$filepath = 'your-path/demo.pdf';
header('Content-Type: application/pdf');
header(sprintf("Content-disposition: inline;filename=%s", basename($filepath)));

@readfile($filepath);
exit;

ps. @ before readfile it's used to suppress errors, maybe remove it in dev mode.

like image 27
Pahomi Avatar answered Oct 11 '22 12:10

Pahomi


you can use PDF Merger library to achive your goals. Here is the link to original library (outdated). Prefer myokyawhtun fork which is maintained

and a sample code will be as following

include 'PDFMerger.php';
$pdf = new PDFMerger;
$pdf->addPDF('path_to_pdf/one.pdf', '1, 3, 4')  //include file1 - pages 1,3,4
    ->addPDF('path_to_pdf/two.pdf', '1-2')      //include file2 -  pages 1 to 2
    ->addPDF('path_to_pdf/three.pdf', 'all')    //include file3 - all pages
    ->merge('browser', 'test.pdf');  // OUTPUT : make sure you choose browser mode here.

Supported modes - browser, file, download and string.


Edit : As i can see you have tagged CI you can put PDFMerger.php in applications/libraries. and load it in autoload.php or in controller

and then can use it like $this->pdfmerger->addPDF() and merge() functions.

like image 9
Karan Thakkar Avatar answered Oct 11 '22 13:10

Karan Thakkar


Instead of using

@readfile($file);

Use

echo file_get_contents($file);

Or ommit the @

like image 1
RichardBernards Avatar answered Oct 11 '22 12:10

RichardBernards