How to display pdf in php

I'm trying to display a pdf file in php, I used:

<html>
<head>
<style type="text/css">
#myiframe {width:600px; height:100%;} 
</style>
</head>
<body>
<div id="scroller">
<iframe name="myiframe" id="myiframe" src="xml.pdf">
</div>
</body>
</html>

it works in html well, but when I want to used this code into a php file, it displays nothing and only try to download the pdf file. Can anybody help me?


There are quite a few options that can be used: (both tested).

Here are two ways.

header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=filename.pdf");
@readfile('pathtofilename.pdf');

or: (note the escaped double-quotes). The same need to be use when assigning a name to it.

<?php

echo "<iframe src="file.pdf" width="100%" style="height:100%"></iframe>";

?>

Ie : name="myiframe" id="myiframe"

would need to be changed to:

name="myiframe" id="myiframe" inside PHP.

Be sure to have a look at: this answer on SO for more options on the subject.

Footnote : There are known issues when trying to view PDF files in Windows 8. Installing Adobe Acrobat Reader is a better method to view these types of documents if no browser plug-ins are installed.


Try this below code

<?php
$file = 'dummy.pdf';
$filename = 'dummy.pdf';
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file);
?>

Demo


从https://pdfobject.com/下载PDFObject库并检查下面的代码:我希望它能起作用。

<!DOCTYPE html>
<html>
<head>
    <title>Pdf Read</title>
    <style>
          .pdfobject-container { height: 500px;}
          .pdfobject { border: 1px solid #666; }
   </style>
   <script src="pdfobject.min.js"></script>
</head>
<body>
        <div id="example1"></div>
        <script>PDFObject.embed("pdfread.pdf", "#example1");</script>
</body>
</html>
链接地址: http://www.djcxy.com/p/36464.html

上一篇: HTML页面中PDF文件的MIME编码

下一篇: 如何在php中显示pdf