Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 330
nli Avatar asked Oct 22 '12 07:10

nli


People also ask

Are PDF files binary files?

General conventionsPDF files are either 8-bit binary files or 7-bit ASCII text files (using ASCII-85 encoding). Every line in a PDF can contain up to 255 characters.

How do I extract binary data from a file?

The binary data of a local file selected by the user can be retrieved using the readAsBinaryString() method of a FileReader object.

Is PDF binary or text?

A PDF file is organized using ASCII characters, except for certain elements that may have binary content. The file starts with a header containing a magic number (as a readable string) and the version of the format, for example %PDF-1.7 .


1 Answers

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;
like image 193
deceze Avatar answered Nov 14 '22 02:11

deceze