Announcement

Collapse
No announcement yet.

Convert PDF to DICOM object

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Convert PDF to DICOM object

    Does Mirth have the functionality to convert BASE-64 encoded data into a DICOM image? I'm trying to attach radiology reports to existing DICOM studies in PACS and was hoping Mirth can assist.

    Any help that you may have is greatly appreciated.

  • #2
    Hi citetech,

    Me too looking for a similar scenario, need to convert a pdf to dicom object then send to PACS archive.

    Any idea how to do it? appreciate your support

    Comment


    • #3
      I pull scan documents (jpg format) from a db and send them as a DICOM Object. I used this website to get me where I needed.

      http://samucs.blogspot.com/2008/12/c...g-dcm4che.html

      Comment


      • #4
        We have channel that receives an HL7 message containing a URL to a PDF file. From that information we create a DICOM Encapsulated PDF and send it to PACS.

        The most important part of this process takes place in the DICOM sender transformer, where we create the actual DICOM object from a template. Below is the code for the transformer.
        Code:
        importPackage(Packages.org.dcm4che2.data);
        
        // prerequisite: have a skeleton with all the static DICOM parts stored in the outbound message template 'tmp'
        
        // convert template to a dcm4che DicomObject
        var dicomBytes = FileUtil.decode(SerializerFactory.getSerializer('DICOM').fromXML(tmp));
        var dcmObject = DICOMUtil.byteArrayToDicomObject(dicomBytes, false);
        
        // miscellaneous setup
        var random = new java.util.Random();
        var now = new java.util.Date();
        
        //  UID prefixes
        var UID_PREFIX = "9.8.7.6.5."; // get your own unique UID root to use here
        var STUDY_SUB = "1.";
        var SERIES_SUB = "2.";
        var INSTANCE_SUB = "3.";
        
        // create SOP instance UID for the new object and insert it into the object
        var instanceUID = UID_PREFIX + INSTANCE_SUB + DateUtil.formatDate("yyyyMMddHHmmssSSS", new java.util.Date()) + "." + random.nextInt(100000);
        
        
        dcmObject.putString(Tag.MediaStorageSOPInstanceUID, VR.UI, instanceUID);
        dcmObject.putString(Tag.SOPInstanceUID, VR.UI, instanceUID);
        
        // object creation date & time
        dcmObject.putDate(Tag.InstanceCreationDate, VR.DA, now);
        dcmObject.putDate(Tag.InstanceCreationTime, VR.TM, now);
        
        // extract values for study date and time from the incoming HL7 message
        studyDateString = msg['OBR']['OBR.7']['OBR.7.1'].toString().substring(0, 8);
        studyTimeString = msg['OBR']['OBR.7']['OBR.7.1'].toString().substring(8, 14);
        
        dcmObject.putString(Tag.StudyDate, VR.DA, studyDateString);
        dcmObject.putString(Tag.StudyTime, VR.TM, studyTimeString);
        
        // accession number from incoming HL7
        dcmObject.putString(Tag.AccessionNumber, VR.SH, msg['OBR']['OBR.2']['OBR.2.1'].toString());
        
        
        // patient demographics from HL7
        dcmObject.putString(Tag.PatientName, VR.PN, msg['PID']['PID.5']['PID.5.1'].toString() + "^" + msg['PID']['PID.5']['PID.5.2'].toString());
        dcmObject.putString(Tag.PatientID, VR.LO, msg['PID']['PID.3']['PID.3.1'].toString());
        dcmObject.putString(Tag.PatientBirthDate, VR.DA, msg['PID']['PID.7']['PID.7.1'].toString().substring(0, 8));
        dcmObject.putString(Tag.PatientSex, VR.CS, msg['PID']['PID.8']['PID.8.1'].toString());
        
        // use previously found StudyUID to link new object to existing study on PACS
        dcmObject.putString(Tag.StudyInstanceUID, VR.UI, $('studyUID'));
        
        // create and insert series UID
        var seriesInstanceUID = UID_PREFIX + SERIES_SUB + DateUtil.formatDate("yyyyMMddHHmmssSSS", new java.util.Date()) + "." + random.nextInt(100000);
        dcmObject.putString(Tag.SeriesInstanceUID, VR.UI, seriesInstanceUID);
        
        dcmObject.putInt(Tag.SeriesNumber, VR.IS, 99);
        
        dcmObject.putString(Tag.SeriesDescription, VR.LO, "Protokoll " + DateUtil.convertDate("yyyyMMddHHmmss", "dd.MM.yyyy HH:mm", msg['OBR']['OBR.22']['OBR.22.1'].toString()));
        
        
        // the PDF has been previously stored as an attachment to the message
        // now we retrieve the attachment and put it's contents into the EncapsulatedDocument field of DICOM object
        var attachmentList = getAttachments();
        if(attachmentList.size() != 1) {
        	throw "Attachment count not exactly 1";
        }
        
        var attachment = attachmentList.get(0);
        if(attachment.getId().equals($('attachmentId'))) {
        	dcmObject.putBytes(Tag.EncapsulatedDocument, VR.OB, FileUtil.decode(new java.lang.String(attachment.getContent())));
        } else {
        	throw "Attachment ID " + attachment.getId() + " does not match expected ID " + $('attachmentId');
        }
        
        
        // convert the final dcm4che DicomObject back to Mirth XML representation and store it in tmp for further processing and sending
        tmp = SerializerFactory.getSerializer('DICOM').toXML( FileUtil.encode(DICOMUtil.dicomObjectToByteArray(dcmObject)));
        Last edited by TarmoR; 03-28-2017, 11:12 PM. Reason: removed original UID_PREFIX value from code

        Comment


        • #5
          Template

          Hi Tarmor,

          can you add that DICOM template?, of course removing sensible data. I don't know how to start developing that template.

          Thank you,

          RB

          Comment


          • #6
            Originally posted by ricber View Post
            Hi Tarmor,

            can you add that DICOM template?, of course removing sensible data. I don't know how to start developing that template.

            Thank you,

            RB
            The template, which I use, but is not probably perfect, is this:
            Code:
            <dicom>
                <tag00020000 len="4" tag="00020000" vr="UL">176</tag00020000>
                <tag00020001 len="2" tag="00020001" vr="OB">00\01</tag00020001>
                <tag00020002 len="30" tag="00020002" vr="UI">1.2.840.10008.5.1.4.1.1.104.1</tag00020002>
                <tag00020003 len="44" tag="00020003" vr="UI">2.25.323317228874724747205459295724198644543</tag00020003>
                <tag00020010 len="20" tag="00020010" vr="UI">1.2.840.10008.1.2.1</tag00020010>
                <tag00020012 len="16" tag="00020012" vr="UI">1.2.40.0.13.1.1</tag00020012>
                <tag00020013 len="12" tag="00020013" vr="SH">dcm4che-2.0</tag00020013>
                <tag00080005 len="10" tag="00080005" vr="CS">ISO_IR 100</tag00080005>
                <tag00080012 len="8" tag="00080012" vr="DA">20160519</tag00080012>
                <tag00080013 len="10" tag="00080013" vr="TM">153704.348</tag00080013>
                <tag00080016 len="30" tag="00080016" vr="UI">1.2.840.10008.5.1.4.1.1.104.1</tag00080016>
                <tag00080018 len="44" tag="00080018" vr="UI">2.25.323317228874724747205459295724198644543</tag00080018>
                <tag00080020 len="0" tag="00080020" vr="DA"/>
                <tag00080030 len="0" tag="00080030" vr="TM"/>
                <tag00080050 len="0" tag="00080050" vr="SH"/>
                <tag00080060 len="2" tag="00080060" vr="CS">OT</tag00080060>
                <tag00080064 len="2" tag="00080064" vr="CS">SD</tag00080064>
                <tag00080070 len="0" tag="00080070" vr="LO"/>
                <tag00080090 len="0" tag="00080090" vr="PN"/>
                <tag00100010 len="0" tag="00100010" vr="PN"/>
                <tag00100020 len="0" tag="00100020" vr="LO"/>
                <tag00100030 len="0" tag="00100030" vr="DA"/>
                <tag00100040 len="0" tag="00100040" vr="CS"/>
                <tag0020000D len="44" tag="0020000D" vr="UI">2.25.147820741865979970770530313520387157005</tag0020000D>
                <tag0020000E len="44" tag="0020000E" vr="UI">2.25.56021845268099275834316537825326533336</tag0020000E>
                <tag00200010 len="0" tag="00200010" vr="SH"/>
                <tag00200011 len="2" tag="00200011" vr="IS">1</tag00200011>
                <tag00200013 len="2" tag="00200013" vr="IS">1</tag00200013>
                <tag00280301 len="4" tag="00280301" vr="CS">YES</tag00280301>
                <tag0040A043 len="0" tag="0040A043" vr="SQ"/>
                <tag00420010 len="0" tag="00420010" vr="ST"/>
                <tag00420011 len="10" tag="00420011" vr="OB">25\50\44\46\2D\31\2E\34\0A\00</tag00420011>
                <tag00420012 len="16" tag="00420012" vr="LO">application/pdf</tag00420012>
            </dicom>
            I also took out from my original post our UID root, which I forgot in there - do not use ours, user your own or get one, if you don't have it yet.

            Comment


            • #7
              Hi Tarmor,

              thank you very much, this is a very good clue!

              RB

              Comment


              • #8
                Input a file reader

                I am trying to convert the code to use a file reader to get the PDF document.
                Did not succeed yet.
                Do you have any suggestions how to do this?
                X Connections
                https://documentor.email
                https://www.x-connections.com

                Comment


                • #9
                  Originally posted by mdehoog View Post
                  I am trying to convert the code to use a file reader to get the PDF document.
                  Did not succeed yet.
                  Do you have any suggestions how to do this?
                  Make sure you have Binary selected as the file type then in your transformer on the destination you can get the raw bytes like so.

                  var rawbytes = connectorMessage.getRawData();

                  Comment


                  • #10
                    Running into an error when trying to get the attachment from a file reader:

                    Transformer error
                    ERROR MESSAGE: Error evaluating transformer
                    com.mirth.connect.server.MirthJavascriptTransforme rException:
                    CHANNEL: EKG PDF to Dicom
                    CONNECTOR: Destination 2
                    SCRIPT SOURCE: TRANSFORMER
                    SOURCE CODE:
                    114:
                    115: // the PDF has been previously stored as an attachment to the message
                    116: // now we retrieve the attachment and put it's contents into the EncapsulatedDocument field of DICOM object
                    117: var attachmentList = getAttachments();
                    118: if(attachmentList.size() != 1) {
                    119: throw "Attachment count not exactly 1";
                    120: }
                    121:
                    122: var attachment = attachmentList.get(0);
                    123: if(attachment.getId().equals($('attachmentId'))) {
                    LINE NUMBER: 119
                    DETAILS: Attachment count not exactly 1
                    at 74a7617b-3404-4da1-83d8-242a7d2d47e1:119 (doTransform)
                    at 74a7617b-3404-4da1-83d8-242a7d2d47e1:155 (doScript)
                    at 74a7617b-3404-4da1-83d8-242a7d2d47e1:157
                    at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:154)
                    at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.doCall(JavaS criptFilterTransformer.java:119)
                    at com.mirth.connect.server.util.javascript.JavaScrip tTask.call(JavaScriptTask.java:113)
                    at java.util.concurrent.FutureTask.run(Unknown Source)
                    at java.util.concurrent.ThreadPoolExecutor.runWorker( Unknown Source)
                    at java.util.concurrent.ThreadPoolExecutor$Worker.run (Unknown Source)
                    at java.lang.Thread.run(Unknown Source)
                    Attached Files

                    Comment


                    • #11
                      You can't get the attachment because you haven't created one.

                      On your Summary tab, choose "Entire Message" for the attachment handler, click the properties button next to it and set the mime type to application/pdf.

                      Comment


                      • #12
                        Thank you agermano.

                        I have set the summary attachment to application/pdf

                        Attachment shows up under attachments.
                        But when attempting top open using the PDF viewer in MC, it fails.
                        Any ideas?
                        Attached Files

                        Comment


                        • #13
                          Thank you agermano.

                          I didnt have the source reader set to Binary mode.

                          Comment


                          • #14
                            Message shows it was sent successfully and the PDF shows as an attachment but the PACs system only shows that we attempted to send something but its not filing to PACS. Using ${tmp} as the sender template in the destination.
                            What I am I doing wrong?

                            Comment


                            • #15
                              Typically you would use ${DICOMMESSAGE} in your destination template.

                              Comment

                              Working...
                              X