tags:

views:

2447

answers:

6

I need to create a pdf file with the fpdf library and save it in a blob field in my MySQL database. The problem is, when I try to retrieve the file from the blob field and send it to the browser for the download, the donwloaded file is corrupted and does not display correctly.

The same pdf file is correctly displayed if I send it immediately to the browser without storing it in the db, so it seems some of the data gets corrupted when is inserted in the db.

My code is something like this:

$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);

to store the pdf, and like this:

$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content));
header('Content-Disposition: attachment; filename=myfile.pdf');
print $content;

to send it to the browser for downloading. What am I doing wrong?

If I change my code to:

$pdf = new MyPDF(); 
$pdf->Output(); //send the pdf to the browser

the file is correctly displayed, so I assume that is correctly generated and the problem is in the storing in the db.

Thanks in advance.

+1  A: 

You shouldn't strip any (back)slashes. When you add them, they are used as escape characters in the query and do not appear in the field.

Try changing this

$content = stripslashes($rs['myblobfield']);

into this:

$content = $rs['myblobfield'];
Ignas R
So easy and so obvious, thanks for showing me my mistake :)
Davide Gualano
+1  A: 

Compare the differences in the strings to help debug the problem.

$pdf = new MyPDF();
echo $pdf->Output("", "S"); //send the pdf string to the browser
exit;

and

$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);
$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
echo $content; //send the pdf string to the browser
exit;
Bart
+1  A: 

Hi!

Actually for security reasons:

  1. Do not use addslashes() but use mysql_real_escape_string() instead

In this specific case (as it is binary data):

  1. Remove stripslashes()
  2. Replace addslashes() by mysql_real_escape_string()
Toto
A: 

I get the same thing as the first guy.

I have copied his code. I enter the data into my db. but when I try to retrieve it, I get "format error: not a PDF or corrupted"

But if I just $pdf->Output(), it shows up fine.

I've tried addslashes(), mysql_real_escape_string(), nothing...and it doesn't work.

I have echoed $content before going into the db, and coming out. I've compared the two, and they are the same.

if I print before the header(), I get these error messages:

Warning: Cannot modify header information - headers already sent by (output started at /home/content/a/m/o/amonit/html/games2.php:9) in .../pdf.php on line 10

Warning: Cannot modify header information - headers already sent by (output started at /home/content/a/m/o/amonit/html/games2.php:9) in .../pdf.php on line 11

Warning: Cannot modify header information - headers already sent by (output started at /home/content/a/m/o/amonit/html/games2.php:9) in .../pdf.php on line 12

please help, or ask me some questions to help diagnose it. thanks JJ

jj4188
A: 

SOLVED IT! I too was having the same problem and was trying the above mentioned ideas. What fixed this for me was that my MYSQL database was storing the pdf as a 'blob' instead of 'mediumblob'. A blob field is limited to 64K and was thereby truncating the rest of my pdf which included the valuable data including EOF (hence the nasty Adobe Error). To confirm, download the PDF file it serves up instead and look at the file size. If it's exactly 64K you'll need to make more room in the DB by setting the field to Medium Blob. Thanks everyone for the help!

By The Way I followed Toto's advice and used mysql_real_escape_string() instead along with 1. Remove stripslashes() 2. Replace addslashes() by mysql_real_escape_string()

With those changes to the original poster's code it worked!

NateDawg
A: 

hi friend anyone can help me for solve this problem 1. i have generate the pdf but idont know how to save the pdf file in one directory. thanks ragards damu

damu
See this: http://www.fpdf.org/en/doc/output.htm
Davide Gualano