Announcement

Collapse
No announcement yet.

Using iText in Mirth - is there an example?

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

  • Using iText in Mirth - is there an example?

    I've searched the Forums looking for an example of someone using iText in a Mirth channel but haven't had any luck thus far. Anyone out there using iText and willing to donate an example? Please?

    Many thanks!

    Ken

  • #2
    Using iText in Mirth Connect would be very similar to using it in any other standalone Java project. The only difference is that you would be working in JavaScript and calling out to Java classes via Rhino. To that end, iText appears to have examples on its website: http://itextpdf.com/examples/
    Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

    Nicholas Rupley
    Work: 949-237-6069
    Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


    - How do I foo?
    - You just bar.

    Comment


    • #3
      OK, here's the example I'm trying to replicate in Mirth.

      *
      * This class is part of the book "iText in Action - 2nd Edition"
      * written by Bruno Lowagie (ISBN: 9781935182610)
      * For more info, go to: http://itextpdf.com/examples/
      * This example only works with the AGPL version of iText.
      */

      package part1.chapter01;

      import java.io.FileOutputStream;
      import java.io.IOException;

      import com.itextpdf.text.Document;
      import com.itextpdf.text.DocumentException;
      import com.itextpdf.text.Paragraph;
      import com.itextpdf.text.pdf.PdfWriter;

      /**
      * First iText example: Hello World.
      */
      public class HelloWorld {

      /** Path to the resulting PDF file. */
      public static final String RESULT
      = "results/part1/chapter01/hello.pdf";

      /**
      * Creates a PDF file: hello.pdf
      * @param args no arguments needed
      */
      public static void main(String[] args)
      throws DocumentException, IOException {
      new HelloWorld().createPdf(RESULT);
      }

      /**
      * Creates a PDF document.
      * @param filename the path to the new PDF document
      * @throws DocumentException
      * @throws IOException
      */
      public void createPdf(String filename)
      throws DocumentException, IOException {
      // step 1
      Document document = new Document();
      // step 2
      PdfWriter.getInstance(document, new FileOutputStream(filename));
      // step 3
      document.open();
      // step 4
      document.add(new Paragraph("Hello World!"));
      // step 5
      document.close();
      }
      }
      ================================
      And here's my attempt to "Mirthisize" it:
      importPackage(com.itextpdf.text.Document);
      importPackage(com.itextpdf.text.DocumentException) ;
      importPackage(com.itextpdf.text.Paragraph);
      importPackage(com.itextpdf.text.pdf.PdfWriter);


      function createPDF()
      {
      var filename = "/test/out/hello.pdf";
      var x = msg['row']['column1']
      var document = new Document();
      PdfWriter.getInstance(document, new FileOutputStream(filename));
      document.open();
      document.add(new Paragraph("Hello World!"));
      document.add(new Paragraph(x));
      document.close();
      return;
      }

      createPDF();
      =======================================
      And here's the error that Mirth reports:
      Transformer error
      ERROR MESSAGE: Error evaluating transformer
      com.mirth.connect.server.MirthJavascriptTransforme rException:
      CHANNEL: test
      CONNECTOR: sourceConnector
      SCRIPT SOURCE: TRANSFORMER
      SOURCE CODE:
      34:
      35: function createPDF()
      36: {
      37: var filename = "/test/out/hello.pdf";
      38: var x = msg['row']['column1']
      39: var document = new Document();
      40: PdfWriter.getInstance(document, new FileOutputStream(filename));
      41: document.open();
      42: document.add(new Paragraph("Hello World!"));
      43: document.add(new Paragraph(x));
      LINE NUMBER: 39
      DETAILS: ReferenceError: "Document" is not defined.
      at eb892433-8de3-47f7-8509-eeaea4360f53:39 (createPDF)
      at eb892433-8de3-47f7-8509-eeaea4360f53:48 (doTransform)
      at eb892433-8de3-47f7-8509-eeaea4360f53:51 (doScript)
      at eb892433-8de3-47f7-8509-eeaea4360f53:53
      at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.call(JavaScr iptFilterTransformer.java:134)
      at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.call(JavaScr iptFilterTransformer.java:100)
      at java.util.concurrent.FutureTask$Sync.innerRun(Unkn own Source)
      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)
      ===================================

      I know that the importPackage statements are working since when I do an importPackage(DobBarf); it complains about there being no such package.

      Any ideas?

      Comment


      • #4
        The issue probably is that you're not passing a package name into importPackage.

        Code:
        importPackage(com.itextpdf.text);
        Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

        Nicholas Rupley
        Work: 949-237-6069
        Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


        - How do I foo?
        - You just bar.

        Comment


        • #5
          No Joy NARUPLY.

          Still gettting:

          Transformer error
          ERROR MESSAGE: Error evaluating transformer
          com.mirth.connect.server.MirthJavascriptTransforme rException:
          CHANNEL: test
          CONNECTOR: sourceConnector
          SCRIPT SOURCE: TRANSFORMER
          SOURCE CODE:
          35:
          36: function createPDF()
          37: {
          38: var filename = "/test/out/hello.pdf";
          39: var x = msg['row']['column1']
          40: var document = new Document();
          41: PdfWriter.getInstance(document, new FileOutputStream(filename));
          42: document.open();
          43: document.add(new Paragraph("Hello World!"));
          44: document.add(new Paragraph(x));
          LINE NUMBER: 40
          DETAILS: ReferenceError: "Document" is not defined.
          at 5fe2ac93-4364-4808-a5c0-d369c0cfbed3:40 (createPDF)
          at 5fe2ac93-4364-4808-a5c0-d369c0cfbed3:49 (doTransform)
          at 5fe2ac93-4364-4808-a5c0-d369c0cfbed3:52 (doScript)
          at 5fe2ac93-4364-4808-a5c0-d369c0cfbed3:54
          at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.call(JavaScr iptFilterTransformer.java:134)
          at com.mirth.connect.server.transformers.JavaScriptFi lterTransformer$FilterTransformerTask.call(JavaScr iptFilterTransformer.java:100)
          at java.util.concurrent.FutureTask$Sync.innerRun(Unkn own Source)
          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)

          Comment


          • #6
            OK, narupley may be on to something.

            I changed the order of instantiation in the code to create a new FileOutputStream first and it stated that it could not find FileOutputStream (ReferenceError: "FileOutputStream" is not defined) so I then added an ImportPackage(java.io) and it was able to instantiate FileOutputStream.

            I can only assume that I'm not specifying the iText package correctly in my ImportPackage statement. However I've tried all of the following variations without success:
            importPackage(java.io);
            importPackage(com.itextpdf);
            //importPackage(com.itextpdf.text);
            //importPackage(com.itextpdf.text.Document);
            //importPackage(com.itextpdf.text.DocumentException) ;
            //importPackage(com.itextpdf.text.Paragraph);
            //importPackage(com.itextpdf.text.pdf.PdfWriter);

            Any ideas or a suggestion as to how I inventory the available packages in Mirth? Also when I attempted to import a non-existant package (DogBarf) I got an error yet there are no errors being generated when I use any and all of the import statement for iText. What's up with that? Does that mean that I'm not specifying Document correctly? Should it be iText.Document or something?

            Help!

            Ken

            Comment


            • #7
              OK, just have to ask this.

              I'm using the Mirth Connect 3.0 Beta. Is there possibly an issue with the Beta and iText?

              Ken

              Comment


              • #8
                Originally posted by kenlacrosse View Post
                OK, just have to ask this.

                I'm using the Mirth Connect 3.0 Beta. Is there possibly an issue with the Beta and iText?

                Ken
                Hmm, not that I'm aware of...
                Step 1: JAVA CACHE...DID YOU CLEAR ...wait, ding dong the witch is dead?

                Nicholas Rupley
                Work: 949-237-6069
                Always include what Mirth Connect version you're working with. Also include (if applicable) the code you're using and full stacktraces for errors (use CODE tags). Posting your entire channel is helpful as well; make sure to scrub any PHI/passwords first.


                - How do I foo?
                - You just bar.

                Comment


                • #9
                  Just a quick note. The same phenomena occurs with Mirth 2.X as well as 3.X.

                  Comment


                  • #10
                    Originally posted by kenlacrosse View Post
                    Just a quick note. The same phenomena occurs with Mirth 2.X as well as 3.X.
                    any luck?

                    Comment


                    • #11
                      FINALLY!

                      function createPDF(){
                      var outputfile = "C:/ldiag/dd2.pdf";

                      var myDocument = new Packages.com.lowagie.text.Document();
                      var outputstream = new Packages.java.io.FileOutputStream(outputfile);
                      var pdfwriter = new Packages.com.lowagie.text.pdf.PdfWriter.getInstanc e(myDocument, outputstream);

                      myDocument.open();
                      myDocument.add(new Packages.com.lowagie.text.Paragraph("hola"));
                      myDocument.close();
                      return;
                      }

                      createPDF();

                      -------------------------------------

                      now, how can I extract the text from a pdf file?

                      Comment


                      • #12
                        Can you explain where "lowagie" comes from in that class and how you determined you needed that? Thanks, -cp

                        Uh, nevermind. I found that he was the original writer of iText and the older versions use this com.lowagie class instead of com.itextpdf.

                        Thanks for the code.
                        Last edited by panickc; 07-10-2014, 07:06 AM.

                        Comment

                        Working...
                        X