I'm trying to create a .pdf file with a base64 string from an image, and I can create it correctly, but when I try to open the file, the program sends a message that tells the file is corrupt or something like that..
I got this code:
define('UPLOAD_DIR', '../image/');
$img = $_POST['image'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);
$uniqueNumber = uniqid();
$namefile = $uniqueNumber.'.png';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
$namefile = $uniqueNumber.'.pdf';
$file = UPLOAD_DIR . $namefile;
$success = file_put_contents($file, $data);
I can open the .png file correctly so, i think it's not problem from the base64 decoded string. Thank you all!
EDIT:
I'm trying this code and getting the same issue.
$data = base64_decode ($img);
//Write data back to pdf file
$pdf = fopen ('test.pdf','w');
fwrite ($pdf,$data);
//close output file
fclose ($pdf);
echo 'Done';
Is that becouse i'm saving an image with .pdf ? I think no, because if i'm doing fopen with .pdf should be with that format.
EDIT 2:
FOUND A SOLUTION.
http://www.fpdf.org/en/script/script45.php
I followed these steps and i can get that, thank you all!
Check out DOMPDF: https://github.com/dompdf/dompdf
You can definitely use DOMPDF to create a PDF with an image tag whose source is that Base64 string. and render that to PDF.