title: PHP : Saving base64 encoded PDF file
body: <p>I am trying to save a pdf file from a response. Here is the
   <a href="http://docs.shipstation.apiary.io/#reference/orders/create-label-for-order" rel="nofollow noreferrer">API documentatin</a></p>
  
  <p><code>Creates a shipping label for a given order. The labelData field returned in the response is a base64 encoded PDF value. Simply decode and save the output as a PDF file to retrieve a printable label</code>.</p>
  
  <p>Here is my code for saving the pdf file.</p>
  
  <pre><code>        $response = json_decode(curl_exec($ch));
          $data = base64_decode($response-&gt;labelData);
          header('Content-Type: application/pdf');
          //echo $data;
          $path = 'E:/newly_created_file'.time().'.pdf';
          file_put_contents($path,$data);
  </code></pre>
  
  <p>When I <code>echo $data</code> in the browser, it shows the response like</p>
  
  <p><code>%PDF-1.4 %���� 2 0 obj &lt;&gt;stream x�+�r �26S0�4SI�r � �*T0T0BC]#=]ccC=#��\��4C�|�@.GF� endstream endobj 4 0 obj &lt;&gt;&gt;&gt;/Contents 2 0 R/Parent</code>  </p>
  
  <p>and when I save the data, it generates an empty pdf file which fails to open. </p>
  
  <p>What is wrong here? </p>
  
  <p>UPDATE : Respose has correct data as show in attached image</p>
  
  <p><a href="https://i.stack.imgur.com/aXoRf.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/aXoRf.png" alt="enter image description here"></a></p>