Decode and create a pdf from a ED HL7 message

Hello Gentlemen,


My Business process connector receive a Oru_01 hl7 message with an ED segment inside (Base64 pdf). How is it possible to extract it and create a pdf file within?

Here is the sample of function I have. I know it doesn't work but if you could help me, it would be perfect. Here is my function, the goal would be it create the file where I want and return me the link of the file.

Method getPdfFromED(req As EnsLib.HL7.Message,segment As %String) As %Status
    do req.GetFieldStreamBase64(.base64, segment)
    set stream1=##class(%Stream.FileCharacter).%New()
    set stream2=##class(%Stream.FileCharacter).%New()
    set sc = stream2.LinkToFile("\\chr13139\test\ceciEstUnTest.txt")
    while 'base64.AtEnd {
        set temp=base64.Read()
        set temp=$translate(temp, $c(13,10))
        do stream1.Write(temp)
    do stream1.Rewind()
    while 'stream1.AtEnd {
        set temp=stream.Read(4000)
        set temp=$system.Encryption.Base64Decode(temp)
        do stream2.Write(temp)
     do stream2.%Save()
    return $$$OK


  • 0
  • 0
  • 65
  • 3
  • 2


The code looks good. What error are you getting?

Try replacing stream2 %Stream.FileCharacter with %Stream.FileBinary.

Try replacing stream1 %Stream.FileCharacter with %Stream.TmpBinary.

If you have message sample less than 3,5 mb in size try to write a test without intermediate stream.

You probably should write to a pdf file and not a txt one.

If you can, get a sample original/decoded file. Compare original file and your stream2 file using hex editor to spot differences.

Thanks for your interest.

I don't receive any errors, the compilation is fine but it doesn't create any file so it's pointless :-/ ...

Annex question, does 'LinkToFile' need that the file exist in the first place or will it create it?

I will try by switching what you said above

Link to file doesn't need for a file to exist, but the containing directory must exist and should be writable by a OS user (cacheusr in uyour case probably).

I'd try to write into a temp dir first, where you're sure you have access:

set file = ##class(%File).TempFilename("pdf")
set sc = stream2.LinkToFile(file)
quit:$$$ISERR(sc) sc

record value of file somewhere (output to display or store in global) and check if the file was created).

%Save method also returns status, you should return it instead of $$$OK:

set sc = stream2.%Save()
quit sc

With a little help I have built the following Method to Decode and save off the Encoded pdf but return me the directory that I saved the PDF under....

ClassMethod DecodeBase64HL7ToFile(base64 As %Stream.GlobalBinary, Ancillary As %String, FileName As %String) As %String
set ArchDir = "/ensemble/data/transfer/AncillaryPDF/"
set ArchAncDir = ArchDir_Ancillary_"/"
set FaxDateDir = ArchAncDir_$PIECE($ZDATE($HOROLOG,7)," ",1)_"-"_$PIECE($ZDATE($HOROLOG,7)," ",2)_"-1/"
if '##class(%Library.File).DirectoryExists(ArchDir)
do ##class(%Library.File).CreateDirectory(ArchDir)
if '##class(%Library.File).DirectoryExists(ArchAncDir)
do ##class(%Library.File).CreateDirectory(ArchAncDir)
if '##class(%Library.File).DirectoryExists(FaxDateDir)
do ##class(%Library.File).CreateDirectory(FaxDateDir)

set Oref = ##class(%FileBinaryStream).%New()
set Oref.Filename = FaxDateDir_FileName
Do base64.Rewind()

While 'base64.AtEnd {
     set ln = base64.ReadLine()
    set lnDecoded = $system.Encryption.Base64Decode(ln)
do Oref.Write(lnDecoded)

Do Oref.%Save()
set PDFFilePath = FaxDateDir_FileName
return PDFFilePath

You can use CreateDirectoryChain method of %File class to create directory tree instead of several calls to CreateDirectory.