Jfish Posted September 26, 2014 Share Posted September 26, 2014 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 Link to comment Share on other sites More sharing options...
Danp2 Posted September 26, 2014 Share Posted September 26, 2014 Which API? Can you show us an example of the raw data coming back in responseText? Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
Jfish Posted September 26, 2014 Author Share Posted September 26, 2014 The API is SigConfirmCertifyV4.0. Be careful what you wish for on the return data ... here it is ... Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Jfish Posted September 27, 2014 Author Share Posted September 27, 2014 Can't edit that post for some reason ... anyway - I made some changes to strip out the xml tags and data between the tags. Still no good... Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Danp2 Posted September 27, 2014 Share Posted September 27, 2014 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. Latest Webdriver UDF Release Webdriver Wiki FAQs Link to comment Share on other sites More sharing options...
UEZ Posted September 27, 2014 Share Posted September 27, 2014 (edited) 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 EndFuncBr,UEZ Edited September 27, 2014 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Jfish Posted September 27, 2014 Author Share Posted September 27, 2014 @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 Link to comment Share on other sites More sharing options...
UEZ Posted September 27, 2014 Share Posted September 27, 2014 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Jfish Posted September 27, 2014 Author Share Posted September 27, 2014 @UEZ - Thanks again - this time some progress to report. It now opens as a PDF (awesome!) but it says "insufficient data for an image". Any thoughts on that? JFish Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
Jfish Posted September 27, 2014 Author Share Posted September 27, 2014 (edited) 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 September 27, 2014 by Jfish Build your own poker game with AutoIt: pokerlogic.au3 | Learn To Program Using FREE Tools with AutoIt Link to comment Share on other sites More sharing options...
UEZ Posted September 27, 2014 Share Posted September 27, 2014 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Jfish Posted September 27, 2014 Author Share Posted September 27, 2014 @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 Link to comment Share on other sites More sharing options...
Solution UEZ Posted September 27, 2014 Solution Share Posted September 27, 2014 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!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
Jfish Posted September 27, 2014 Author Share Posted September 27, 2014 @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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now