【问题标题】:open a pdf inline in the browser. File link has no extension在浏览器中打开一个 pdf 内联。文件链接没有扩展名
【发布时间】:2019-10-18 23:02:20
【问题描述】:

我有一个 pdf 生成的发票,我喜欢在浏览器中内联显示: 链接例如:guest/view/invoice/vqhH8sujcOrpg0t2nF4WQExDLKXmYTNe 没有扩展名。

以下代码在我将其更改为附件时加载 PDF 文档失败,但在我喜欢内联显示时提供下载。

$file = site_url('guest/view/generate_invoice_pdf/' . $invoice_url_key);
            $filename = 'factuur.pdf';
            header("Content-type: application/pdf");
            header('Content-Disposition: inline; filename="' . $filename . '"');
            header('Content-Transfer-Encoding: binary');
            header('Accept-Ranges: bytes');
            echo $file;

【问题讨论】:

标签: php


【解决方案1】:

对不起,虽然我进行了彻底的搜索,但我没有解决方案是这样的: 原文来自:Chrome has "Failed to load PDF document" error message on inline PDFs

我一直在努力解决同样的问题。这与我在浏览器中获得一致的结果一样接近。我认为您可能遇到问题的原因是某些 PDF 太大而 readfile() 无法正确处理。试试这个:

$fp = fopen($file, "r") ;

header("Cache-Control: maxage=1");
header("Pragma: public");
header("Content-type: application/pdf");
header("Content-Disposition: inline; filename=".$myFileName."");
header("Content-Description: PHP Generated Data");
header("Content-Transfer-Encoding: binary");
header('Content-Length:' . filesize($file));
ob_clean();
flush();
while (!feof($fp)) {
   $buff = fread($fp, 1024);
   print $buff;
}
exit;````

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-28
    • 1970-01-01
    • 2011-12-31
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    相关资源
    最近更新 更多