如何从二进制文件生成pdf文件? [英] How do I generate a pdf-file from a binary file?

查看:652
本文介绍了如何从二进制文件生成pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从php5中从数据库检索的二进制文件中生成pdf文件?它采用base64编码,而我只是对其进行解码,但不知道下一步该怎么做...

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

推荐答案

二进制数据只是实际文件,或者是该文件的重要内容,只是没有文件名.

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);

$binary变量中就有文件数据/文件内容.从这里开始,这取决于您想做什么.您可以将数据写入文件,然后得到一个实际的" PDF文件:

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);

您可以使用适当的标题将数据吐出到浏览器中,然后用户会收到对他来说像PDF文件的东西:

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;

这篇关于如何从二进制文件生成pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆