title: Save a PDF created with FPDF php library in a MySQL blob field
body: <p>I need to create a pdf file with the <a href="http://www.fpdf.org" rel="nofollow noreferrer">fpdf</a> 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.</p>
  
  <p>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.</p>
  
  <p>My code is something like this:</p>
  
  <pre><code>$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
  $content = $pdf-&gt;Output("", "S"); //return the pdf file content as string
  $sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
  mysql_query($sql);
  </code></pre>
  
  <p>to store the pdf, and like this:</p>
  
  <pre><code>$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;
  </code></pre>
  
  <p>to send it  to the browser for downloading.
  What am I doing wrong?</p>
  
  <p>If I change my code to:</p>
  
  <pre><code>$pdf = new MyPDF(); 
  $pdf-&gt;Output(); //send the pdf to the browser
  </code></pre>
  
  <p>the file is correctly displayed, so I assume that is correctly generated and the problem is in the storing in the db.</p>
  
  <p>Thanks in advance.</p>