Last active
August 26, 2017 21:57
-
-
Save mtblanton/e3f54114afa2b90cb144 to your computer and use it in GitHub Desktop.
Mirth transformer script to take a base-64 encoded rtf and convert it to a base-64 encoded PDF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// =============================== | |
// AUTHOR: Matt Blanton | |
// CREATE DATE: 1/7/2016 | |
// PURPOSE: Takes encoded rtf data from HL7 message, converts it to pdf, then places the encoded pdf data into the message | |
// SPECIAL NOTES: Open/LibreOffice must be installed and JODConverter + libs must be placed in Mirth Connect/custom-lib/ - I get best results using OpenOffice 4 for RTF-PDF | |
// =============================== | |
// Change History: | |
// | |
//================================== | |
var channelName = Packages.com.mirth.connect.server.controllers.ChannelController.getInstance().getDeployedChannelById(channelId).getName(); | |
//File name format is PID-Date/Time(in YYYYMMDDhhmmss)-OBX# | |
var fileNameString = msg['PID']['PID.3']['PID.3.1'].toString() + '-' + msg['MSH']['MSH.7']['MSH.7.1'].toString(); | |
var resultLocation = 'C:\\Program Files\\Mirth Connect\\appdata\\temp\\'; | |
for (var i=0; i<msg['OBX'].length(); i++) { | |
var msgRtfString = msg['OBX'][i]['OBX.5']['OBX.5.1'].toString(); | |
logger.info("Decoding OBX RTF string") | |
try { | |
var decodedRtfString = FileUtil.decode(msgRtfString); | |
} | |
catch(e) { | |
logger.error(channelName + ' failed to decode OBX RTF string because: ' + e); | |
throw(e); | |
} | |
var rtfLocation = resultLocation + fileNameString + '-' + i + '.rtf'; | |
logger.info("Writing RTF to file") | |
try { | |
FileUtil.write(rtfLocation, false, decodedRtfString); | |
} | |
catch(e) { | |
logger.error(channelName + ' failed to write RTF file because: ' + e); | |
throw(e); | |
} | |
var pdfLocation = resultLocation + fileNameString + '-' + i + '.pdf'; | |
//args to feed JODConverter | |
var argArray = new Array(); | |
argArray[0] = rtfLocation; | |
argArray[1] = pdfLocation; | |
logger.info("Converting RTF to PDF"); | |
try { | |
var jodConv = Packages.org.artofsolving.jodconverter.cli.Convert; | |
logger.debug(Packages.org.artofsolving.jodconverter.cli.Convert.main(argArray)); | |
} | |
catch(e) { | |
logger.error(channelName + ' failed to convert to PDF because: ' + e); | |
throw(e); | |
} | |
logger.info("Encoding PDF"); | |
try { | |
var encodedPdf = Packages.org.apache.commons.codec.binary.Base64.encodeBase64String(FileUtil.readBytes(pdfLocation)); | |
} | |
catch(e) { | |
logger.error(channelName + ' failed to encode PDF because: ' + e); | |
throw(e); | |
} | |
logger.info('Placing encoded PDF in transformed message'); | |
tmp['OBX'][i]['OBX.5']['OBX.5.1'] = 'application^PDF^^Base64^' + encodedPdf.toString(); | |
//may not be necessary, Mirth MAY delete files in $installDir/appdata/temp automatically | |
logger.info("Deleting files"); | |
try { | |
var file = Packages.java.io.File(rtfLocation); | |
file["delete"](); | |
file = Packages.java.io.File(pdfLocation); | |
file["delete"](); | |
} | |
catch(e) { | |
logger.error('Files failed to delete because: ' + e); | |
} | |
} |
@thenealmiller, sorry for the late reply. It's placed in the source transformer. I believe it was version 3. I'm not at that company anymore, so unfortunately I can't check for you :(
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am looking to utilize this code. Do you place this in the source transformer? Also what version of jodconverter are you using 2 or 3?