Ask a Question

How to encode a PDF file to base64 in groovy?

SOLVED
New2API
Frequent Contributor

How to encode a PDF file to base64 in groovy?

Hi, I am using below script to encode a PDF file. However, file gets corrupted when decoding or an API uploads it to a specific location.

Can you please point out if there is a better way or correct way to encode a PDF file using groovy script.

 

import net.sf.*
import groovy.io.FileType

 

//## Get Directories ##//
def projectPath = context.expand('${projectDir}')
def filePath = context.expand( '${#Project#TestDataPath}' ) 
def docName = context.expand( '${#TestCase#fileName}' )

 

//## get file ##//
log.info "get pdf from $dumpFilePath"
testFile = new File(filePath + "$docName").text

//Encode file to Base64 format
log.info "Encoding the file..."
encodedFile = "$testFile".getBytes("UTF-8").encodeBase64().toString()

log.info "Updating encoded file at testCase level property testFile"
testRunner.testCase.setPropertyValue("testFile", "$encodedFile")

 

 

I also tried - encodedFile = "$testFile".bytes.encodeBase64().toString(). But same result.

 

TIA.

4 REPLIES 4
richie
Community Hero

Hey @New2API

I think.....bit of a guess here as im not a groovy whizz (@ChrisAdams.....where are you? ), but "i think" it depends on what youre trying to encode.... i think your encode line is the groovy for encoding a string....not something wrapped up into a partially compressed file like a .pdf. that's a total guess on my part, but i think that "might" be the reason its not encoding as required.

I'll have a look (suggest you do the same) to see if you can 1. See if my guess is correct and 2. If it is, find out how to encode a file rather than a string.

Im probably wrong....i normally am whenever i try guessing.

Oh. Have you got the right import declarations? ( guessing you have otherwise your error would likely be obvious....)

Cheers,

Rich
if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
New2API
Frequent Contributor

@richie, that's what even I thought too. couldn't find any groovy native function to do this. but ended up using Java snippet.

 

Here is the updated code:

 

import java.io.File
import java.nio.file.Files
import java.util.Base64

 

//## Get Directories ##//
def dumpFilePath = context.expand( '${#Project#TestDataPath}' )
def docName = context.expand( '${#TestCase#fileName}' )


//## Encode file to Base64 format ##//
log.info "get pdf file from $dumpFilePath"
testFile = new File(dumpFilePath + "$docName")

 

log.info "Encoding the file..."
byte [] bytes = Files.readAllBytes(testFile.toPath())
String encodedFile = Base64.getEncoder().encodeToString(bytes)

 

log.info "Updating encoded file at testCase level property testFile"
testRunner.testCase.setPropertyValue("testFile", "$encodedFile")

Hi,

 

Does https://support.smartbear.com/readyapi/docs/testing/scripts/samples/encode.html help somehow ?

Especially, this line

encodedFile = "$testFile".getBytes("UTF-8").encodeBase64().toString()

seems missing 'bytes' call:

encodedFile = "$testFile".getBytes("UTF-8").bytes.encodeBase64().toString()

 

Regards,
  /Alex [Community Champion]
____
[Community Champions] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Champions]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Champion] signature is assigned on quarterly basis and is used with permission by SmartBear Software.
https://community.smartbear.com/t5/Community-Champions/About-the-Community-Champions-Program/gpm-p/252662
================================
New2API
Frequent Contributor

@AlexKaras , I tried this solution and it didn't work for a PDF file.

Below code worked from me.

 

byte [] bytes = Files.readAllBytes(TestFile.toPath());
String EncodedFile = Base64.getEncoder().encodeToString(bytes)

 

thanks,

Sunil

cancel
Showing results for 
Search instead for 
Did you mean: