45

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

Content-type:application/pdf头函数中的东西?

这是base64编码的字符串

4

2 回答 2

73

试试这段代码

$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';
于 2013-06-10T11:28:32.807 回答
29

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

$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;
于 2016-05-02T16:32:11.413 回答