Display PDF on browser
Solution 1
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.
Solution 2
$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.
Solution 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.
Solution 4
Instead of using
@readfile($file);
Use
echo file_get_contents($file);
Or ommit the @
Solution 5
my recommended library for embedding PDFs is PDFObject. Check it out here: http://pdfobject.com/
Rakesh Shetty
Software engineer with a experience of 5 years in web development.Having an extreme passion to work on Front End technologies as well as on the Server Side. Specialist on : PHP,jQuery, Javascript, Ajax.Codeigniter and other frameworks. HTML, HTML5,CSS MYSQL, PostgreSQL, Oracle and other open source database. Social Media Application (Facebook,Twitter,Google Plus and others). API for Android, IOS.
Updated on December 03, 2020Comments
-
Rakesh Shetty over 3 years
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
-
Rakesh Shetty over 9 yearsthanks 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
-
RichardBernards over 9 yearsYou cannot do multiple downloads at once (not without invoking the code multiple times from javascript via AJAX or something like that)
-
RichardBernards over 9 yearsI think I told you how to achieve it in the previous comment
-
Rakesh Shetty over 9 yearscan I do something like this :- include_once($path); ?
-
RichardBernards over 9 years@RakeshShetty No, you have to invoke the script multiple times... Via AJAX from the frontend of your application: check stackoverflow.com/a/2339452/1857053
-
Rakesh Shetty over 9 yearspreferred 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 over 9 yearsyes 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 -
Karan Thakkar over 9 yearscopy
PDFMerger.php
inapplication/libraries
. inautoload.php
include$autoload['libraries'] = array('pdfmerger',........
and then you can use it in CI as i have mentioned in answer :) @RakeshShetty -
Rakesh Shetty over 9 yearsI 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
-
Karan Thakkar over 9 yearsdidn't copied
fpdf
andfpdi
folders ??? after copying you'l getAssigning 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 -
Rakesh Shetty over 9 yearsI have copied on system/libraires location
-
Karan Thakkar over 9 yearsit should be
application/libraries
as i mentioned.copy php file and both the folders
-
James over 5 years
filename="'.$filename.'"');
could befilename="'.$filename);
(sorry all those quotes make my head fuzzy) -
mickmackusa over 5 years@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 over 5 years@arshad please add some explanation to your code-only answer if you are able.