Why PDF is complicated in Mirth

Visit www.hl7engine.com to know more

This blog is about why PDF is complicated in Mirth? and How we can Split PDF in Mirth?.

Splitting of PDF, PDF parsing is complicated in Mirth because of the libraries used to perform few actions in it.

Basically, in JAVA if you want to split a PDF or parse a PDF content or manipulate data of a PDF content you would use couple of famous Libraries such as

  1. PDF BOX from Apache
  2. iText library (Licensed from 7.1 Version)

If you are going to use the unlicensed stable version on iText Library then the best version is 5.5.1

Problem of using PDF library in Mirth:

By Default, Mirth already uses these two libraries by default for its other functionality.

For Example: Mirth uses PDF BOX v.1.8.4 for the pdf viewer extension. If you are using a new version of PDF BOX library and provide it in anywhere in custom library or other locations it wont work.

Because Mirth do not identify which version of the library it has to select. You can see these two libraries in the following location shown in the screenshot below:

How to use PDF Box Library:

The Best library you can use to perform multiple functionality of PDF is using Apache PDF BOX library.

First, Mirth wants to read the font of the PDF that is suppose to manipulate. To, do that we need to add another library called fontbox-1.8.4 inside C:\Program Files\Mirth Connect\extensions\doc\lib location.

Then add this library path  in destination.xml in C:\Program Files\Mirth Connect\extensions\doc as <library type=”SERVER” path=”lib/fontbox-1.8.4.jar” />

Another approach:

If you really do not want to use the PDF Viewer functionality in mirth.

You can disable that extension and provide later version (v2.5) of PDF BOX library in the same way mentioned above.

Note: Using the PDF BOX library outside without following the above approach will not work. It will always throw error.

Is Splitting PDF possible inside Mirth?

Yes, certainly possible.

There is a Java program already written using Apache PDF Box library function https://gist.github.com/actsasflinn/4516ae1c322447bdc2634fab9240d70c 

I used the same function and converted to Javascript. Here is a sample of that code which is converted to EX4JS

Code converted from Java to Javascript – Splitting PDF

var AnyValueOfYyourChoice = ”;

for(q=0;q<data.length;q++){

var pdPage = org.apache.pdfbox.pdmodel.PDPage();
var inputDocument = org.apache.pdfbox.pdmodel.PDDocument.loadNonSeq(new java.io.File(globalMap.get(‘pdfReaderFilePath’)+$(‘originalFilename’)),null);
var stripper = new org.apache.pdfbox.util.PDFTextStripper();
var outputDocument = new org.apache.pdfbox.pdmodel.PDDocument();
var uuid = UUIDGenerator.getUUID();
var page;

for (page = 1; page <= inputDocument.getNumberOfPages(); ++page) {

stripper.setStartPage(page);
stripper.setEndPage(page);
var text = stripper.getText(inputDocument);
var p = new java.util.regex.Pattern.compile(DataNeedToBeCheckedFor);
var m = p.matcher(text);
if(m.find()){
var pdPage = inputDocument.getDocumentCatalog().getAllPages().get(page – 1);
outputDocument.importPage(pdPage);
}
}
var output_file = new java.io.File(globalMap.get(‘newPdfReaderPath’) +$(‘fileNameDocType’)+’_’+AnyValueOfYyourChoice+”.pdf”);
outputDocument.save(output_file);
outputDocument.close();
inputDocument.close();
}

Advertisements

Write HL7V2 Message to PDF

Scope :

To parse the incoming HL7V2.X message and write it to PDF.

Pre-requisite:

  • MirthConnect : 3.4.1.8057 (latest version)

Channel setup :

  1. Create a New channel name it as your wish Here I have named HL7V2 to PDF
  2. Click on Set Data Types button set the source connector inbound to HL7V2.x
  3. Outbound and Destination 1 to Raw
     – that’s it the channel setup is done for it.

Source Transformer :

  • Go to the source transformer and right click to create a new step
  • Name the step as HL7V2 to PDF
  • Select the drop-down of  the step that you have created and select Javascript

Code : 

//MSH Data
var sender = msg[‘MSH’][‘MSH.3’][‘MSH.3.1’].toString();
var receiver = msg[‘MSH’][‘MSH.5’][‘MSH.5.1’].toString();
var messageType = msg[‘MSH’][‘MSH.9’][‘MSH.9.1’].toString();
var messageEvent = msg[‘MSH’][‘MSH.9’][‘MSH.9.2’].toString();
var uniqueId = msg[‘MSH’][‘MSH.10’][‘MSH.10.1’].toString();
channelMap.put(“sender”,sender);
channelMap.put(“receiver”,receiver);
channelMap.put(“messageType”,messageType);
channelMap.put(“messageEvent”,messageEvent);
channelMap.put(“uniqueId”,uniqueId);
//PID Data
var patientName = msg[‘PID’][‘PID.5’][‘PID.5.1’].toString()+” “+msg[‘PID’][‘PID.5’][‘PID.5.2’].toString();
channelMap.put(“patientName”,patientName);
//OBX data
var obxLen = msg[‘OBX’].length();
for(obx=0;obx<obxLen;obx++)
{
var obxSetId = msg[‘OBX’][obx][‘OBX.3’][‘OBX.3.1’].toString();
logger.info(obxSetId)
channelMap.put(“setId”,obxSetId);
}

Destination :

  1. Select Document Writer as the destination setup
  2. Provide the directory to be written and the file name to be generated.

HTML Template : 

Design an HTML template with the following code below:

<html>

HL7 Message Patient Details


//quotes added to remove wordpress html way

<hr></hr>
<br></br>

${patientName}

//quotes added to remove wordpress html way
<ol>
<li><u>Sending Facility</u> : ${sender}</li>
<li><u>Receiving Facility</u> : ${receiver}</li>
<li><u>Message Type </u> : ${messageType}</li>
<li><u>Message Event </u> : ${messageEvent}</li>
<li><u>Control-Id </u> : ${uniqueId}</li>
<hr></hr>
${setId}
</ol>
<br>
</br>

</html>

Destination Configuration :

destination-pdf

Sample data for Source :

use the sample data provided here to pass it on to mirth

pdf-writer

 

 

Blog at WordPress.com.

Up ↑