Search code examples
phpjqueryimagebase64fpdf

How to send base64 png image to PHP


I have a base64 png image.
I'd like to use it in FPDF

I create it like this:

var dataURI = canvas.toDataURL("image/png");

And send it to FPDF like this:

$('#send').click(function(e){
    e.preventDefault(); 
    var uri = "create_pdf.php?imgURI="+ dataURI;
    window.open ( uri, "Temp wind" );
});

inside the create_pdf.php I have:

require('fpdf.php');

$img = $_GET['imgURI'];
$img = str_replace('data:image/png;base64,', '', $img);
$img = str_replace(' ', '+', $img);
$data = base64_decode($img);

$pdf = new FPDF();
$pdf->AddPage();
$pdf->Image( $data, 0, 0, 200 );

$pdf->Output( "myimage.pdf", 'D' );

Instead of the usual prompt to save PDF I get this error:

Image file has no extension and no type was specified: �PNG IHDRSJ����IDATx......

How to properly send that base64 encoded image?


Solution

  • Seems like pfdf takes saved iamge as parameter, not base64 Try the answer here