【问题标题】:Displaying contents of a pdf in php [duplicate]在php中显示pdf的内容[重复]
【发布时间】:2014-02-24 06:34:59
【问题描述】:

当在上传的 pdf 文件上回显 file_get_contents 时,我得到一堆废话,如下所示:

6}ÕìúÖ

是否有解码此信息的方法?

【问题讨论】:

  • 您期待什么? PDF 具有二进制文件格式,其内容从不只是您可以在控制台上阅读的纯文本。即使它没有被压缩,在所谓的“纯”文本中和周围也有大量的基础设施。

标签: php pdf


【解决方案1】:

此代码可能对您有所帮助

<?php
 $file = 'path/to/PDF/file.pdf';
 $filename = 'filename.pdf';
 header('Content-type: application/pdf');
 header('Content-Disposition: inline; filename="' . $filename . '"');
 header('Content-Transfer-Encoding: binary');
 header('Accept-Ranges: bytes');
 @readfile($file);

?>

成功

【讨论】:

    【解决方案2】:

    如果您想阅读/更改 pdf 文件的内容,您必须使用像 FPDF 这样的库

    如果要显示 pdf,则必须设置正确的内容类型:

    header('Content-type: application/pdf');
    header('Content-Disposition: inline;');
    echo file_get_contents('yourpdf.pdf');
    

    (不过 readfile 是一个更好的选择)

    【讨论】:

      【解决方案3】:

      试试这个: http://www.fpdf.org/

      这是一个在与此相同的其他问题中也被建议的库。

      【讨论】:

      • "让浏览器知道一个 PDF 文件即将到来。" - 什么浏览器?他正在尝试阅读 pdf 文件,而不是将其发送给客户端。
      • 哦,你是对的,我完全看错了! @user4035 我将只更新库的答案。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2019-11-13
      • 1970-01-01
      相关资源
      最近更新 更多