Rajesh
Rajesh

Reputation: 345

Display pdf blob file from database

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());
$result=mysql_fetch_array($query);
$content=$result['pdf'];
<object data="<?php echo $content;?>" type="application/pdf" style="height:200px;width:60%"></object>

but in browser it shows..

> 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

and I tried

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

but no use...please help meeee

Upvotes: 6

Views: 30846

Answers (3)

Alex M
Alex M

Reputation: 106

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>

Upvotes: 2

Jay Soni
Jay Soni

Reputation: 41

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");

Upvotes: 3

Dolly Aswin
Dolly Aswin

Reputation: 2794

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>

Upvotes: 12

Related Questions