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

时间:2013-06-10 10:48:34

标签: php pdf header base64 encode

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

标题函数中有Content-type:application/pdf之类的内容吗?

this is the base64 encoded string

2 个答案:

答案 0 :(得分:53)

试试这段代码

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

答案 1 :(得分:20)

有了这个,您可以创建并打开文件......但也许这不是问题的答案

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

答案是用标题回显内容:

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