How can I convert a file into its encoded Base64?

I want to convert a PDF file into its Base64 version. Everything I1ve found in my internet research involves promisses, async functions and DOM manipulation, which is not supported by Studio Pro (as far as I know). Usually you need to create an HTML page with an input of type file and an id. Then you use getElementById() to get the file, create an async function that converts the file into Base64.

But I’m working with local files and I need to convert them without uploading them anywhere. I’ve tried using btoa() but Studio Pro throws and error sayong Variable 'btoa' is not defined, which is weird because it’s not supposed to be a variable.

Any ideas?

Hello!

Is it possible to use command promt (using command certutil decode or encode and other parameters)?

Best Regards

1 Like

I’ve tried the following command, but it says the syntax is incorrect.

[convert]::ToBase64String((Get-Content -path "fileName" -Encoding byte))

Apparently, it has to be run using PowerShell. I want it to run in the background, so I tried running the command in the prompt by typing PowerShell, then ENTER, then trying the command above again. It works just fine when I run it manually, but not when I use the Command Prompt activity.

Hi @cris-dsc , try using “execute js code” activity:

  • pdf_content = Buffer.from(pdf_content).toString(‘base64’)
    remeber to check “Is Global” in pdf_content assign activity

4 Likes

Thank you. Your suggestion works, but I’ve just realized that it’s not what I need.

The file cannot be decoded back into a PDF file, probably because I’m using the Read Text activity. So it encodes the text into base64. What I actually need is to encode the PDF file into base64.

2 Likes

Hello!

There are 2 commands.

certutil [options] -encode infile outfile

certutil [options] -decode infile outfile

I think it is possible to use command promt activity in Studio Pro.
(without using PowerShell).

Do you use these commands?

Best Regards

1 Like

Hello @cris-dsc!

Just like @Mikhail suggested, the best option here is to use the “certutil” command via the “Command Prompt” activity from the OS Automation → System section.

You can set up 2 variables called pdfPath (path of the file you want to convert to base64) and outputPath (file where the resulting base64 code will be saved), and then use the following command inside the Command Prompt activity (with “Calculate a value” selected):

'certutil -encode "' + pdfPath + '" "' + outputPath + '"'

You can then use the “Read text file” activity from the OS Automation → Files & Folders section to read the outputPath file, and save its contets to a variable.

Here’s an example:

Hope this helps! If it does, please help us by marking my reply as a solution. Thank you!

3 Likes

Thank you! It worked! I was having trouble with getting the quotes right. @Mikhail thank you too for the suggestion.

2 Likes

Hello @cris-dsc!

You are welcome!

I’m glad, that this idea was useful for you!

2 Likes