Announcement

Collapse
No announcement yet.

Merge PDFs

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

  • 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:
    https://www.tutorialkart.com/pdfbox/...multiple-pdfs/

    I cobbled together this:
    Code:
    var origFile = FileUtil.readBytes($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'.pdf');
    var newFile = $('pdf_payload');
    var pdfMerger = new PDFMergerUtility();
    	pdfMerger.setDestinationFileName($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'.pdf');
    	pdfMerger.addSource(origFile);
    	pdfMerger.addSource(newFile);
    	pdfMerger.mergeDocuments(null);
    Last edited by dforesman; 08-29-2019, 10:58 AM.

  • #2
    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.
    Last edited by omnitux; 08-30-2019, 12:46 AM.

    Comment


    • #3
      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.

      Code:
      JavaScript Writer error
      ERROR MESSAGE: Error evaluating JavaScript Writer
      com.mirth.connect.server.MirthJavascriptTransformerException: 
      CHANNEL:	HL7 with base64 payload to PDF
      CONNECTOR:	merge files if exists pt 2
      SCRIPT SOURCE:	JavaScript Writer
      SOURCE CODE:	
      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.

      Comment


      • #4
        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.
        Code:
        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();
        	pdfMerger.setDestinationFileName($('file_location')+$('pt_mrn')+"_"+$('pt_name')+'_MERGED'+'.pdf');
        	pdfMerger.addSource(origFile);
        	pdfMerger.addSource(newFile);
        	pdfMerger.mergeDocuments(null);
        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
        SOURCE CODE:
        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);
        3676:
        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);
        3682:
        3683: /*
        LINE NUMBER: 3679
        DETAILS: Wrapped java.lang.RuntimeException: java.io.FileNotFoundException: [[email protected] (The system cannot find the file specified)

        Comment


        • #5
          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!

          Code:
           
          //merge examples
          //https://www.programcreek.com/java-api-examples/?api=org.apache.pdfbox.util.PDFMergerUtility
          //https://stackoverflow.com/questions/13808090/create-pdf-and-merge-with-pdfbox
          
          //byte array
          //http://www.mirthproject.org/community/forums/showthread.php?p=253236
          //http://www.mirthcorp.com/community/forums/showthread.php?t=3909
          
          //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
          //destination
          var bout2 = new java.io.BufferedOutputStream(new java.io.FileOutputStream('C:/Users/Media/Desktop/PDF testing/old/MM2_pdf_dummy.pdf'));
          //magic
                      merger.setDestinationStream(bout2);
                      merger.mergeDocuments();

          Comment

          Working...
          X