0

I need to save a pdf file into BLOB (database is MsSql 2012, but I would need it work on 2008 too).

Here is compilation of what I have tried so far:

(File where I save pdf into database)

function preparePDF2String($filepath)
{
    $handle = @fopen($filepath, 'rb');
    if ($handle)
    {
        $content = @fread($handle, filesize($filepath));
        $content = bin2hex($content);
        @fclose($handle);
        return "0x".$content;
    }
}

$out = preparePDF2String('pdf/pdf.pdf');
$q_blob = "INSERT INTO HISTORIA_ARCHIWUM_test(PDFName, PDFData) VALUES('First test pdf', $out) ";

$r_blob = mssql_query($q_blob,$polacz);

Results (ok, I think)

enter image description here

(Now the php file where I try to output the pdf)

// header('Content-type: application/pdf');
// readfile('pdf/pdf.pdf'); - this works just fine, so pdf is ok


$q_blob = "select top 1 * from HISTORIA_ARCHIWUM_test";
$r_blob = mssql_query($q_blob,$polacz);
$w_blob = mssql_fetch_array($r_blob);

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');

echo $w_blob['PDFData'];
//echo hex2bin ($w_blob['PDFData']); - I also tried this - same problem

Problem: when I try to output the pdf in the browser I get error (this pdf is of not supported format). It won't open in Adobe reader also. My guess is that I output it wrong, but maybe it is the way I save it?

2
  • Why not save the PDF outside the database in filesystem, save its path link in database and have PHP use the link (much like images)? You avoid converting the binary content to string. Commented Nov 10, 2015 at 14:06
  • It's my client requirement. He need big pdf respository that is easy to copy (automatic backup - one big database file) Commented Nov 10, 2015 at 14:15

1 Answer 1

2

EDIT from comments
It seems php+mssql truncates string when you get it from db, but its manageable in php.ini. I set mssql.textlimit = 160000 and mssql.textsize = 160000 and it works fine with Hex conversion and varchar(max).


You are transforming the pdf data before you save it to the database, you have to undo the transformation before you output it.

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');

echo hex2bin(substr($w_blob['PDFData'], 2));

Also why are you transforming the data in the first place, just save it as binary.

Sign up to request clarification or add additional context in comments.

9 Comments

Sorry for delayed answear (I had no access - yesterday was a non-working day here). I did try to undo transfromation, but with no sucess - I tried couple hex2binary functions (including those from php.net) and all see to have problem. Strange becouse in database it seems ok. You sugession not to do transformation is propably best way, but I have problem with that to. If I read pdf and try to save it in binary I get: Warning: mssql_query() [function.mssql-query]: message: The identifier that starts with '.....' is too long. Maximum length is 128. So far I was not able to deal with this.
What type of field is PDFData? Try using VARBINARY(MAX)
It is VARBINARY(MAX) - and when to think about it this accually might be a problem if I do conversion to HEX :) (I assume that varbinary means binary only) - I need to figure out how to make my query work, without conversion. Really simple test: INSERT INTO HISTORIA_ARCHIWUM_test(PDFName, PDFData) VALUES('First test pdf', $out) where out is read pdf.
Have you tried using a parameterized query to insert the data?
Try select top 1 CONVERT(TEXT,PDFData) as PDFData * from HISTORIA_ARCHIWUM_test
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.