php PHP从base64编码的数据字符串中获取pdf文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17022101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP get pdf file from base64 encoded data string
提问by Jenson M John
I have a base64-encoded string containing a pdf. How to create a .pdf
file from this encoded string using php?
我有一个包含 pdf 的 base64 编码字符串。如何.pdf
使用 php 从这个编码的字符串创建一个文件?
Something like Content-type:application/pdf
in header function?
类似于Content-type:application/pdf
标题功能的东西?
回答by Samy Massoud
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';
回答by esdebon
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;