Convert DOCX to RTF with Python on Mac OS

Hi,


I am trying to get started with Aspose Words on my development machine which is Mac OSX Yosemite. I am using jpype to interface with the library over python.

I have successfully used the library to perform basic functions. I was able to convert a simple .docx file to .rtf. When I try to convert the same .docx file to .pdf, the python script will freeze. Additionally when I try to convert the aspose sample .doc file to .rtf, the script freezes in the same way. I have very little information to debug with because I am working in python and the script just freezes, it does not fail and produce an error.

My suspicion is it relates to availability of java libraries that aspose depends on (particularly JAI). Is there some way to troubleshoot this (i.e. by ensuring necessary libraries are available to aspose).

Thanks,
Gordon

I have managed to install Eclipse on the same machine and compile a simple example in java where everything works. Would still like to get this working with jpype. I am certain this is an issue with certain dependencies not being available in jpype - just not sure how to debug. If I knew what dependency is most likely missing I could write python scripts to check if those libraries are available through jpype.

Hi Gordon,

Can you please share your code and a sample to reproduce the issue? I was not able to notice this on Python with jpype. Please also share which version of Aspose.Words for Java are you using?

Best Regards,

Here is my code. Note that a 'test.pdf' file is created but it is zero bytes large. I have left the script running for several minutes before killing it and results are not any different.

def convert_doc_to_pdf(filepath):
# explicitly include all extension folders
extdir = '/System/Library/Java/Extensions/'
extdir += ':/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/'
extdir += ':/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/ext/'
extdir += ':' + settings.ASPOSE_JAVA_WORDS_PATH

# cannot use default JVM path because it points to java 1.6 which is 32bit
# `settings.JVM_PATH` which points to 1.8 - 64bit
jpype.startJVM(settings.JVM_PATH,
'-Djava.ext.dirs={}'.format(extdir))
Document = jpype.JClass('com.aspose.words.Document')

# test that various dependencies are available
# these statements all run without any errors
jpype.JClass('javax.crypto.Cipher')
jpype.JClass('javax.media.jai.AreaOpImage')
jpype.JClass('javax.imageio.stream.ImageInputStreamImpl')

doc = Document(filepath)
# api calls like this work without raising any error
print doc.getPageCount()

# this works
doc.save('test.rtf')

# this causes the script to hang
doc.save('test.pdf')

Hi Gordon,

Sorry, I was not able to see the issue at my end. I can convert simple as well as complex document to PDF at my end. JAI or other libraries are not required in case of PDF conversion; JAI is required if you want to convert to images.

Can you please share which version of Aspose.Words are you using and also share the input document? Please also try with a different version of Java e.g. JRE 7 and share how it goes (although it is working with both JDK 7 and 8 at my end).

Best Regards,

Can you tell me what version of jpype you are using? I am using 0.6.1. I have a tried a few different documents. When I use the template document that is in all of your examples, it will freeze when converting to RTF and PDF. When I use a simple document that only has a few lines of text and no special formatting, converting to RTF works converting to PDF freezes.


Do you know of any way that I can collect better debugging information from the java virtual machine when calling via jpype?

I managed to get the JVM to print out debug information on what classes are being loaded. The log is huge but here are the last few lines before it freezes:


[Loaded sun.util.logging.LoggingSupport$1 from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.logging.LoggingProxy from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.logging.LoggingProxyImpl from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.util.logging.LoggingSupport$2 from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.awt.AppContext$GetAppContextLock from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.misc.JavaAWTAccess from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded sun.awt.AppContext$6 from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap$Values from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap$IdentityHashMapIterator from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]
[Loaded java.util.IdentityHashMap$ValueIterator from /Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/rt.jar]

I have done further debugging. The process seems to hang when calling the following method:


[Dynamic-linking native method java.lang.ProcessEnvironment.environ … JNI]

Is there an environment variable I need to define? I have not yet applied a license to my aspose library - would that affect anything?

Ok I have solved the issue. If you start jpype with the following arguments it works:

extdir = ‘/System/Library/Java/Extensions/’
extdir += ‘:/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/’
extdir += ‘:/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/jre/lib/ext/’
extdir = ‘:’ + settings.ASPOSE_JAVA_WORDS_PATH

jpype.startJVM(jpype.getDefaultJVMPath(),
‘-Djava.awt.headless=true’,
‘-Dawt.toolkit=sun.awt.HToolkit’,
‘-Djava.ext.dirs={}’.format(extdir))

See this post for reference:

You should either document this on your python jpype help page or add autodetection to cover this edge case in the words library.

Gordon

Hi Gordon,

Good to know that the issue has been resolved and thanks for letting us know. We will document this point for future reference in case anyone else faces the same issue.

Best Regards,