Display pdf blob file from database

后端 未结 3 980
遇见更好的自我
遇见更好的自我 2021-01-13 04:26

I have stored pdf file into database i.e blob type. Now I wanna display pdf like

$sqll=\"select * from pdff\";
$query=mysql_query($sqll) or die(mysql_error()         


        
相关标签:
3条回答
  • 2021-01-13 05:03

    If your data still in Blob, you need to encode your data using base64_encode(). Please try it

    <object data="data:application/pdf;base64,<?php echo base64_encode(content) ?>" type="application/pdf" style="height:200px;width:60%"></object>
    
    0 讨论(0)
  • 2021-01-13 05:09

    I hope the following will do exactly what you want:

    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");
    
    0 讨论(0)
  • 2021-01-13 05:26

    I know this thread is old but I recently had a similar issue to solve and wrote this quick guide to cover it. https://medium.com/@alexmoran_19787/display-pdf-from-blob-file-11996146dbc0

    Basically Depending on if you addslashes or not when inputing the pdf into the database you can view it by the following I had this in my controller and passed the info to a php page. Setting the headers and if you addslashes make sure to stripslashes otherwise you will just end up with the raw data or blank screen.

        $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,
        ]);
    

    This is from the php page.

      <object data="data:application/pdf;base64,<?php echo base64_encode($content);?>" type="application/pdf" height="100%" width="100%"></object>
    
    0 讨论(0)
提交回复
热议问题