【问题标题】:How to display pdf in php如何在php中显示pdf
【发布时间】:2013-08-05 02:39:36
【问题描述】:

我正在尝试在 php 中显示一个 pdf 文件,我使用过:

<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"></iframe>
        </div>
    </body>
</html>

它在 HTML 中运行良好,但是当我想将此代码用于 php 文件时,它什么也不显示,只尝试下载 pdf 文件。 有人可以帮帮我吗?

【问题讨论】:

  • 那里没有 PHP 代码。你是如何使用 PHP 的?
  • 你是否使用了.php的文件扩展名?
  • 你的代码对我有用
  • 也可以试试header("Content-type: application/pdf"); header("Content-Disposition: inline; filename=filename.pdf"); @readfile('path\to\filename.pdf');

标签: php pdf


【解决方案1】:

有很多选项可以使用:(都经过测试)。

这里有两种方法。

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

or:(注意转义的双引号)。为它分配名称时也需要使用相同的方法。

<?php

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

?>

name="myiframe" id="myiframe"

需要改成:

name=\"myiframe\" id=\"myiframe\" 在 PHP 中。

请务必查看:this answer on SO,了解有关该主题的更多选项。

脚注:尝试在 Windows 8 中查看 PDF 文件时存在已知问题。如果未安装浏览器插件,安装 Adob​​e Acrobat Reader 是查看这些类型文档的更好方法。

【讨论】:

  • 如果你从不保存@readfile()怎么办?路径从何而来?
【解决方案2】:

试试下面的代码

<?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);
?>

【讨论】:

    【解决方案3】:

    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>
    

    【讨论】:

      【解决方案4】:
      if(isset($_GET['content'])){
        $content = $_GET['content'];
        $dir = $_GET['dir'];
        header("Content-type:".$content);
        @readfile($dir);
      }
      
      $directory = (file_exists("mydir/"))?"mydir/":die("file/directory doesn't exists");// checks directory if existing.
       //the line above is just a one-line if statement (syntax: (conditon)?code here if true : code if false; )
       if($handle = opendir($directory)){ //opens directory if existing.
         while ($file = readdir($handle)) { //assign each file with link <a> tag with GET params
           echo '<a target="_blank" href="?content=application/pdf&dir='.$directory.'">'.$file.'</a>';
      }
      

      }

      如果您单击链接,将出现一个带有 pdf 文件的新窗口

      【讨论】:

        【解决方案5】:

        从数据库中显示 pdf 文件的简单方法,我们可以下载它。
        $resume 是来自数据库的 pdf 文件名。
        ../resume/filename 是存储文件的文件夹路径。

        <a href="../resumes/<?php echo $resume; ?>"/><?php echo $resume; ?></a>
        

        【讨论】:

          【解决方案6】:

          如果使用 pdf 或 img 则很容易

          return (in_Array($file['content-type'], ['image/jpg', 'application/pdf']));
          

          【讨论】:

            猜你喜欢
            • 2012-03-31
            • 2014-06-07
            • 1970-01-01
            • 2013-02-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-04-17
            相关资源
            最近更新 更多