Output standard Base64 encoded stream to PDF (PHP)

I use the Royal Mail Shipping API to create PDF printable labels, my PHP SoapClient returns the valid answer shown below (only the original answer is shown, as the whole answer is huge).

%PDF-1.7 %äãÏÒ 4 0 obj <> stream xœ endstream endobj 3 0 obj 8 endobj 9 0 obj <> stream xÚí]bì*¤Höýoübƒ¤Æ-q²É>ø%Íë‚ÔFÒ<1ÆoÌúÝú¯ý?1Æ%Èa9Ò4QÌ!}üŠ ÆãS€ZÿŸ2Mô¨H}üßÇcŒ˜Z´½\¡´üý'y©1Æø$¨RÓd°úø'ÆÄŒ1Ægð´ ¨Š'ª°Z¾MCF1Æ}¥/¨{d˜ZQ•†Þ7Æ_P¢õ' kjŒ1.J¦ê"ÕÑŽ©,ž‹1ÆãNÿÅIü{}L%üÄcŒÑS Þª€êÁI"ÀÅÃcŒcHÚsïuP5Ð4Æ .ê2¤mbŒ1vU¼vè:ž>Æ<´¾1ÆØTŠûfÓ¢œÆcTŒ³wGF1Æ 

Can anyone suggest a better way to “convert” this standard Base64 label to correctly so that I can physically load it into the browser. My code below downloads a PDF file, but when I try to open this, the file size is always 57kb and I return the following message to Acrobat Reader

 "There was an error opening this document. The file is damaged and could not be repaired." 

My code is as follows:

 $rm = new RoyalMailLabelRequest(); // provide shipment number, order tracking id, output format (eg PDF/PNG) $response = $rm->PrintLabel('TTT000358756GB', '276831601444829801', 'PDF'); header('Content-type: application/pdf'); header('Content-Disposition: attachment; filename="doc-'.$document.'.pdf"'); $data = base64_decode($response); file_put_contents('pdf/label.pdf', $data); 

UPDATE

When I try to repeat the decoded answer, I get the following ... not sure what is happening with this ... odd.

 $response = $rm->PrintLabel('TTT000358756GB', '276831601444829801', 'PDF'); $data = base64_decode($response); echo $data;exit; 

Reply to Echo'd $ after base64_decoded

 <1uï(n?Ëzx-‡}üX¥µêÿVx7œ¡×¬¶·š› 

I also added the full base64_encode answer, which my code returns here on pastebin if it helps anyone http://pastebin.com/JEtmRURK

0
source share
1 answer

Sorted - it was a very long day.

I did not need to decode the answer at the end, so the following will work (I hope this helps someone who does the API integration)

 function PrintLabelRequest($shipmentNumber, $transactionId) { $rm = new RoyalMailLabelRequest(); // function from library returns a response using SOAP $response = $rm->PrintLabel($shipmentNumber, $transactionId); // name the file & saved this label as a PDF in the following folder $filename = 'printedlabel-' . $shipmentNumber; file_put_contents(dirname(__FILE__) . '/labels/'. $filename .'.pdf', $response); } 
+1
source

Source: https://habr.com/ru/post/983654/


All Articles