How do I generate a pdf-file from a binary file?

荒凉一梦 提交于 2019-11-30 13:19:06

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 file in the $binary variable. 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:

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:

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="my.pdf"');
echo $binary;

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!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!