Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Problem opening PDF after creating it using PHP and storing in the MySQL table.

New Here ,
Sep 08, 2009 Sep 08, 2009

Copy link to clipboard

Copied

Hello,

I am creating a pdf document and outputting it as a string using:

$mypdf = $pdf->Output('', 'S');

I am then putting it into a database as a BLOB and redirecting to page y using:

$insertSQL = sprintf("INSERT INTO PDF_file (pdf_name) VALUES (%s)",
                              GetSQLValueString($mypdf, "text"));

mysql_select_db($database_files, $files);
$Result1 = mysql_query($insertSQL, $files) or die(mysql_error());

I have a link on page y called:

<a href="home.php?pdf_id=<?php echo $row_getPDF['pdf_id'];?>">Click here to view your PDF.</a>

When whoever clicks the link, the following is processed:

if (isset($_GET['pdf_id'])) {
header("Content-type: application/pdf");
header('Content-disposition: attachment; filename="myresume.pdf"');
echo $row_getPDF['pdf_name'];
}

A box opens and I am giving the option to open or save the file. The pdf downloads but when I try to view it there is a couple lines present on the second page and that's it. The pdf is either not being stored properly or not downloaded properly. It works just fine when I am not converting it into a string. Any help is appreciated.

Thanks,
Kelsey

TOPICS
Server side applications

Views

983
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Sep 09, 2009 Sep 09, 2009

Copy link to clipboard

Copied

It works just fine when I am not converting it into a string.

Well, why convert into a string? It's a binary object. In fact, why store it in a database? Storing it in a database just bloats your tables. It would make more sense to store it outside the database, and just store the details of its location.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 09, 2009 Sep 09, 2009

Copy link to clipboard

Copied

The reason I am storing it in the database is because the pdf file being created needs to be sent to multiple users if needed and viewed by multiple users. Or is there a way to do this without storing it locally on a computer?  I have done more looking into the problem though and it seems like there is an issue in storing the string in the database and retrieving it. So what I have done is the following:

//create pdf, convert to string, assign it to a variable, and get pdf name from form

$pdf_content = $pdf->Output('$pdf', 'S');

$fileName = $_POST['pdf_name'];

$fileName .= '.pdf';

//insert into database

$insertSQL = sprintf("INSERT INTO my_table (pdf_name, pdf_content) VALUES (%s, %s)",
                       GetSQLValueString($fileName, "text"),
                       GetSQLValueString($pdf_content, "text"));
  mysql_select_db($database_mydatabase, $mydatabase);
  $Result1 = mysql_query($insertSQL, $mydatabase) or die(mysql_error());

  $insertGoTo = "/view/pdf.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));

Then on the page "pdf.php" I have a link to click on the pdf:
<a href="home.php?pdf_id=<?php echo $row_getPDF['pdf_id'];?>"><?php echo $row_getPDF['pdf_name'];?></a>

After creating the url I have:

if (isset($_GET['pdf_id'])) {

        $fileType = 'application/pdf';
        header("Content-type: ".$fileType."; charset=UTF-8");
        header("Content-disposition: attachment; filename=".$row_getPDF['pdf_name']);
        echo $row_getPDF['pdf_content'];

}

When I open the pdf, the pdf is corrupted and only a couple lines of the original pdf work. I have tried using  "$_SESSION['pdf_content'] = $pdf->Output('$pdf', 'S');" and replacing "echo $row_getPDF['pdf_content'];" with  "echo $_SESSION['pdf_content'];" and it works just fine. So what am I doing wrong when storing it in the database? The "pdf_content" column is set to MEDIUMBLOB.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 09, 2009 Sep 09, 2009

Copy link to clipboard

Copied

LATEST

I just got it working. I was missing the function addslashes() on the pdf string. The tutorial I was following wasn't using it. Thanks anyways for the help.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines