【问题标题】:How to view pdf without downloading?不下载怎么看pdf?
【发布时间】:2019-10-23 08:50:48
【问题描述】:

大家好,我正在使用选择命令从数据库中查看 pdf,它正在工作,但每次刷新页面时它都会尝试下载所有 pdf。我希望它是一个正常的列表,然后单击下载。

这是我的代码

<?php
function selectFishprice()
{
    try {
        $connection = connect();
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sqlQuery = "SELECT price AS pdf_path, Date FROM upload WHERE id = 1";
        $statement = $connection->query($sqlQuery);
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        $Fishprices = $statement->fetchAll();
        if (!empty($Fishprices)) {
            return $Fishprices;
        } else {
            return NULL;
        }
    } catch (PDOException $exception) {
        die("Error: " . $exception->getMessage());
    }
}

$fishPrices = selectFishprice();
?>
<table id="users">
    <thead>
        <tr>
            <th scope="col">Fish Market Price</th>
            <th scope="col">As of</th>
        </tr>
    </thead>
    <tbody>
<?php
    if (!empty($fishPrices)) {
        foreach ($fishPrices as $fishPrice) {
            $date = strtotime($fishPrice['Date']);
?>
            <tr class="table-primary">
            <td scope="row"><img src="<?= $fishPrice['pdf_path'] ?>"/></td>
            <td scope="row"><?= date('M j Y', $date)?></td>
            <?php
        }
    } else {
?>
        <tr class="table-primary">
            <td class="text-center" colspan="4" scope="row">No records found.</td>
        </tr>
<?php
    }
?>
    </tbody>
</table>

我从网站上阅读到使用“标题”,但我不明白我将“标题”放在哪里?感谢您的帮助,谢谢

【问题讨论】:

  • 您绝对没有显示任何与 PDF 相关的代码。
  • 你好。 $fishPrice['price'] 包含pdf路径“price”只是表中字段的名称。
  • 那你为什么使用PDF文件的路径作为img元素的src?这将如何尝试“下载”任何东西?
  • 因为我认为它与我制作的其他列表相似,所以我现在将 img 元素替换为 embed 有点工作。感谢您指出这一点。
  • 您想要显示下载链接还是在您的页面中嵌入 PDF?从你的问题看不清楚。

标签: php pdf view download


【解决方案1】:

使用anchor tag (i.e. &lt;a&gt;)

function selectFishprice()
{
    try {
        $connection = connect();
        $connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $statement = $connection->query("SELECT price, Date FROM upload WHERE id = 1 ");
        $statement->setFetchMode(PDO::FETCH_ASSOC);
        $Fishprices = $statement->fetchAll();
        if (!empty($Fishprices)) {
            return $Fishprices;
        } else {
            return NULL;
        }
    } catch (PDOException $exception) {
        die("Error: " . $exception->getMessage());
    }
}

$fishPrices = selectFishprice();

         <table id="users">
               <thead>
                    <tr>
                      <th scope="col">Fish Market Price</th>
                       <th scope="col">As of</th>
                        </thead>
                         <tbody>
                         <?php
                          if(!empty($fishPrices)) {
                             foreach ($fishPrices as $fishPrice) {
                                 $date = strtotime($fishPrice['Date']);
                              ?>
                             <tr class="table-primary">
                            <td scope="row"><a href ="<?= $fishPrice['price'] ?>" target="_blank"><img src="" alt="PDF"/></a></td>
                             <td scope="row"><?= date('M j Y', $date)?></td>
                             <?php
                             }
                           } else {
                             ?>
                           <tr class="table-primary">
                            <td class="text-center" colspan="4" scope="row">No records found.</td>
                                            </tr>
                                            <?php
                                        }
                                        ?>
                                        </tbody>
                                    </table>

【讨论】:

  • 谢谢。在意识到不应将其视为图像之前,我已经将 embed 标记替换为锚标记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 2019-07-05
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
相关资源
最近更新 更多