Display PDF on browser

codeigniter, html, pdf, php

Solution

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.

Problem

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 : 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

Original source

Related problems