【问题标题】:Display pdf blob file from database显示数据库中的 pdf blob 文件
【发布时间】:2017-04-18 08:11:29
【问题描述】:

我已将 pdf 文件存储到数据库中,即 blob 类型。 现在我想显示 pdf 像

$sqll="select * from pdff";
$query=mysql_query($sqll) or die(mysql_error());
$result=mysql_fetch_array($query);
$content=$result['pdf'];
<object data="<?php echo $content;?>" type="application/pdf" style="height:200px;width:60%"></object>

但在浏览器中显示..

> endobj 6 0 obj << /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] /ColorSpace << /Cs1 7 0 R /Cs2 10 0 R >> /ExtGState << /Gs2 34 0 R /Gs1 35 0 R >> /Font << /F1.0 31 0 R >> /XObject << /Im4 21 0 R /Im1 8 0 R /Im3 16 0 R /Im2 11 0 R /Im5 26 0 R /Im6 32 0 R /Fm3 23 0 R /Fm1 13 0 R /Fm2 18 0 R /Fm4 28 0 R >> /Properties << /Pl2 36 0 R /Pl1 37 0 R >> >> endobj 23 0 obj << /Length 24 0 R /Filter /FlateDecode /Type /XObject /Subtype /Form /FormType 1 /BBox [649 536 669 556] /Resources 25 0 R /Group << /S /Transparency /CS 10 0 R /I true /K false >> >> stream xMŽAƒ0ï}žÀĉmȹ/àÄU+Íÿ¥:(\|˜]ïî etc

我试过了

 <object data="<?php echo base64_decode($content);?>" type="application/pdf" style="height:200px;width:60%"></object>

但没用...请帮助meeee

【问题讨论】:

  • 每次在新代码中使用the mysql_ 数据库扩展a Kitten is strangled somewhere in the world 时,它都已被弃用,并且已经存在多年,并且在 PHP7 中永远消失了。如果您只是学习 PHP,请花精力学习 PDO 或 mysqli 数据库扩展。 Start here
  • 我一直在 yii 作为开发人员工作,在 yii 之前,我只是在 php 上尝试过,好的。我需要帮助...

标签: php html pdf blob


【解决方案1】:

如果您的数据仍在Blob 中,您需要使用base64_encode() 对数据进行编码。 请尝试一下

<object data="data:application/pdf;base64,<?php echo base64_encode(content) ?>" type="application/pdf" style="height:200px;width:60%"></object>

【讨论】:

  • 已通过使用 iframe 标签解决 ..谢谢@Dolly Aswin
【解决方案2】:

我希望以下内容完全符合您的要求:

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename=name.pdf');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
@readfile("data:application/pdf;base64,$content");

【讨论】:

    【解决方案3】:

    我知道这个线程很旧,但我最近有一个类似的问题要解决,并写了这个快速指南来涵盖它。 https://medium.com/@alexmoran_19787/display-pdf-from-blob-file-11996146dbc0

    基本上取决于您在将pdf输入数据库时​​是否添加斜杠,您可以通过以下方式查看它 我在我的控制器中有这个并将信息传递给一个 php 页面。设置标题,如果您添加斜线,请确保使用带斜线,否则您最终会得到原始数据或空白屏幕。

        $content = stripslashes($text["file"]);
        header('Content-type: application/pdf');
        header('Content-Disposition: inline; filename=document.pdf');
        header('Content-Transfer-Encoding: binary');
        header('Accept-Ranges: bytes');
        
        $this->load->view("administrator/load_pdf", [
            "title"=>"Display PDF",
            "pdf"=>$content,
        ]);
    

    这是来自 php 页面。

      <object data="data:application/pdf;base64,<?php echo base64_encode($content);?>" type="application/pdf" height="100%" width="100%"></object>
    

    【讨论】:

      猜你喜欢
      • 2020-02-20
      • 2014-02-10
      • 2020-12-09
      • 1970-01-01
      • 2012-10-21
      • 2011-03-02
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多