Sidebar

How do I create a PDF when OBX-5 is in RTF format?

0 votes
2.3K views
asked Feb 1, 2015 by marc-m-9513 (570 points)

2 Answers

0 votes
 
Best answer

Here is a function to convert RTF to PDF. This does not rely on any external installs. 

function convertRtfToPdf(rtfString) {
   // NOTE: To use this function the following libraries need to be added as external libraries in QIE
   // flying-saucer-core-9.1.16.jar (http://central.maven.org/maven2/org/xhtmlrenderer/flying-saucer-core/9.1.16/flying-saucer-core-9.1.16.jar)
   // flying-saucer-pdf-itext5-9.1.16.jar (http://central.maven.org/maven2/org/xhtmlrenderer/flying-saucer-pdf-itext5/9.1.16/flying-saucer-pdf-itext5-9.1.16.jar)
   // itextpdf-5.5.13.jar (http://central.maven.org/maven2/com/itextpdf/itextpdf/5.5.13/itextpdf-5.5.13.jar)
   // jtidy-r938.jar (http://central.maven.org/maven2/net/sf/jtidy/jtidy/r938/jtidy-r938.jar)
   
   // STEP 1: use native java to convert RTF to HTML
   var pane = new javax.swing.JEditorPane();
   pane.setContentType("text/rtf");
   var kikRtf = pane.getEditorKitForContentType("text/rtf");
   var stringReader = new java.io.StringReader(rtfString);
   kikRtf.read(stringReader, pane.getDocument(), 0);
   kikRtf = null;
   var kitHtml = pane.getEditorKitForContentType("text/html");
   var stringWriter = new java.io.StringWriter();
   kitHtml.write(stringWriter, pane.getDocument(), 0, pane.getDocument().getLength());
   
   // STEP 2: use jTidy to conver the dirty HTML to clean XHTML
   var tidy = new org.w3c.tidy.Tidy();
   tidy.setXHTML(true);
   var jTidyOutput = new java.io.ByteArrayOutputStream();
   var jTidyInput = new java.io.ByteArrayInputStream(stringWriter.toString().getBytes());
   tidy.parseDOM(jTidyInput, jTidyOutput);   
   jTidyOutput.close();
   var jTidyBytes = jTidyOutput.toByteArray();
   
   // STEP 3: use flying-saucer-pdf-itext5 and itextpdf to convert the XHTML to PDF
   var iTextOutput = new java.io.ByteArrayOutputStream();
   var renderer = new org.xhtmlrenderer.pdf.ITextRenderer();
   renderer.setDocumentFromString(new java.lang.String(jTidyBytes));
   renderer.layout();
   renderer.createPDF(iTextOutput);
   iTextOutput.close();
   return iTextOutput.toByteArray();
}

   NOTE: To use this function the following libraries need to be added as external libraries in QIE
   o flying-saucer-core-9.1.16.jar (http://central.maven.org/maven2/org/xhtmlrenderer/flying-saucer-core/9.1.16/flying-saucer-core-9.1.16.jar)
   o flying-saucer-pdf-itext5-9.1.16.jar (http://central.maven.org/maven2/org/xhtmlrenderer/flying-saucer-pdf-itext5/9.1.16/flying-saucer-pdf-itext5-9.1.16.jar)
   o itextpdf-5.5.13.jar (http://central.maven.org/maven2/com/itextpdf/itextpdf/5.5.13/itextpdf-5.5.13.jar)
   o jtidy-r938.jar (http://central.maven.org/maven2/net/sf/jtidy/jtidy/r938/jtidy-r938.jar)
   

answered Dec 12, 2018 by brandon-w-8204 (33,270 points)
+1 vote

This only works with .43 version or older. This is due to the Jar files not being maintained.

You could take the contents from OBX-5 and then use a java jar from some source in order to convert from rtf into pdf. 

 

Below is an example using openoffice. You would need to have open office installed on your computer and import the jar files into Qvera.

 

// This mapping uses the jodconverter-2.2.2 library found at

//   http://www.artofsolving.com/opensource/jodconverter

 

// put the following 5 jars into the QIE\lib\jodconverter-2.2.2\ folder

//   jodconverter-2.2.2.jar

//   juh-3.0.1.jar

//   jurt-3.0.1.jar

//   ridl-3.0.1.jar

//   unoil-3.0.1.jar

// and then configure QIE to include that directory using the

//   'Manage External Libraries' button under 'System Configuration'

 

// RTF content

var rtf = message.getNode('/');

 

// connect to an OpenOffice.org instance running on port 8100

var connection = new com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection(8100);

var outputStream = new java.io.ByteArrayOutputStream();

try {

   connection.connect();

 

   // convert RTF to PDF

   var inputStream = new java.io.ByteArrayInputStream(new java.lang.String(rtf).getBytes());

   var registry = new com.artofsolving.jodconverter.DefaultDocumentFormatRegistry();

   var converter = new com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter(connection, registry);

   converter.convert(inputStream, registry.getFormatByFileExtension("rtf"), outputStream, registry.getFormatByFileExtension("pdf"));

} finally {

   // close the connection

   connection.disconnect();

}

 

// create output filename as {PID-5.1}_{OBX-14}_{DocTypeName}.pdf

var filename = "c:\\temp\\" + source.getNode('PID-5.1') + '_' + source.getNode('OBX-14') + '.pdf';

var file = new java.io.File(filename);

if (file.exists()) {

   qie.deleteFile(file);

}

 

// write bytes out to file

var out = new java.io.FileOutputStream(file, false);

var bytes = outputStream.toByteArray();

for (var i=0 ; i < bytes.length ; i++) {

   out.write(bytes[i]);

}

out.close();

 

// log file saved

qie.info('saved to ' + filename);

answered Feb 2, 2015 by michael-h-5027 (14,350 points)
edited Dec 12, 2018 by brandon-w-8204
...