How to Display PDF in PHP

Display PDF on browser

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.

Display pdf blob file from database

If your data still in Blob, you need to encode your data using base64_encode().
Please try it

<object data="data:application/pdf;base64,<?php echo base64_encode(content) ?>" type="application/pdf" style="height:200px;width:60%"></object>

How to display PDF file using PHP

I am taking a look at your path, and I see that you are using backslashes \ instead of a forward slash / and presuming you are not using Windows, it should be instead a /

example:

$file1 = 'uploads/aa.php';
header('Content-type: application/pdf');
header('Content-Disposition : inline; filename="' . $file1 . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

@readfile($file1);

more information on it here: https://www.w3schools.com/php/php_ref_filesystem.asp

Displaying PDF to Browser with PHP not working

You need to make sure you are not sending any text before writing headers.

Example of what not to do:

<!DOCTYPE html>
<html>
...
<?php
header("Content-type: application/pdf");

Example of how to fix that:

<?php
header("Content-type: application/pdf");
?>
<!DOCTYPE html>
<html>
...

In addition your script is very insecure. Here's what you should do, your entire PHP script should be:

<?php
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
if (!is_readable($loc)) {
http_response_code(404);
echo "Cannot find that file";
die();
}
if (strtolower(pathInfo($loc,PATHINFO_EXTENSION)) != "pdf") {
http_response_code(403);
echo "You are not allowed access to that file";
die();
}

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$title);
header("Content-Length: ".filesize($loc));
readfile($loc);

If you want to show things like a page title or a frame around it, you should use an iframe in another "wrapper" page:

<?php 
$loc = filter_input(INPUT_GET,"loc");
$title = filter_input(INPUT_GET,'title')?:basename($loc);
?>
<html><head><title>My title</title></head>
<body>
<iframe src="/view.php?<?php echo ($loc?"loc=$loc":"").($title?"title=$title":"") ?>">
</iframe>
</body>
<html>

Display a pdf with PHP not displayed correctly

Have you tried embedding it using html object tag?

<object data="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf">
<embed src="https://storage.server_test.com/agc/catalogs/catalog1.pdf" type="application/pdf" />
</object>

Alternatively in php, try this header:

header('Content-Disposition: attachment; filename="' . $filename . '"');

Finally, if @readfile($file) isn't working, try:

echo @file_get_contents($file);

After trying this myself locally, I think I managed to get it working. Try this:

<?php

$remote_pdf = 'http://www.saronicferries.gr/content/pdfFiles/sample.pdf';
$remote_file_name = end(explode('/', $remote_pdf));
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'. $remote_file_name .'"');
header('Content-Transfer-Encoding: binary');
readfile($remote_pdf);

?>

Output:

Sample Image

I think what was the problem is that filesize($file) was failing for remote file. After removing that and header('Accept-Ranges: bytes');, it works.



Related Topics



Leave a reply



Submit