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

后端 未结 2 1254
抹茶落季
抹茶落季 2021-01-02 09:40

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

2条回答
  •  死守一世寂寞
    2021-01-02 10:02

    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;
    

提交回复
热议问题