如何使用 php 显示来自 mysql 的 .pdf 文件

How to display .pdf files from mysql using php

亲爱的,我可以显示来自 mysql 的图像。但是当我想显示也存储在我的数据库中的 pdf 时,我无法做到这一点。当我使用此代码时,此代码会显示一小块绿色图像,但不会显示 .pdf 文件。此代码适用于 image 。但我无法显示 .pdf 文件。 任何人都可以帮助我告诉我我必须在我的代码中的什么地方进行更改。我要求请提供能够显示 PDF 文件的正确更改行。 提前致谢`

<?php

    $servername = "localhost";
    $username   = "root";
    $dbname     = "db_dat";
    $password   = "";


    $conn = mysqli_connect($servername, $username, $password, $dbname);

    $passno="";
    $doi="";
    $doe="";
    $poi="";
    $empid='';
    $emp_name='';
    $desg='';
    $comp_add='';
    $dob='';
    $img='';


   if (isset($_POST['pass'])) {
        $passno=$_POST['pass'];
    }
    if (isset($_POST['dateofissue'])) {
        $doi=$_POST['dateofissue'];
    }
    if (isset($_POST['dateofexpiry'])) {
        $doe=$_POST['dateofexpiry'];
    }
    if (isset($_POST['issueplace'])) {
        $poi=$_POST['issueplace'];
    }
    if (isset($_POST['issueplace'])) {
        $poi=$_POST['issueplace'];
    }
    if (isset($_POST['content'])) {
        $empid=$_POST['content'];
    }
    if (isset($_POST['name'])) {
        $emp_name=$_POST['name'];
    }
    if (isset($_POST['designation'])) {
        $desg=$_POST['desgination'];
    }
    if (isset($_POST['companyaddress'])) {
        $comp_add=$_POST['companyaddress'];
    }
    if (isset($_POST['dateofbirth'])) {
        $dob=$_POST['dateofbirth'];
    }
    if (isset($_POST['image'])) {
        $img=$_POST['image'];
    }



    $sql = "SELECT * from upload WHERE EMP_ID='".$empid."'";
    $result = mysqli_query($conn,$sql) ;
    while($row = mysqli_fetch_array($result)){
       $img= '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
       $passno=$row['passport_no'];
       $doi=$row['dateofissue'];
       $doe=$row['dateofexpiry'];
       $poi=$row['placeofissue'];
       $empid=$row['EMP_ID'];
       $emp_name=$row['employee_name'];
       $desg=$row['designation'];
       $comp_add=$row['company_address'];
       $dob=$row['dateofbirth'];
    }
?>    
<!DOCTYPE html>
<html>
    <head>


        <body>
            <form method="POST" action="" >
                <table>
                    <tr><td>Enter Employee Id</td> <td><input type="text" name="content"   id="content"></td></tr>
                    <tr><td><input type="submit" class="FormSubmit"></td></tr>
                    <tr><td>Employee Name</td> <td>        <?php echo $emp_name; ?> </td></tr>
                    <tr><td>Employee desgination</td> <td> <?php echo $desg; ?> </td></tr>
                    <tr><td>Company Address</td> <td>      <?php echo $comp_add; ?> </td></tr>
                    <tr><td>DOB.</td> <td>                 <?php echo $dob; ?> </td></tr>
                    <tr><td>Date of Issue</td> <td>        <?php echo $doi; ?> </td></tr>
                    <tr><td>Date of Expiry</td> <td>       <?php echo $doe; ?> </td></tr>
                    <tr><td>Place of Issue</td> <td>       <?php echo $poi; ?> </td></tr>
                    <tr><td>Passport Image</td> <td>       <?php echo $img; ?> </td></tr>
                    <div class="space"></div>
                    <div id="flash"></div>
                    <div id="show"></div>
                    </div>
                </table>
            </form>    
        </body>
    </head>    
</html>

这是因为您添加的是图片而不是 pdf...没有任何带有 <pdf 的标签,就像我们为图片添加的标签一样,因此创建可点击的 link.There pdf 和图片。

您的可点击 link 将由

生成
 $img="<a href='"$row['image']."'>Download </a> ";

另一种方法是使用 iframe

您将在其中创建 iframe 并在 iframe 中打开 pdf

 echo" <iframe src='"$row['image']."'></iframe>";

row['image'] 必须有像

这样的完整路径
 http://hostname.com/uploads/yourPDFfile.pdf

在显示 pdf 文件之前,首先检查它是否为 pdf,然后从您的目录中给出 pdf 图像的路径

要显示 pdf 文件,您还必须设置适当的 headers 才能显示文件。 尝试设置这些 headers 并将 $filename 替换为您的文件路径。

  header('Content-type: application/pdf');
  header('Content-Disposition: inline; filename="' . $filename . '"');
  header('Content-Transfer-Encoding: binary');