PHP从base64编码的数据字符串中获取pdf文件

PHP从base64编码的数据字符串中获取pdf文件

问题描述:

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

我有一个包含pdf的base64编码字符串。 如何使用php从这个编码的字符串创建 .pdf code>文件? p>

标题函数中的 Content-type:application / pdf code>之类的东西? p>

这是base64编码的字符串 p> div>

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

With this you can create and open the file... but maybe this is not the answer to the question

$data = base64_decode($this->Get(self::Body));
file_put_contents('file.pdf',$data);

The answer is echo the content with a header :

$data = base64_decode($this->Get(self::Body));
header('Content-Type: application/pdf');
echo $data;