Jump to content
Sign in to follow this  
Jfish

Base64 decode image returned from USPS API

Recommended Posts

Jfish

Hello all.  I managed to create an API call to the U.S.P.S. web tools API that lets you create shipping labels etc.  The return image can be one of several formats including the one I chose - PDF.  I believe the return value is a Base64 encoded string.  I am doing the following to try and create the PDF file:

$result = $winHttpReq.responseText
$output=FileOpen(@ScriptDir&"\label.pdf",2)
FileWrite($output,StringToBinary(_Base64Decode($result)))
FileClose($output)

I am using the _Base64Decode found here: The PDF file won't open as a PDF so I am pretty sure I am doing it wrong.  Any guidance would be very much appreciated. 

JFish


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
Danp2

According to the docs, the value of SignatureConfirmationLabel is supposed to be base64Binary. I was able to decode the data in Notepad++, but the resulting file was corrupt. However, the typical PDF header information was there.

Haven't tried with Autoit.

Share this post


Link to post
Share on other sites
UEZ

Try this:

 

#include <String.au3>
Global $sData = _StringBetween(FileRead("SigConfirmCertifyV4.0.txt"), "<SignatureConfirmationLabel>", "</SignatureConfirmationLabel>")[0]
$bB64 = _Base64Decode($sData, 1)
If @error Then Exit ConsoleWrite(@error & @CRLF)

$hFile = FileOpen(@ScriptDir & "\Test.pdf", 18)
FileWrite($hFile, Binary($bB64))
FileClose($hFile)

Func _Base64Decode($sData, $iConversation = 4)
   Local $oXml = ObjCreate("Msxml2.DOMDocument")
   If Not IsObj($oXml) Then
      SetError(1, 1, 0)
   EndIf
   Local $oElement = $oXml.createElement("b64")
   If Not IsObj($oElement) Then
      SetError(2, 2, 0)
   EndIf
   $oElement.dataType = "bin.base64"
   $oElement.Text = $sData
   Local $sReturn = BinaryToString($oElement.nodeTypedValue, $iConversation)
   If StringLen($sReturn) = 0 Then
      SetError(3, 3, 0)
   EndIf
   Return $sReturn
EndFunc

Br,

UEZ

Edited by UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Jfish

@UEZ - thank you for the response but that is still not working - it could be me (probable).  I did manage to decode it in Notepad but my issue is that I am not sure if tags in the response should be trimmed.  I tried it both ways - neither will open as a pdf although the trimmed version at least decodes and purports to be a PDF with a bunch of FDF data.  Here is the complete response in case you can shed any light on what I am doing wrong:

<?xml version="1.0" encoding="UTF-8"

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
UEZ

I updated the code from post#6. Try again.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Jfish

Update: The b64 encoded message works in PHP - I was able to decode it and see the full PDF using the same string between the label tags.  The PHP code I used was:

<?php
$myfile = fopen("phpfile.txt", "r") or die("Unable to open file!");
$b64= fread($myfile,filesize("phpfile.txt"));
fclose($myfile);
$contents = base64_decode($b64);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="label.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . strlen($contents));
echo $contents;
?>

I got the above snippet from a google search on PHP and USPS.  It looks like the headers that follow the decoding are required to get the image to properly render.  What is the equivalent of that for the Msxml2.DOMDocument used to decode in AutoIt?

EDIT: my feeble attempt:

Func _Base64Decode($sData)
   Local $oXml = ObjCreate("Msxml2.DOMDocument")
   If Not IsObj($oXml) Then
      SetError(1, 1, 0)
   EndIf
   Local $oElement = $oXml.createElement("b64")
   If Not IsObj($oElement) Then
      SetError(2, 2, 0)
   EndIf
   $oElement.dataType = "bin.base64"
   $oElement.Text = $sData
   Local $sReturn = BinaryToString($oElement.nodeTypedValue, 4)
   If StringLen($sReturn) = 0 Then
      SetError(3, 3, 0)
  EndIf
  $oXml.setRequestHeader('Content-type: application/pdf');
  $oXml.setRequestHeader('Content-Disposition: inline; filename="label.pdf"');
  $oXml.setRequestHeader('Content-Transfer-Encoding: binary');
  $oXml.setRequestHeader('Content-Length: ' & stringlen($sReturn));
   Return $sReturn
EndFunc

Would love to be able to decode the same in AutoIt.  Any suggestions to get the PDF to render properly would be much appreciated.

Edited by Jfish

Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
UEZ

Try the code from post#6 again - it should work now.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Jfish

@UEZ - same result as before "insufficient data ..."  Have you been able to successfully decode the sample to a PDF and open it?  Also, do you think the headers are required?  They are in PHP.


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites
UEZ

Yes, it worked for me. I decoded the b64 string from your post#7 using the code from post#6 and I was able to open the PDF properly.

Did you realize that I modified the function _Base64Decode? The issue for me was the BinaryToString in that function which converted to UT8 format which seems to corrupt the PDF.

Br,

UEZ


Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Share this post


Link to post
Share on other sites
Jfish

@UEZ ... THANK YOU !!!! That worked.  I am not sure what I did wrong a moment ago but I double checked it all - and copied and pasted you code.  Thank you very much.  That is extremely useful.

JFISH


Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×