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 Overflow
🌐
Base64.Guru
base64.guru › home › base64 converter › base64 encode
PDF to Base64 | Base64 Encode | Base64 Converter | Base64
The PDF to Base64 converter generates ready-made examples, depending on the selected output format.
🌐
YouTube
youtube.com › watch
PHP - How to convert pdf to base64 or base64 to pdf using PHP - YouTube
This video will show you, how to convert pdf to base64 or base64 to pdf using PHP
Published: January 27, 2020
🌐
Example Code
example-code.com › phpext › base64_pdf.asp
PHP Extension PDF File Encoding to Base64
Chilkat • HOME • Android™ • AutoIt • C • C# • C++ • Chilkat2-Python • CkPython • Classic ASP • DataFlex • Delphi DLL • Go • Java • JavaScript • Node.js • Objective-C • PHP Extension • Perl • PowerBuilder • PowerShell • PureBasic • Ruby • SQL Server • Swift • Tcl • Unicode C • Unicode C++ • VB.NET • VBScript • Visual Basic 6.0 • Visual FoxPro • Xojo Plugin
🌐
DEV Community
dev.to › rogeriotaques › uploading-pdf-files-as-base64-strings-in-php-and-saving-it-in-the-hard-disk-f7o
Uploading PDF files as base64 strings in PHP and saving it in the hard-disk. - DEV Community
February 14, 2020 - The approach taken was to convert the PDF file into a base64 string and submit that alongside the complementary data via POST method.
🌐
YouTube
youtube.com › stargan mitra teknologi
PHP: convert pdf to base 64 and base64 back to pdf - YouTube
php code to convert pdf file to base64 so you can save if in database beware of file size because saving file to database tend to grow database size excessively
Published: October 16, 2020
Views: 2K
🌐
Base64.Guru
base64.guru › home › developers › php › examples
Convert Base64 to PDF in PHP | Examples | PHP | Developers | Base64
Below I want to show you a basic example of how to do this, but before continuing I want to warn you that PDF files may contain malicious content that may jeopardize the security of users viewing such PDF files. Therefore, if you get Base64 from an untrusted source, you must sanitize the PDF contents by removing all active content (such as JavaScript, actions, embedded files). ... <?php # Define the Base64 string of the PDF file $b64 = 'JVBERi0xLjUKJYCBgoMKMSAwIG9iago8PC9GaWx0ZXIvRmxhdGVEZWNvZGUvRmlyc3QgMTQxL04gMjAvTGVuZ3RoIDg0OC9UeXBlL09ialN0bT4+CnN0cmVhbQp4AZVVbW/iOBD+K/NtW1W9+CUvzmlVCcjCcl2
🌐
Base64 Encode
base64encode.net › pdf-to-base64
PDF to Base64 Encode - Online Tool
PDF to Base64 · Base32 Encode · PHP base64_encode() Python base64.b64encode() Perl encode_base64() VB System.Convert.ToBase64String() C# System.Convert.ToBase64String() Java encode() Golang EncodeToString() Ruby Base64.encode64() MySQL to_base64() Dojo dojox.encoding.base64.encode() PostgreSQL ...
Find elsewhere
🌐
Facebook
facebook.com › laravelnews.in › posts › how-to-convert-pdf-file-to-base64-encode-using-php › 2349099678510747
Laravel News - How to convert PDF file to Base64 encode...
September 8, 2019 - Explore the things you love · Log in to Facebook · English (UK) · Русский · Українська · Suomi · 中文(简体) · Deutsch · العربية · More languages…
🌐
Stack Overflow
stackoverflow.com › questions › 70514296 › convert-fpdf-output-to-base64-string
php - Convert FPDF output to base64 string - Stack Overflow
I am working with the PHP API where I need to create the PDF file using the FPDF and then I wanted to save it to the database by converting it to the base64 string. When I have tried the default $pdf->Output() method then I got the PDF file ...
🌐
PHP Freaks
forums.phpfreaks.com › php coding › php coding help
Decode base64 pdf file - PHP Coding Help - PHP Freaks
April 16, 2009 - Hi, I'm trying to work on a script that can decode an encoded pdf file from xml. I have the xml in a variable called $result. Inside that xml there is code for the pdf. It is encoded in base64. My question is how can I pull the data from the xml, decode it, and display the pdf file that it is. Th...
🌐
Stack Overflow
stackoverflow.com › questions › 48082329 › how-to-encode-pdf-file-in-base64-format › 48335986
php - How to encode pdf file in base64 format? - Stack Overflow
January 3, 2018 - $filename = "../pdfs/reactjs_tutorial.pdf"; $handle = fopen($filename, 'rb'); $contents = fread($handle,filesize($filename)); fclose($handle); $encoded = chunk_split(base64_encode($contents)); /*This is how I have done*/
Top answer
1 of 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 of 2
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>";
🌐
APIVoid
apivoid.com › tools › pdf-to-base64
PDF to Base64 Converter - PDF to Base64 Encoder | APIVoid
Convert PDF files to Base64 data URIs with this free online tool. Upload a PDF and instantly get a Base64 string you can embed in HTML (for example in <iframe> or <embed>). Ideal for developers and teams who need a quick way to inline or transfer PDFs without hosting the file separately.
🌐
Convertchief
convertchief.com › tools › pdf-to-base64
PDF to Base64 Encoder - Convert PDF to Base64 Data
Convert PDF documents to Base64 encoded strings for embedding or data transfer.
🌐
Stack Overflow
stackoverflow.com › questions › 27132216 › base64-pdf-files-php
Base64 PDF Files PHP - Stack Overflow
Is it ok if we convert PDF to base64 and then store it in database? Although I know that it takes more space but space is not an issue for me. What are the pros and cons of the latter? Any suggest...