This topic contains 4 replies, has 3 voices, and was last updated by  Ryan McBee 4 years, 10 months ago.

Decoding Base64 In HL7

  • I’m writing my first interface for handling orders and I find myself having to decode a field containing a Base64 encryption of a PDF. filter.base64.dec() is decoding the information and writing a valid PDF file, and contains part of the data visible in the full PDF. But it’s not the complete PDF.

    Can anyone advise what I might be doing wrong? I’d bet it has something to do with how I’m handling the string, I’m not very experianced in that. I’m at a loss as to what it is and would appreciate any help anyone can provide.

    require("dateparse")
    require("node")
    require("hl7util")
    require("codemap")
    require("stringutil")
    
    function main(MSG)
       MSGParser(MSG)
       DestinationPath = 'c:/test/'
       CreateImage(file)
       end
    
    function MSGParser(MSG)
       rawmsg = hl7.parse{vmd='Image.vmd',data=MSG}
       file = rawmsg.Group4[1].Group5.Group7[1].OBX[5][5]
       filetype = rawmsg.Group4[1].Group5.Group7[1].OBX[5][3]
       PatMrnNO = rawmsg.Group1.PID[3][1]
       PatAcctNo = rawmsg.Group1.PID[18][1]
       HospitalSited = rawmsg.MSH[4][1]   
       
    function CreateImage()
       local FileName = os.date('Trace_'..PatMrnNO..'_'..PatAcctNo..'_%Y%m%d%H%M%S.'..filetype)
       local F = io.open(DestinationPath..FileName, "w")
       F:write(filter.base64.dec(tostring(file)))
       F:close()
    end

    M.R. McBee

    try to change mode in which you open the file (PDF files they binary) and don’t call to string() since content of OBX-5 is anyway string:

    local F = io.open(DestinationPath..FileName, “wb”)

    F:write(filter.base64.dec(file))

    When assigning the value to ‘file’, try this:

    file = rawmsg.Group4[1].Group5.Group7[1].OBX[5][5]:nodeValue()

    This will ensure you get the raw value. Calling tostring() on the HL7 node will perform HL7 escaping on the value, which can get you into trouble in this context. So when writing to the file, omit the call to tostring() (since you used :nodeValue() above):

    F:write(filter.base64.dec(file))

    That should hopefully do the trick!

    yes, Kevin is right, I missed the nodeValue() call, having said that, notice the file mode ‘wb’.

    Thanks a bunch guys. Works like a charm.

    M.R. McBee

Tagged: 

You must be logged in to reply to this topic.