Announcement

Collapse
No announcement yet.

tiff to pdf conversion

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

  • tiff to pdf conversion

    I have everything working except for one thing

    I need to convert a tiff file that is on a local drive to a pdf

    I have seen several examples but it seems to involve me needing to create a new jar file which I do not know how to do.

    Can someone point me in the right direction

  • #2
    Which version of mirth are you on?

    Most java examples you find can be converted to javascript fairly easily.

    Comment


    • #3
      Mirth Connect Server 3.5.0.8232 is the version I am on.

      Comment


      • #4
        Mirth Connect Server 3.5.0.8232 is my current version

        Comment


        • #5
          convert this to use in mirth

          http://www.rgagnon.com/javadetails/java-0645.html

          something like this is exactly what I need to do. Can someone point me to the steps to get this into mirth

          Comment


          • #6
            I'm assuming you're using a File Reader and File Writer...

            Set up your source and destination connectors with inbound and outbound templates set to Raw.

            Make sure both your File Reader and Writer are set to binary mode.

            Set the Attachment Handler to Entire Message.

            In your destination transformer:

            Code:
            var Document = com.lowagie.text.Document;
            var PageSize = com.lowagie.text.PageSize;
            var Rectangle = com.lowagie.text.Rectangle;
            var PdfWriter = com.lowagie.text.pdf.PdfWriter;
            var RandomAccessFileOrArray = com.lowagie.text.pdf.RandomAccessFileOrArray;
            var TiffImage = com.lowagie.text.pdf.codec.TiffImage;
            
            var document = new Document(PageSize.LETTER, 0, 0, 0, 0);
            var baos = new java.io.ByteArrayOutputStream();
            writer = PdfWriter.getInstance(document, baos);
            document.open();
            cb = writer.getDirectContent();
            var comps = 0;
            var tiff = getAttachments().get(0).getContent();
            tiff = FileUtil.decode(new java.lang.String(tiff));
            var ra = new RandomAccessFileOrArray(tiff);
            comps = TiffImage.getNumberOfPages(ra);
            for (var c = 0; c < comps; ++c) {
                var img = TiffImage.getTiffImage(ra, c + 1);
                if (img != null) {
                    img.scalePercent(7200 / img.getDpiX(), 7200 / img.getDpiY());
                    document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
                    img.setAbsolutePosition(0, 0);
                    cb.addImage(img);
                    document.newPage();
                }
            }
            ra.close();
            document.close();
            
            msg = addAttachment(FileUtil.encode(baos.toByteArray()), 'application/pdf').getAttachmentId();
            I also set up a Mapper step for a connectorMap variable named pdfFileName. The mapping is $('originalFilename'), and the string replacement is ('\.tiff?$', '.pdf'). This is what I used for the File Writer filename.

            You could also do the javascript in the source transformer instead.

            Comment


            • #7
              conversion

              actually I am trying to this inside of the source file transformer.

              I have to pull a bunch of documents which are a mix of pdf's and txt then combine them into one pdf. I have the combining pdf's into 1 pdf working and the portion I am trying to get is when 1 of the files is a tiff convert it to pdf then I can combine all the pdf's into 1 and send it along in the destination. Once the tiff is converted to pdf write it back to the same directory on the drive so that it can be added into the pdf

              I will look through your code and see if I can adapt it into the transformer.

              I want to activate the tiff to pdf portion based on the file extension. Plan is to put the tiff to pdf conversion within a if statement so if extension is tiff convert to pdf and continue through the script
              Last edited by mroberts; 05-25-2018, 12:31 PM.

              Comment


              • #8
                Not sure if I follow... what kind of source connector are you using? Javascript reader or file reader?

                Comment


                • #9
                  Below is my code. I am attempting to get all document pages associated with an ID. They can be mixed bag of tiff and pdf. When it is a tiff I need to convert the tiff to pdf. Once I have all of pdf's copied over to the directory then I combine them into 1 pdf. I then take that single pdf and put it in a message. The code works fine when all the source files are pdf but I do not have the tiff to pdf working. If I take it out life is a great and it works fine. I need to get the section to convert the tiff to a pdf working.

                  Ignore the color tag in the code I was trying to highlight the pdf convert section
                  HTML Code:
                  //copy files to directory to convert to pdf
                  //
                  //Define variables
                  var dbConn;
                  var pagecount = $('totalpages');
                  var destdir = "C:\\Users\\user\\Desktop\\export\\";
                  channelMap.put("destdir",destdir);
                  var filelocname;
                  var filename;
                  var result1;
                  
                  //DB connection variable
                  var dbConn = databaseConnectionFactory.createDatabaseConnection('net.sourceforge.jtds.jdbc.Driver','jdbc:jtds:sqlserver://sql:1433/db','users','passwords');
                  
                  
                  //retrieve doc id that we are gatting
                  var doc_id = $('document_id')
                  //end variables
                  
                  //for loop to get all of documents
                        for(var counter = 1; counter <= $('page_count'); counter = counter + 1) 
                        {
                  
                  //file name and path
                  var sql = "select location+sub_location+file_name  FROM   page  WHERE  document_id = '" +$('document_id')+ "' and sequence_nbr = "+counter+" ";
                  var result1 = dbConn.executeCachedQuery(sql);
                  result1.next();
                  var filenameandpath = result1.getString(1);
                  
                  //actual file name
                  
                  var sql2 = "select file_name  FROM   page  WHERE  document_id = '" +$('document_id')+ "' and sequence_nbr = "+counter+" ";
                  var result2 = dbConn.executeCachedQuery(sql2);
                  result2.next();
                  var fname = result2.getString(1);
                  
                  //file extension
                  var sql3 = "select right(file_name, 4) FROM   page  WHERE  document_id = '" +$('document_id')+ "' and sequence_nbr = "+counter+" ";
                  var result3 = dbConn.executeCachedQuery(sql3);
                  result3.next();
                  var file_ext = result3.getString(1);
                  //assign variables for write
                  var srcFile = result1.getString(1);
                  var destDir = destdir+counter+"-"+doc_id+"."+file_ext;
                  
                  //Copy files to be copied
                  importPackage(java.io);
                  importPackage(org.apache.commons.io);
                  var inputFile = new File(srcFile);
                  var outputFile = new File(destDir);
                  
                  FileUtils.copyFile(inputFile, outputFile);
                  //end of move files
                  
                  
                  //if not pdf convert  in section below
                  [COLOR="Lime"]if(file_ext = "tif"){
                  var Document = com.lowagie.text.Document;
                  var PageSize = com.lowagie.text.PageSize;
                  var Rectangle = com.lowagie.text.Rectangle;
                  var PdfWriter = com.lowagie.text.pdf.PdfWriter;
                  var RandomAccessFileOrArray = com.lowagie.text.pdf.RandomAccessFileOrArray;
                  var TiffImage = com.lowagie.text.pdf.codec.TiffImage;
                  
                  var document = new Document(PageSize.LETTER, 0, 0, 0, 0);
                  var baos = new java.io.ByteArrayOutputStream();
                  writer = PdfWriter.getInstance(document, baos);
                  document.open();
                  cb = writer.getDirectContent();
                  var comps = 0;
                  var tiff = outputFile;
                  tiff = FileUtil.decode(new java.lang.String(tiff));
                  var ra = new RandomAccessFileOrArray(tiff);
                  comps = TiffImage.getNumberOfPages(ra);
                  for (var c = 0; c < comps; ++c) {
                      var img = TiffImage.getTiffImage(ra, c + 1);
                      if (img != null) {
                          img.scalePercent(7200 / img.getDpiX(), 7200 / img.getDpiY());
                          document.setPageSize(new Rectangle(img.getScaledWidth(), img.getScaledHeight()));
                          img.setAbsolutePosition(0, 0);
                          cb.addImage(img);
                          document.newPage();
                      }
                  }
                  ra.close();
                  document.close();
                  }[/COLOR]
                  //end of convert
                    
                      }
                  dbConn.close();
                  
                  // code to merge pdf's
                  //create guid for new file name
                  var v_guid;
                  var uuid = new String(UUIDGenerator.getUUID());
                  v_guid = uuid.replace(/-/g, "");
                  
                  //var uuid = guid();
                  var ut = new org.apache.pdfbox.util.PDFMergerUtility();
                     for(var pdfcounter = 1; pdfcounter <= $('page_count'); pdfcounter = pdfcounter + 1) 
                        {
                  //pdf merge section
                  ut.addSource(destdir+pdfcounter+"-"+doc_id+"."+file_ext);
                  
                  //New combined PDF to be sent
                        }
                  var fname_pdf = (destdir+v_guid+".pdf");      
                  ut.setDestinationFileName(destdir+v_guid+".pdf");
                  channelMap.put("fname_pdf",fname_pdf);
                  ut.mergeDocuments();
                  //end pdf utility
                  //var delfile= Packages.java.io.File(outputfile);
                  //delfile["delete"]();

                  Comment


                  • #10
                    error message

                    Wrapped java.lang.IllegalArgumentException: Bad endianness tag

                    I get this error when I run my code

                    Comment

                    Working...
                    X