php 如何从二进制文件生成 pdf 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13006505/
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
How do I generate a pdf-file from a binary file?
提问by nli
How do I generate a pdf-file from a binary file retrieved from database in php5? It comes base64 encoded, and I just decoded it, but don't know what to do next...
如何从从 php5 中的数据库检索的二进制文件生成 pdf 文件?它是 base64 编码的,我只是解码了它,但不知道下一步该怎么做...
回答by deceze
The binary data is simply the actual file, or rather the important contents of that file, just without file name.
二进制数据只是实际的文件,或者更确切地说是该文件的重要内容,只是没有文件名。
$base64 = /* some base64 encoded data fetched from somewhere */;
$binary = base64_decode($base64);
And there you have the file data/contents of the filein the $binaryvariable. From here, it depends on what you want to do. You can write the data to a file, and you get an "actual" PDF file:
有你有文件数据/文件内容中的$binary变量。从这里开始,这取决于您想要做什么。您可以将数据写入文件,并获得“实际”PDF 文件:
file_put_contents('my.pdf', $binary);
You can spit the data out to the browser with an appropriate header, and the user will receive something that looks like a PDF file to him:
您可以使用适当的标题将数据吐出到浏览器,用户将收到看起来像 PDF 文件的内容:
header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;
回答by Kris
I'm repeating your last sentence .:) I dont know what is the question! :). If you want to pus the file to a browser, you can set the headers and stream the decoded content. Or if you want the file as is, write on to file system and use it. Please be more clear on your question!
我在重复你的最后一句话。:) 我不知道问题是什么!:)。如果要将文件推送到浏览器,可以设置标头并流式传输解码的内容。或者,如果您想要文件原样,请写入文件系统并使用它。请把你的问题说清楚!
Thanks!!
谢谢!!

