Sidebar

PDF to base64

0 votes
686 views
asked Feb 6, 2018 by john-p-3531 (210 points)
I have been trying to use the following code (from an answer to a question someone else asked here) to get a PDF file converted into base64.

 

var pdf = new File('c:\\hl7\\Sample Report.pdf');

var bytes = qie.readFile(pdf);

//var s = new java.lang.String(bytes);

var encoder = new org.apache.commons.codec.binary.Base64(0, null, false);

var base64 = new java.lang.String(encoder.encode(bytes));

 

I get an error message "File is not defined"

Is there a syntax error in the above code or was the code provided in response to the previous question incorrect ?

Secondarily, how is this passed into a parameter map or similar so that i can embed the base64 into the message template I have created as a system variable.

1 Answer

0 votes
 
Best answer

The easiest way to do this would be to use the QIE functions.

// The qie.readFile() can accept a path to the file, so just passing a string is the easiest way to read a file
var bytes = qie.readFile('c:\\hl7\\Sample Report.pdf');

// qie.base64EncodeBytes() will encode the bytes that were returned from the qie.readFile()
var base64 = qie.base64EncodeBytes(bytes);

// validate the output with a debug statement
qie.debug(base64);

This will read the file and base64 encode the contents.

 

Once you have the contents you can store them in a parameterMap or messageCache.  Then your template can reference the messageCache or parameterMap.

// MessageCache Example
// the node tag for this will be '{mc:encodedFile}'
messageCache.setValue('encodedFile', base64);

 

// ParameterMap Example
var parameterMap = qie.newParameterMap();
// the node tag for this will be '{p:encodedFile}'
parameterMap.put('encodedFile', base64);
 

// NOTE: make sure to pass the parameter map into your evaluate template call
var output = qie.evaluateTemplate('template', parametersMap, 'HL7', false, '');

 

answered Feb 6, 2018 by ben-s-7515 (12,640 points)
edited Feb 6, 2018 by ben-s-7515
...