【问题标题】:Problems embedding PDF file sent with XSendFile in a webpage在网页中嵌入使用 XSendFile 发送的 PDF 文件时出现问题
【发布时间】:2016-03-25 20:13:17
【问题描述】:

我想在网页中嵌入 PDF 文件。我需要动态生成 PDF,以便我可以先对用户进行身份验证,所以我在 Apache 上使用 XSendFile。当我访问浏览器并立即提供 PDF 文件供下载时,我拥有的 PHP 文件运行良好。这是我正在使用的代码(http://www.brighterlamp.com/2010/10/send-files-faster-better-with-php-mod_xsendfile/ 提供)

// Get a list of loaded Apache modules
$modules = apache_get_modules();
if (in_array('mod_xsendfile', $modules)) {
    // Use XSendFile if possible
    header ('X-Sendfile: ' . $pathToFile);
    header ('Content-Type: ' . $documentMIME);
    header ('Content-Disposition: attachment; filename="' . $actualFilename . '"');
    exit;
} else {
    // Otherwise, use the traditional PHP way..
    header ('Content-Type: ' . $documentMIME);
    header ('Content-Disposition: attachment; filename="' . $actualFilename . '"');
    @ob_end_clean();
    @ob_end_flush();
    readfile($pathToFile);
    exit;
}

到目前为止一切顺利。现在我想使用对象标签将此 PDF 嵌入网页中,例如:

<object data="dynamicpdf.php" type="application/pdf">
   <p>PDF embed failed</a></p>
</object>

但这失败了。如果我将数据属性切换为静态 PDF 文件,那么它工作正常。

有什么想法吗?

【问题讨论】:

    标签: php html x-sendfile


    【解决方案1】:

    iframe 是否适合您?

    点赞&lt;iframe src="dynamicpdf.php"&gt;

    Content-Disposition 标头强制下载。去掉它。

    一般建议: 我不会使用像 apache_get_modules 这样假定特定网络服务器环境的函数。

    如果您将来从 mod_php 或 apache 切换出来会怎样?您的代码将中断。

    相反,我会在流式 php 响应中进行传递,这比将整个 PDF 缓冲到 RAM 中然后发送它的输出更节省内存。

    通过使用 PHP 流式传输 PDF,您也只有一种实现,它的速度与 x-sendfile 相同:

    Streaming a large file using PHP

    【讨论】:

    • 替换为
    • 这取决于浏览器。嵌入 PDF 的用例是什么?为什么不使用 HTML。网络不是为 PDF 制作的。
    • 但是要正确嵌入 PDF,请删除 Content-Disposition 标头。这会强制下载。我忘了提。
    • PDF 包含最终用户需要打印的运输标签/说明。 PDF 是一种更简洁的交付方式。删除 Content-Disposition 就可以解决问题。适用于
    • 不是为了那个。我的回答有点误导。我会更新的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-26
    • 1970-01-01
    • 2021-02-27
    • 2018-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多