14

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

Rakesh Shetty
  • 4,548
  • 7
  • 40
  • 79
  • This is a little side step, but you can generate those PDF's on the fly using fPDF. If you pull all the required fields from the db and generate the PDF, you can save yourself all the resources of storing them. It's very light weight and practical. – Impulss Nov 21 '14 at 01:41

8 Answers8

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

Abdul Qadir R.
  • 1,059
  • 3
  • 9
  • 26
arshad
  • 361
  • 3
  • 8
  • `filename="'.$filename.'"');` could be `filename="'.$filename);` (sorry all those quotes make my head fuzzy) – James Oct 24 '18 at 14:25
  • 4
    @James that would cause an unclosed double quoted string. Please deal with your head fuzziness in a different way -- curly braces perhaps if you don't like dots. – mickmackusa Jan 09 '19 at 22:03
  • @arshad please add some explanation to your code-only answer if you are able. – mickmackusa Jan 09 '19 at 22:04
9

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.

redochka
  • 12,345
  • 14
  • 66
  • 79
Karan Thakkar
  • 1,492
  • 2
  • 17
  • 24
  • yes im using CI so as you said I have to include pdfmerger.php inside `applications/libraries` but there is another folder fpdf and fpdi should I upload it into the same location ? and How can I load the merger in my controller ? Can you please help me how to use phpmerger in CI – Rakesh Shetty Nov 21 '14 at 05:01
  • copy `PDFMerger.php` in `application/libraries`. in `autoload.php` include `$autoload['libraries'] = array('pdfmerger',........` and then you can use it in CI as i have mentioned in answer :) @RakeshShetty – Karan Thakkar Nov 21 '14 at 05:38
  • I tried and it is loaded but now it is throwing error - require_once(fpdf/fpdi.php): failed to open stream: No such file or directory – Rakesh Shetty Nov 21 '14 at 05:52
  • didn't copied `fpdf` and `fpdi` folders ??? after copying you'l get `Assigning the return value of new by reference is deprecated` if you are using PHP 5+ you need to remove `&` from those lines as its been deprecated in PHP 5+. @RakeshShetty – Karan Thakkar Nov 21 '14 at 06:00
  • I have copied on system/libraires location – Rakesh Shetty Nov 21 '14 at 06:04
  • it should be `application/libraries` as i mentioned. `copy php file and both the folders` – Karan Thakkar Nov 21 '14 at 06:04
3

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.

Pahomi
  • 455
  • 6
  • 17
1

Instead of using

@readfile($file);

Use

echo file_get_contents($file);

Or ommit the @

RichardBernards
  • 3,146
  • 1
  • 22
  • 30
  • thanks and i tried that file_get_contents($file);exit; it shows the pdf file but actually there is more than one pdf file and im using foreach to display pdf file. So when I remove exit; from file_get_contents($file); it shows %PDF-1.4 % 5 0 obj >stream – Rakesh Shetty Nov 20 '14 at 12:40
  • You cannot do multiple downloads at once (not without invoking the code multiple times from javascript via AJAX or something like that) – RichardBernards Nov 20 '14 at 12:41
  • I think I told you how to achieve it in the previous comment – RichardBernards Nov 20 '14 at 12:48
  • can I do something like this :- include_once($path); ? – Rakesh Shetty Nov 20 '14 at 12:50
  • @RakeshShetty No, you have to invoke the script multiple times... Via AJAX from the frontend of your application: check http://stackoverflow.com/a/2339452/1857053 – RichardBernards Nov 20 '14 at 12:53
  • preferred solution create a script to zip the files - should I create a zip folder for my pdf files and then display the result as per your code ? – Rakesh Shetty Nov 20 '14 at 12:57
1

my recommended library for embedding PDFs is PDFObject. Check it out here: http://pdfobject.com/

Darius
  • 612
  • 2
  • 11
  • 23
1

This method works if you want to display a pdf in the browser page.

$file = 'filename.pdf';
echo "<embed src='$file' type='application/pdf' width='80%' height='600px' />";
Mike Volmar
  • 1,927
  • 1
  • 22
  • 31
0

Friends just use Anchor Tag that's it. Now-day's all newer browsers supports view of any files.

0

This one worked for me on Chrome.

$path = $filename;
              header("Content-Length: " . filesize ( $path ) ); 
             // header("Content-type: application/pdf"); 
              header("Content-disposition: inline; filename=".basename($path));
              header('Expires: 0');
              header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
              ob_clean();
              flush();
              //readfile($path);






          echo "<embed src='$path' type='application/pdf' width='80%' height='320px' />";

Dlaw
  • 131
  • 2
  • 14