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
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.)
$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.
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.
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.
Instead of using
@readfile($file);
Use
echo file_get_contents($file);
Or ommit the @
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With