web stats
tiff to pdf conversion - Mirth Community

Go Back   Mirth Community > Mirth Connect > Support

Reply
 
Thread Tools Display Modes
  #1  
Old 05-22-2018, 09:05 AM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default 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
Reply With Quote
  #2  
Old 05-22-2018, 09:49 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 841
agermano is on a distinguished road
Default

Which version of mirth are you on?

Most java examples you find can be converted to javascript fairly easily.
Reply With Quote
  #3  
Old 05-22-2018, 11:03 AM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default

Mirth Connect Server 3.5.0.8232 is the version I am on.
Reply With Quote
  #4  
Old 05-22-2018, 11:04 AM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default

Mirth Connect Server 3.5.0.8232 is my current version
Reply With Quote
  #5  
Old 05-23-2018, 10:45 AM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default 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
Reply With Quote
  #6  
Old 05-24-2018, 06:47 PM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 841
agermano is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 05-25-2018, 11:07 AM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default 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 at 12:31 PM.
Reply With Quote
  #8  
Old 05-25-2018, 11:42 AM
agermano agermano is offline
Mirth Guru
 
Join Date: Apr 2017
Location: Indiana, USA
Posts: 841
agermano is on a distinguished road
Default

Not sure if I follow... what kind of source connector are you using? Javascript reader or file reader?
Reply With Quote
  #9  
Old 05-25-2018, 01:18 PM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default

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"]();
Reply With Quote
  #10  
Old 05-29-2018, 10:51 AM
mroberts mroberts is offline
OBX.1 Kenobi
 
Join Date: Jul 2010
Posts: 40
mroberts is on a distinguished road
Default error message

Wrapped java.lang.IllegalArgumentException: Bad endianness tag

I get this error when I run my code
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -8. The time now is 03:50 AM.


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