Mirth Community

Mirth Community (http://www.mirthcorp.com/community/forums/index.php)
-   Support (http://www.mirthcorp.com/community/forums/forumdisplay.php?f=6)
-   -   Merge PDFs (http://www.mirthcorp.com/community/forums/showthread.php?t=220489)

dforesman 08-29-2019 11:51 AM

Merge PDFs
I am sure I missed a post or a hint somewhere; however I am in the process of trying to combine PDFs that exist on a file share. Hoping for a little guidance with this, I am suspecting I need to build a function. Any guidance is appreciated. Thanks!

Version 3.7.1

Using this link:

I cobbled together this:

var origFile = FileUtil.readBytes($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'.pdf');
var newFile = $('pdf_payload');
var pdfMerger = new PDFMergerUtility();

omnitux 08-30-2019 01:40 AM

That are too little information. Which errors are there? What's the value of "$('pdf_payload')"?

You have to instantiate the class PDFMergerUtility with "var pdfMerger = new org.apache.pdfbox.util.PDFMergerUtility();". If you do not load a package than you have to use the full package path.

Maybe you will have a look here: https://pdfbox.apache.org/docs/1.8.1...a.lang.String). The method "addSource" only accepts String, File, InputStream or List<InputStream>. FileUtil.readBytes gives you a byte array and there is no overloaded method to do that.

dforesman 08-30-2019 05:03 AM

As HL7 messages are received, I have validation in place that IF a file already exists I want to read in the existing file and combine it with the message that is in OBX 5.5 which was named $('pdf_payload').

So the error message I receive is below; I am suspecting I am not calling pdfbox properly; though I did add the newer version 2.0.16 to the custom lib.


JavaScript Writer error
ERROR MESSAGE: Error evaluating JavaScript Writer
CHANNEL:        HL7 with base64 payload to PDF
CONNECTOR:        merge files if exists pt 2
SCRIPT SOURCE:        JavaScript Writer
3665: }
3666: function doScript() {
3667: //move current document
3668: var origFile = FileUtil.readBytes($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'.pdf');
3669: var newFile = $('pdf_payload');
3670: var pdfMerger = new PDFMergerUtility();
3671:        pdfMerger.setDestinationFileName($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'.pdf');
3672:        pdfMerger.addSource(origFile);
3673:        pdfMerger.addSource(newFile);
3674:        pdfMerger.mergeDocuments(null);
LINE NUMBER:        3670
DETAILS:        ReferenceError: "PDFMergerUtility" is not defined.

dforesman 08-30-2019 07:51 AM

Well now I feel a little silly. I re-read your post and realized you had some other points and I failed to update my logic with your suggestions.

ow it is tripping up on the bytes.

var origFile = FileUtil.readBytes($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'.pdf');
        channelMap.put('origFile', origFile); //validation

var newFile = $('pdf_payload');
        channelMap.put('newFile', newFile); //validation
var pdfMerger = new org.apache.pdfbox.util.PDFMergerUtility();

JavaScript Writer error
ERROR MESSAGE: Error evaluating JavaScript Writer
com.mirth.connect.server.MirthJavascriptTransforme rException:
CHANNEL: HL7 with base64 payload to PDF
CONNECTOR: merge files if exists pt 2
SCRIPT SOURCE: JavaScript Writer
3674: //newFile = new XML(SerializerFactory.getHL7Serializer().toXML(new java.lang.String(org.apache.commons.codec.binary.B ase64.decodeBase64(newFile),'UTF-8')))
3675: channelMap.put('newFile', newFile);
3677: var pdfMerger = new org.apache.pdfbox.util.PDFMergerUtility();
3678: pdfMerger.setDestinationFileName($('file_location' )+$('pt_mrn')+"_"+$('pt_name')+'_MERGED'+'.pdf');
3679: pdfMerger.addSource(origFile);
3680: pdfMerger.addSource(newFile);
3681: pdfMerger.mergeDocuments(null);
3683: /*
DETAILS: Wrapped java.lang.RuntimeException: java.io.FileNotFoundException: [B@f439c4e (The system cannot find the file specified)

dforesman 09-06-2019 05:44 PM

Merge pdf working code
So I went back and started from scratch after reading different posts about using pdfbox within the forums and finally got it working. I included the working code below, JavaScript is one of my weaker languages but omnitux’s reply taught me some new tricks like using java docs.

Anyway, this works with pdfbox that is shipped with mirth; tested working in 3.7.1 and 3.8.1.

Once again Mirth saves the day!


//merge examples

//byte array

//existing file
var pdf1 = FileUtil.readBytes('C:/Users/Media/Desktop/PDF testing/old/testing_large.pdf');
//file to be merged
var pdf2 = FileUtil.readBytes('C:/Users/Media/Desktop/PDF testing/old/Psychiatric SOAP Note Example.pdf');
var merger = new org.apache.pdfbox.util.PDFMergerUtility();
  merger.addSource(new java.io.ByteArrayInputStream(pdf1));
  merger.addSource(new java.io.ByteArrayInputStream(pdf2));
  // do the rest with the merger
var bout2 = new java.io.BufferedOutputStream(new java.io.FileOutputStream('C:/Users/Media/Desktop/PDF testing/old/MM2_pdf_dummy.pdf'));

All times are GMT -8. The time now is 11:16 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation