skip to Main Content

I have BLOB information from my database and trying to display a PDF file in browser using this:

echo "<object data='data:application/pdf;base64,".base64_encode($row['PDF_File'])."' type='application/pdf' style='height:600px;width:60%'></object>";

But with the result I get a problem like this:

failure

The error message (originally in Russian) in English is:

Error

The PDF document could not be loaded.

To reboot

If I change base64_encode to base64_decode it will crack, if I delete this, it won’t show up.

New PDFs are added to DB by JavaScript script:

var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
console.log(reader.result);
  const data = {
    file_pdf: reader.result
  };
  sendForm(data);
};

What can I do to solve this?

2

Answers


  1. Chosen as BEST ANSWER

    Thanks a lot to Giacomo1968.

    I found out that in <object> line with metadata duplicates the data:application/pdf;base64, already in the database field. So we need just remove that part.

    echo "<object data='data:application/pdf;base64,".$row['PDF_File']."' type='application/pdf' style='height:600px;width:60%'></object>";
    

    Turns into:

    echo "<object type='application/pdf' data='".$row['PDF_File']."'></object>";
    

  2. This answer on StackOverflow explains the issue.

    One idea of how to get the PDF is creating a PHP file that directly downloads the PDF file like this:

    <?php
    $row = mysqli_fetch_assoc($result);
    header("Content-type: application/pdf");
    echo $row['PDF_File'];
    ?>
    

    Or you can try to force a download by adding the Content-Disposition header like this; change the value of $filename to be whatever the filename should be:

    <?php
    $row = mysqli_fetch_assoc($result);
    $filename = 'the_pdf.pdf';
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    echo $row['PDF_File'];
    ?>
    

    Update 1: Based on the original poster’s comments:

    “All PDFs in my DB starts with "data:application/pdf;base64”

    If that is the case, you don’t have to use base64_encode again. Just change your line to the following; removing that base64_encode:

    echo "<object data='data:application/pdf;base64,".$row['PDF_File']."' type='application/pdf' style='height:600px;width:60%'></object>";
    

    Or using my example with the download, change it to the following; note I am using base64_decode for the echo line to properly decode the Base64:

    <?php
    $row = mysqli_fetch_assoc($result);
    $filename = 'the_pdf.pdf';
    header('Content-type: application/pdf');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    echo base64_decode($row['PDF_File']);
    ?>
    

    Update 2: If none of above works, my only guess is the JavaScript upload process is messed up. Or the database storage is messed up. Instead of BLOB for DB storage, try LONGTEXT for storage because you are not storing binary data but a very long Base64 encoded text string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search