Free Support Forum - aspose.com

RTF string to PDF

Hello,

We're using Perl and Java with Aspose.Words. We're trying to avoid disk i/o by passing the RTF string directly into the java function and then return the PDF document data. Nothing I try works. The resulting PDF document is always blank although it contains multiple pages. Sometimes it actually has the correct page count, just no text. I'd send you my current code but it is a mess right now because of all the things I've been trying. Could you please point me to, or supply, a simple example that would accomplish this? If you do need to see my code I can try to clean it up and post it.

Thank you!

Hi Mike,

Thanks for your inquiry. Could you please attach your input RTF string here for testing? I will investigate the issue on my side and provide you more information.

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;

Hi Mike,

It is nice to hear from you that you have solved your problem. We always appreciate positive feedback from our customers. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.