Support Portal

for ProcessRobot and WinAutomation

Start a new topic

Save data in variable as Binary File

I'm pulling my hair out trying to work on a solution to a problem I never imagined would exist!  Anyone with an idea of what I should be doing please help!


I have downloaded a PDF document encoded in Base64 - which I have stored in a variable.


To create a PDF I need to save the data I have in the variable as a 'binary file', has anyone came across this problem before and has a solution that they could share?



ADMIN

Hello David,


My recommendation would be to implement something as the following:

Provided that you have already decoded the BASE64 text into plain-text then you could set this variable to the Clipboard, run MS Word, Paste the text into Word document. Afterwards you can Export this file as a PDF directly from MS Word.

Alternatively, you could write this to a .txt file, save it and rename the file with overwriting the file extension to .bin. This way, you should have the file in the required format.


Hope this helps.

Hi Michalis,


Thanks for the fast reply... if it clarifies my question any further please see the data below that I have retrieved into a variable.  It is a PDF of a shipping label.


I have tried saving it as a text file and renaming the extension .pdf but had no joy.  Ideally I'd like to avoid MS Word.  I've been looking for solutions that would save the file from inside my JavaScript function - I've seen code that I think might work on the internet but I have no idea how to implement it!


  

%PDF-1.4
%⣏ӊ30 0 obj
<</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix[1 0 0 1 0 0]/FormType 1/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/Font<</ArialMT 6 0 R>>>>/BBox[0 0 60.6 8.1]/Length 97>>stream
x읍뎀@�D�唴k�ᨑ喛܄퀈�[鼷f溧臀&/ퟸڊꊆ8₄�ܯ�9Δ
rᵱԈꛋ܈즐婜궆ы(��Ze�͊endstream
endobj
6 0 obj
<</Subtype/TrueType/FirstChar 32/Type/Font/BaseFont/ArialMT/FontDescriptor 7 0 R/Encoding/WinAnsiEncoding/LastChar 255/Widths[277 277 354 556 556 889 666 190 333 333 389 583 277 333 277 277 556 556 556 556 556 556 556 556 556 556 277 277 583 583 583 556 1015 666 666 722 722 666 610 777 722 277 500 666 556 833 722 777 666 777 722 666 610 722 666 943 666 666 610 277 277 277 469 556 333 556 556 500 556 556 277 556 556 222 222 500 222 833 556 556 556 556 333 500 277 556 500 722 500 500 500 333 259 333 583 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 277 333 556 556 556 556 259 556 333 736 370 556 583 333 736 552 399 548 333 333 333 576 537 333 333 333 365 556 833 833 833 610 666 666 666 666 666 666 1000 722 666 666 666 666 277 277 277 277 722 722 777 777 777 777 777 583 777 722 722 722 722 666 666 610 556 556 556 556 556 556 889 500 556 556 556 556 277 277 277 277 556 556 556 556 556 556 556 548 610 556 556 556 556 500 556 500]>>
endobj
39 0 obj
<</Subtype/Form/Filter/FlateDecode/Type/XObject/Matrix[1 0 0 1 0 0]/FormType 1/Resources<</ProcSet[/PDF/Text/ImageB/ImageC/ImageI]/Font<</ArialMT 6 0 R>>>>/BBox[0 0 160.7 8.5]/Length 103>>stream
x쥍ᎂ0� 

  



In order to achieve that, the simplest way is to use the "Run Powershell Script" Action, inserting the following command: [io.file]::WriteAllBytes('c:\output.pdf',$DecodedPdfData) Hope it helps.

Thank you Lean,


This looks like a very nice solution but I'm getting an 'unable to write bytes' error.


I've attached a simple WAJ of the part of the process i'm stuck at, as there's probably something obvious that I'm doing wrong.


Dave

waj

Are you sure the variable %Base64Raw% contains the correct base64 string?

Could you send us base64 as a file?

Same observation with Lukasz, normally a properly decoded byte array when viewed in a typical text viewer should include an end of file marker: %%EOF


image


From your screenshot, it seems that the data is partway decoded.

I will be dropping out of the conversation now. BTW, that approach I have suggested is the one we have implemented using UiPath. An API is returning a base64 response to a particular request that will let the client decode it and invoke a file download on the browser. Since we are directly talking to the api, converting base64 to byte array then passing it to powershell to write on file is the way to go. Javascript will have troubles writing directly to disk, generally, it is not possible. 

Hope you resolve this.

%Base64Raw% holds a Base64 string that is double encoded.... that may be why you see the file incorrectly?  My JS decodes it 2 times then stores it in %DecodedPDFData%


I've just generated a new Base64 string and tested it using an online tool.


The txt file attached has the original Base64 string that needs to be decoded twice to get the data that I want to write into the PDF.


This works for me using the :


 I visit this site:  https://www.motobit.com/util/base64-decoder-encoder.asp

.. and fill the data (from my attached txt file) into the window... 

then select - 'decode the dataset from Base64 string' and 'Export to Binary File'


The first time I name the file to be exported as PDF.bin

The second time I set the source as the .bin file that just downloaded and import it.  I name the new file labelpdf.pdf and it works fine.


txt
(53.2 KB)

David,

in the attachment I am sending a version using VBScript for decoding :)

Created quickly but I hope it helps :)

prp

if there were problems with opening the * .prp file I attach the VBScript script code.

image


VBScript:

  

Option Explicit

Const foForReading          = 1 ' Open base 64 code file for reading
Const foAsASCII             = 0 ' Open base 64 code file as ASCII file
Const adSaveCreateOverWrite = 2 ' Mode for ADODB.Stream
Const adTypeBinary          = 1 ' Binary file is encoded

' Variables for decoding
Dim objXML
Dim objDocElem

' Variable for write binary picture
Dim objStream

' Create XML Document object and root node
' that will contain the data
Set objXML = CreateObject("Msxml2.DOMDocument")
Set objDocElem = objXML.createElement("b64")
objDocElem.DataType = "bin.base64"

' Set text value
objDocElem.text = "%FileContents%"

' Open data stream to picture file
Set objStream = CreateObject("ADODB.Stream")
objStream.Type = adTypeBinary
objStream.Open()

' Get binary value and write to file
objStream.Write objDocElem.NodeTypedValue

objStream.SaveToFile "%outputFile%", adSaveCreateOverWrite
objStream.Close

' Clean all

Set objXML = Nothing
Set objDocElem = Nothing
Set objStream = Nothing

  


1 person likes this

Thanks for your efforts Lukasz,


Forgive me for my ignorance but how should I correctly use a .PRP file?


I've opened it in notepad and copy/pasted the contents into a 'Run VB Script' function but I'm getting errors saying variables don't exist.

beat me to it!! thanks

hmm...  I think I've done what I'm meant to but the VBscript is throwing a parsing error.


I think this is going to drive me mad before the weekend!


I've attached my WAJ to show where I'm at.

image


waj

Hi David,

Oh, I have checked the site where you are making the conversion. Sad to say, it has one peculiar setting that will make normal decoding of base64 string fail. As you see most decoder will assume ANSI, ASCII or even UTF8 codepage to be applied. Unfortunately, the site you have done the encoding default's to using iso-8859-1

image


If you don't mind scrapping the JS part of your script, give the Powershell command below a try:

   

[io.file]::WriteAllBytes('DESTINATION',[System.Text.Encoding]::GetEncoding('iso-8859-1').GetBytes([System.Text.Encoding]::GetEncoding('iso-8859-1').GetString([System.Convert]::FromBase64String([System.Text.Encoding]::ASCII.GetString([System.Convert]::FromBase64String($decoded))))))

 
Just modify 'DESTINATION' and directly assign to '$decoded' your base64 string.  

Hope this helps.

Regards,
Lean

Login or Signup to post a comment