phphttppdfheaderresponse

PHP binary data into pdf


I've written an api to an external service which is giving me a response with binary data. Now I would like to create an PDF to view the data correctly.

The Response headers of the API:

0 => string 'HTTP/1.1 200 OK' (length=15)
1 => string 'Cache-Control: private' (length=22)
2 => string 'Content-Length: 275454' (length=22)
3 => string 'Content-Type: application/pdf' (length=29)
4 => string 'Server: Microsoft-IIS/8.0' (length=25)
5 => string 'model: Invariant' (length=16)
6 => string 'Content-Disposition: attachment; filename=response_3a293a37-7a00-49ca-b557-fa218f834eb4.pdf' (length=91)
7 => string 'X-AspNet-Version: 4.0.30319' (length=27)
8 => string 'X-Powered-By: ASP.NET' (length=21)
9 => string 'Set-Cookie: ARRAffinity=3d8fa41aef05715eddd28bd82d74a76f49904e3d3e55e9dbdf656e2598101182;Path=/;HttpOnly;Domain=x2c-test.sprengnetter.at' (length=136)
10 => string 'Date: Wed, 06 Dec 2017 12:12:56 GMT' (length=35)
11 => string 'Connection: close' (length=17)

The name of the PDF is a random name.

Now Im trying to get a pdf out of my response:

$response_pdf = file_get_contents($remote_print, true, $context);
    if($response_pdf === FALSE){
        var_dump($http_response_header);
        echo "error pdf";
    }else{
        var_dump($http_response_header);

        $base64 = "/$response_pdf/";
        $binary = base64_decode($base64);
        file_put_contents('my.pdf',$binary);
        header('Content-type: application/pdf');
        header('Content-Disposition: attachment; filename="my.pdf"');
        return $binary;

In the Response is data like this:

%PDF-1.5 4 0 obj <>/XObject<>>>/Group <>>> endobj 5 0 obj <>stream x��Y[O#7�+~�Uc|�<���UUU[�ݪo�IJ�I�H�ԟ�

I dont have much experience with binary data and how to create a pdf out of binary data. So I would be happy for some help.


Solution

  • I could answer my question myself.

    First I created a tempfile with tempnam() and renamed it with rename() and give it the correct path .. then I just did file_put_contents() on the new generated file.