Sidebar

How do I create a PDF from an ORU?

0 votes
1.8K views
asked Nov 5, 2013 by oscar-p-6691 (550 points)
How do you create a PDF from an ORU in the destination node file path?

2 Answers

+3 votes
 
Best answer

The below example code was provided by Richard at Qvera.  I edited it to make it a bit more simple.  You must install the itext jar files in Qvera for this to work.  Name it like any other file using {s:OBR-3}.pdf or whatever you want to name it in the destination node.

 

// This mapping uses the itextpdf-5.3.4.jar library found at
//   http://itextpdf.com

// copy the jar into the QIE\lib\ folder
// and then configure QIE to include the jar using the
//   'Manage External Libraries' button under 'System Configuration'

importPackage(java.io);

// wrap all lines in OBX-5 to 77 characters or less
// wrapOBX5(77);

// get text out of all OBX-5 (uncommet this and comment out the above line for multiple OBX-5)
var obx5 = 'A NICE HEADER~~';
var instances = message.getAllNodes('OBX-5');
for (var inst = 0 ; inst < instances.length ; inst++) {
   obx5 += instances[inst] + "~";
}

// parse into string array
var lines = obx5.split('~');

// convert text to PDF
var byteArrayOutputStream = new java.io.ByteArrayOutputStream();
var document = new com.itextpdf.text.Document();
document.setPageSize(com.itextpdf.text.PageSize.LETTER);
document.setMargins(72, 72, 36, 54);
var font = new com.itextpdf.text.Font(com.itextpdf.text.Font.FontFamily.COURIER, 10);
com.itextpdf.text.pdf.PdfWriter.getInstance(document, byteArrayOutputStream).setInitialLeading(10);
document.open();

//document.add(com.itextpdf.text.pdf.codec.BmpImage.getImage("c:\\sample.bmp"));
var pngmap = new com.itextpdf.text.pdf.codec.PngImage.getImage("C:\\hl7\\in\\cgi-example.png");
pngmap.scaleAbsolute(150, 75);
pngmap.setAbsolutePosition(400, 680);
document.add(new com.itextpdf.text.Chunk("Line One Text\nLine Two Text\nLine Three Text", font));
document.add(pngmap);
document.add(new com.itextpdf.text.Chunk("\n\n\n\n\n______________________________________________________________________________\n\n", font));

for (var i = 0; i < lines.length; i++) {
   document.add(new com.itextpdf.text.Chunk(lines[i], font));
   document.add(com.itextpdf.text.Chunk.NEWLINE);
}
document.close();

//Get observation date for folder structure
var obsdate = source.getNode('OBR-7');
var year = obsdate.substring(0, 4);
var month = obsdate.substring(4, 6);
var day = obsdate.substring(6, 8);

// create new message with PDF as the content
message = qie.createTextMessage();
message.setNode('/', byteArrayOutputStream.toString('ISO-8859-1'));

answered Nov 5, 2013 by oscar-p-6691 (550 points)
edited Nov 5, 2013 by oscar-p-6691
0 votes

Another way to handle this instead of using iText we can use Flying Saucer.

 

message = qie.createTextMessage('', 'UTF-8');
 
var sourceLines = source.getAllNodes('NTE-3');
var textDoc = '<p>';
 
for (var i=0 ; i < sourceLines.length ; i++) {
   textDoc = textDoc + "<br/>" + sourceLines[i];
}
 
message.setNode('/', textDoc + '</p>');
 
// This mapping uses the flyingsaucer-R8.zip library found at
 
// extract the contents of the zip file into the QIE\lib\flyingsaucer-R8\ folder
// and then configure QIE to include the folder using the
//   'Manage External Libraries' button under 'System Configuration'
 
// convert html to PDF
var byteArrayOutputStream = new java.io.ByteArrayOutputStream();
var renderer = new org.xhtmlrenderer.pdf.ITextRenderer();
renderer.setDocumentFromString(message.getNode('/'));
renderer.layout();
renderer.createPDF(byteArrayOutputStream);
 
// create new message with PDF as the content
message = qie.createTextMessage('', 'ISO-8859-1');
message.setNode('/', byteArrayOutputStream.toString('ISO-8859-1'));

 

answered Oct 27, 2016 by michael-h-5027 (14,350 points)
...