1

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?

4
  • 1
    you should probably provide a translation for any non-english language error messages. is the byte stream in your database base64 encoded? describe how new PDFs are added to the DB. Commented Mar 20 at 1:07
  • Giacomo1968, yea it's pdf. Frank Thomas, yes, it's encoded. Error says "failed to load PDF file". New PDFs are added to DB by JS script: var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function() { console.log(reader.result); const data = { file_pdf: reader.result }; sendForm(data); };
    – Hopm Mpoн
    Commented Mar 20 at 1:20
  • 1
    so that covers the clientside upload, but not the serverside (the php or whatever of the post request). either way I'm still not seeing anything that indicates base64 encoding, so its likely that using base64 encode/decode is not the correct transformation of byte stream. have you just tried returning the file stream contents instead of trying to encode it? Commented Mar 20 at 1:34
  • Yea i tried this, but not working either. All PDFs in my DB starts with "data:application/pdf;base64,"
    – Hopm Mpoн
    Commented Mar 20 at 1:41

2 Answers 2

3

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.

2
  • I see how it must work, but that doesn't help me somehow. When i open downloaded file i have the same issue(
    – Hopm Mpoн
    Commented Mar 20 at 1:39
  • Update: in the first case it looks normal from F12, but there is blank space 0x0 instead of pdf viewer. In the second case i get the same issue, but the pdf contain characters like "u«Zjљe‰Ж­Љ‰яҐЧЫjЗєа•AґДёР(ДЂАЃЅ‰Ё(рр(ЅLЂЅ(шш)•№‘Ѕ‰Ё(ИЂАЃЅ‰Ё(рр(Ѕ9ХµМЃlАЂДЂАЃIt(шш)•№‘Ѕ‰Ё(МЂАЃЅ‰Ё(рр(Ѕ" and so on
    – Hopm Mpoн
    Commented Mar 20 at 2:00
1

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>";

Not the answer you're looking for? Browse other questions tagged .