PHP base64 encode a pdf file - php

I am using an API where I can send a document to something like dropbox. According to the documentation, the file which is sent needs to be BASE64 encoded data.
As such, I am trying something like this
$b64Doc = chunk_split(base64_encode($this->pdfdoc));
Where $this->pdfdoc is the path to my PDF document.
At the moment, the file is being sent over but it seems invalid (displays nothing).
Am I correctly converting my PDF to BASE64 encoded data?
Thanks

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

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.

Convert base64 to pdf and save to server path.
// Real date format (xxx-xx-xx)
$toDay = date("Y-m-d");
// we give the file a random name
$name = "archive_".$toDay."_XXXXX_.pdf";
// a route is created, (it must already be created in its repository(pdf)).
$rute = "pdf/".$name;
// decode base64
$pdf_b64 = base64_decode($base_64);
// you record the file in existing folder
if(file_put_contents($rute, $pdf_b64)){
//just to force download by the browser
header("Content-type: application/pdf");
//print base64 decoded
echo $pdf_b64;
}

Related

Return an encoded B64 pdf file

I have a String representing a pdf file encoded in base64.
I can decode the string like this:
function get_file_from_b64($b64_file) {
//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);
return $pdf;
}
I would like to create a page that download the file but I don't want to save the file on the filesystem read it in a php page and then send it back to the browser.
There is a way to send back the file directly without saving it on the filesystem?
For example I can send a file back to the browser like this:
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename=downloaded.pdf");
readfile("test/file.pdf");
This implies that the decoded file is saved on test/file.pdf.
The readfile command simply echos the content of the file. Instead of doing that echo the variable with your contexts in it.
You will base to base64 decode the PDF if it has been base64 encoded
For example
echo base64_decode($pdf_content);

Laravel decode base64 image and hashName and store public folder

How I can store decoded image to laravel public image path and hash image name ?
I tried like this, but didnt work and doesn't get any error message;
if (!empty($request->image)) {
$file = base64_decode($request->image)->hashName();
base64_decode($request->image)->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
This code works fine but I use API and via curl sending image then needed base64 decoding:
if ($request->hasFile('image')) {
$file = $request->file('image')->hashName();
$request->image->store('user-uploads/avatar');
asset('user-uploads/avatar/' . $file);
}
What you suggest save image and send image url to api and downloads image too path or send base64 image encode and in api decode, like I'm trying at the moment?
You can't call functions on the output of base64_decode, as it returns as string.
I would suggest you post the name of the file along with the base 64 encoded content, which can then be saved using laravel's storage helpers:
Storage::disk('local')->put('image.png', base64_decode($request->image))

Laravel File Storage: How to store (decoded) base64 image?

How to store base64 image using the Laravel's filesytem (File Storage) methods?
For example, I can decode base64 image like this:
base64_decode($encoded_image);
but all of the Laravel's methods for storing files can accept either a Illuminate\Http\File or Illuminate\Http\UploadedFile instance.
So I guess I'd have to convert base64 image (or decoded base64 image) to Illuminate\Http\File or Illuminate\Http\UploadedFile, but how?
Just use put to store the encoded contents:
Storage::put('file.jpg', $encoded_image);
All it's doing is wrapping file_put_contents.
Then to read it back out:
$data = base64_decode(Storage::get('file.jpg'));
Which, you guess it, is wrapping file_get_contents.
You can upload your base64 Image using laravel File Storage like this
$base64_image = $request->input('base64_image'); // your base64 encoded
#list($type, $file_data) = explode(';', $base64_image);
#list(, $file_data) = explode(',', $file_data);
$imageName = str_random(10).'.'.'png';
Storage::disk('local')->put($imageName, base64_decode($file_data));
Hope it will help you

Create Base64 from uploaded file without saving locally php

I need to send a file in base 64 format through a SOAP service. I cannot save the file locally. Is there a way to convert an uploaded file and send it through in one instance, without saving it?
I initially thought it was as easy as:
$base64file = base64_encode($_FILES["cv"]["tmp_name"]);
But that doesn't seem to be working great.
The problem with your code is that you are encoding the filename, not the file content.
Use this to open the file and convert it to base64:
$base64file = base64_encode(file_get_contents($_FILES["cv"]["tmp_name"]));
Then you can send it back to the client. Just double check that the Soap server does not double base64 encode the string.

How do I generate a pdf-file from a binary file?

How do I generate a pdf-file from a binary file retrieved from database in php5? It comes base64 encoded, and I just decoded it, but don't know what to do next...
The binary data is simply the actual file, or rather the important contents of that file, just without file name.
$base64 = /* some base64 encoded data fetched from somewhere */;
$binary = base64_decode($base64);
And there you have the file data/contents of the file in the $binary variable. From here, it depends on what you want to do. You can write the data to a file, and you get an "actual" PDF file:
file_put_contents('my.pdf', $binary);
You can spit the data out to the browser with an appropriate header, and the user will receive something that looks like a PDF file to him:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
I'm repeating your last sentence .:) I dont know what is the question! :). If you want to pus the file to a browser, you can set the headers and stream the decoded content. Or if you want the file as is, write on to file system and use it. Please be more clear on your question!
Thanks!!

Categories