base64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file
$b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));
Answer from Machavity on Stack Overflowbase64_encode takes a string input. So all you're doing is encoding the path. You should grab the contents of the file
$b64Doc = chunk_split(base64_encode(file_get_contents($this->pdfdoc)));
base64_encode() will encode whatever string you pass to it. If the value you pass is the file name, all you are going to get is an encoded filename, not the contents of the file.
You'll probably want to do file_get_contents($this->pdfdoc) or something first.
Try this piece of code
$pdf_base64 = "base64pdf.txt";
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//Decode pdf content
$pdf_decoded = base64_decode ($pdf_content);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$pdf_decoded);
//close output file
fclose ($pdf);
echo 'Done';
With this you can create and open the file... but maybe this is not the answer to the question
$data = base64_decode($this->Get(self::Body));
file_put_contents('file.pdf',$data);
The answer is echo the content with a header :
$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;
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.
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>";