PHP get pdf file from base64 encoded data string

Jenson M John picture Jenson M John · Jun 10, 2013 · Viewed 84.5k times · Source

I have a base64-encoded string containing a pdf. How to create a .pdf file from this encoded string using php?

Something like Content-type:application/pdf in header function?

this is the base64 encoded string

Answer

Samy Massoud picture Samy Massoud · Jun 10, 2013

Try this piece of code

$pdf_base64 = "base64pdf.txt";
//Get File content from txt file
$pdf_base64_handler = fopen($pdf_base64,'r');
$pdf_content = fread ($pdf_base64_handler,filesize($pdf_base64));
fclose ($pdf_base64_handler);
//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);
echo 'Done';