【问题标题】:Failed to load PDF document in my PHP code无法在我的 PHP 代码中加载 PDF 文档
【发布时间】:2017-04-04 00:22:52
【问题描述】:

我是 PHP 新手,正在尝试通过代码实现以下功能

功能: 我有一个下载按钮,点击后我应该能够下载存储在 htdocs-->xxfilename-->abc.pdf 下的 .pdf 文件;

代码: 我在我的 xyz.php 网页中使用以下代码

<?php
$title = "Learning";
$content = '
            <h3> Intro</h3>
            <p> 
                Introduction goes here
            </p>
            <form action = "xyz.php" method = "post" name = "downloadform">
                <input type="submit" value="Download" name="dwnld_file"/>
            </form>
        ';
if (isset($_POST['dwnld_file'])) {
    mysql_connect("localhost", "root", "");
    mysql_select_db("PQR");
    $res = mysql_query("Select * from tab1");
    while ($row = mysql_fetch_array($res)) {
        $file = '$row["col2"]';
        header('Content-Type: application/pdf');
        header('Content-Disposition: attachment; filename="' . $row["col2"] . '"');
        header('Content-Transfer-Encoding: binary');
        header('Accept-Ranges: bytes');
        header('Content-Length:' . filesize($file));
        readfile($file);
    }
}
include 'Template.php';
?>

错误: 我的 pdf 文件正在下载,但打开时显示“加载 pdf 文档失败”

请帮助我哪里错了。

***编辑**** 我尝试了不同的方法,我的文件正在下载,但仍然显示“无法加载 pdf 文档”

我的其他方法代码如下

<form action = "xyz.php" method = "post" name = "downloadform">
        <input type="submit" value="Download " name="dwnld_file"/>
    </form>
    <?php
    if (isset($_POST['dwnld_file'])) {
        mysql_connect("localhost", "root", "");
        mysql_select_db("test");
        $res = mysql_query("Select * from tab1");
        while ($row = mysql_fetch_array($res)) {
            $file = $row["col1"];
            echo $file;

            header('Content-Type: application/pdf');
            header('Content-Disposition: attachment; filename="' .$file. '"');
            header('Content-Transfer-Encoding: binary');
            header('Accept-Ranges: bytes');
            header('Content-Length:' . filesize($file));
            readfile('myfile/'.$file);
        }
    }
    ?>

如果我做错了什么,请告诉我。

【问题讨论】:

标签: php pdf download


【解决方案1】:

在进行了更多研究后,我实施了另一个解决方案,并且能够解决问题。

下面是我用来实现的代码。

概念:

1) 我们可以使用 HTML+JSP+PHP 来获得这个功能。

2) 在 HTML + JSP 端,我们需要调用 eventListener 来捕捉点击按钮的事件。

3) 此事件将重定向到 php 页面,该页面会将 pdf 文件下载到您的本地计算机。

代码片段:(这里 XYZ 是您要显示下载按钮的页面名称)

在 XYZ.php 中

<input type="button" id="btnshow" value="Download" name="dwnld" />
        <script>
            var btn = document.getElementById('btnshow');
            btn.addEventListener('click', function () {
                document.location.href = '<?php echo 'download.php'; ?>'
            })
        </script>

在download.php中

<?php
mysql_connect("localhost", "root", "");
mysql_select_db("database_name");
$res = mysql_query("Select * from table_name");
while ($row = mysql_fetch_array($res)) {
    $file = 'Pdf_File/' . $row["Path"];
    $filename = $row["Path"];
    header('Content-Type: application/pdf');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Transfer-Encoding: binary');
    header('Accept-Ranges: bytes');
    readfile($file);
}
?>

希望对像我这样的新手有所帮助。

编码愉快!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-14
    • 2018-07-19
    • 2018-05-24
    • 2018-01-10
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    相关资源
    最近更新 更多