Hello,
Thank you for your reply. I think I have it figured out now. We were passing in base64 encoded RTF string when we did not need to do that. To hopefully help others, here is the perl module we've come up with. I'm certainly open to improvement suggestions should you have any.
Thanks.
use MIME::Base64;
use Inline (
Java => <<'END_OF_JAVA_CODE',
import java.io.*;
import com.aspose.words.*;
import org.apache.commons.codec.binary.*;
public class rtfStringConvert {
public String doRtfStringConvert (String rtfString, String newFormat) throws Exception{
// License stuff
License license = new License();
license.setLicense("/var/java/Aspose.Words.lic");
// Convert the string into an array of bytes and pass it into a new memory stream
ByteArrayInputStream rtfStream = new ByteArrayInputStream(rtfString.getBytes());
// This may be useful in the future but disabling for now
//FileFormatInfo info = FileFormatUtil.detectFileFormat(rtfStream);
//if (info.getLoadFormat() != LoadFormat.RTF) {
//throw new Exception("The RTF string passed is invalid.");
//System.err.println("***invalid format***");
//}
//rtfStream.reset();
// Create new Document from inputStream
Document rtfDoc = new Document(rtfStream);
// Set Fonts
FontSettings.setFontsFolder("/var/java/fonts/", false);
// Create TOC if it exists
rtfDoc.updateFields();
// Create output stream
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
// Convert to the desired format
if ( newFormat.equals("pdf") ) {
rtfDoc.save(outStream, SaveFormat.PDF);
} else if ( newFormat.equals("doc") ) {
rtfDoc.save(outStream, SaveFormat.DOC);
} else if ( newFormat.equals("docx") ) {
rtfDoc.save(outStream, SaveFormat.DOCX);
}
// Convert outStream data to Base64 String so we can pass back to Perl
String outString = new String(Base64.encodeBase64Chunked(outStream.toByteArray()));
return outString;
}
}
END_OF_JAVA_CODE
CLASSPATH => '/var/java/Aspose.Words.jdk16.jar:/var/java/commons-codec-1.6.jar',
SHARED_JVM => 1,
DIRECTORY => '/var/path/to/tmp/dir/',
);
package AsposeWordsInline;
sub convertRtfString {
my ($rtfString, $format) = @_;
my $inlineClass = new rtfStringConvert();
my $newFile = $inlineClass->doRtfStringConvert($rtfString, $format);
return MIME::Base64::decode_base64($newFile);
}
1;