Display pdf blob file from database
Asked Answered
C

3

6

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

Califate answered 3/12, 2016 at 14:23 Comment(2)
Every time you use the mysql_ database extension in new code a Kitten is strangled somewhere in the world it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the PDO or mysqli database extensions. Start hereTuna
I have been working on yii as developer,before on yii,I just tried on php,,ok. And I need help...Califate
A
12

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>
Alan answered 3/12, 2016 at 15:4 Comment(1)
resolved by using an iframe tag ..thank you @Dolly AswinCalifate
G
3

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");
Geehan answered 13/10, 2019 at 16:5 Comment(0)
C
2

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>
Chaussure answered 3/7, 2020 at 15:32 Comment(2)
This just saved me. stripslashes was my issue.Burroughs
glad its still helping.Chaussure

© 2022 - 2024 — McMap. All rights reserved.