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.
var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function() { console.log(reader.result); const data = { file_pdf: reader.result }; sendForm(data); };