Convert Base64Binary to PDF using PHP - php

I tried lots of script which is available in internet to decode my Base64Binary to PDF they not working. Please help me.
Here is the sample encoded PDF

$data = base64_encode($contents);
file_put_contents('output.pdf', base64_decode($data));

Related

Convert FPDF output to base64 string

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 in my postman response.
Then I have tried to use this line of code to encode the pdf to base64 format. It worked but I am getting the broken pdf when I decode the base64 string.
$pdfFile = $pdf->Output("","S");
$base64String = chunk_split(base64_encode($pdfFile));
Am i doing anything wrong with this? Any small help would be appriciated.

Convert base64 PDF to base64 image, without saving it to any file

I do not know if it's a valid question to ask here but I'm tired of finding the solutions/libraries, so I had to ask for help from you guys.
Basically I'm generating a PDF using TCPDF and by using the parameter 'E' in the output method I get the the document as base64 mime (base64 String).
So as I'm building a API I don't want to save the PDF (or any file) on the server but just want to convert the output of base64 PDF to a base64 image (jpg) and throw the base64 output as json encoded.
So how do I convert the base64 PDF to base64 image (jpg).
You can use imagemagick to do that.
$imagick = new Imagick();
$imagick->readImageBlob($pdfBlob);
$imagick->setImageFormat("jpeg");
$imageBlob = $imagick->getImageBlob();
see http://php.net/manual/book.imagick.php

Coverting Hex to Image in PHP?

I am developing mobile app which talks with server via PHP Webservice. This is my first time using PHP. I managed to upload data in to database. Now i need to send an image to store it in ftp server. For that i converted image->hex and sent from my app.
Server Side
I got the hex code but not sure how to convert it in to an image and store in in ftp server. I am really struggling here. I googled it but couldn't find exact one.
Any help is much appreciated.
Convert the HEX string to binary:
$binary = pack("H*", $hex);
pack("H*", ...) is equivalent to hex2bin, which is available since PHP 5.4.
Write it to disk:
file_put_contents("file.png", $binary);
Suppose you have received a hex string in a page where you want to convert this hex to real image. Please check this code snippet will help you or not.
<?php
$hexpic=".......................
.....................";
# convert the hex string to binary
$data = pack("H" . strlen($hexpic), $hexpic);
#write the binary string into an image file
file_put_contents("sample.png", $data);
?>

How to save a base64 decoded image in the filesystem using php?

I am getting a base64 encoded JPEG string via a POST request to my web service.
I want to decode it and save it in the filesystem.
How can I achieve this using PHP 5.3.
I am able to successfully decode the data using the base64_decode function.
How can I save this decoded string as a JPEG image in the server?
Thanks in advance.
If you are sure the image will always be jpg then you can simply use: file_put_contents();
<?php
$decoded=base64_decode($encodedString);
file_put_contents('newImage.JPG',$decoded);
//leave it to you to randomize the filename.
?>
Replacing the blank spaces with + signs is required if the data is derived from canvas.toDataURL() function.
$encodedString = str_replace(' ','+',$encodedString);
See this question
It helped a lot in my case.

encoding from .txt file to string using php

I'm doing this project where I receive a .txt file from a MATLAB program in a PHP server. I would like to extract the data in the .txt file and encode it to a string to be sent by the server to an android. Any ideas on how to do that?
Screw the string.
readfile()
use below code for your task.
$file = file_get_contents('test.txt', true);
$parsed_str = json_decode($file);
I think it may be helpful to you..
Thanks.

Categories