Java: JODConverter 3.x Getting Started
April 9th, 2009 by jeremychoneRequirements:
- OpenOffice installed and OpenOffice started (e.g., OpenOffice in the system tray on Windows)
- Java
Quick steps to test:
- Download the latest JODConverter 3.x from google code.
- Unzip the jar file
- Do a quick test:
java -jar lib/jodconverter-${version}.jar test.odt test.pdf
- you should see something like
C:\apps\jodconverter-3.0-beta-1>java -jar lib/jodconverter-3.0-beta-1.jar test.odt test.pdf Apr 9, 2009 12:56:42 PM net.sf.jodconverter.office.OfficeProcess start INFO: starting process with acceptString 'socket,host=127.0.0.1,port=8100' and profileDir 'C:\Users\JEREMY~1\AppData\Local\Temp\.jodconverter_socket_host-127.0.0.1_port-8100' Apr 9, 2009 12:56:42 PM net.sf.jodconverter.office.OfficeProcess start INFO: started process; pid -1 Apr 9, 2009 12:56:47 PM net.sf.jodconverter.office.OfficeConnection connect INFO: connected: 'socket,host=127.0.0.1,port=8100' Apr 9, 2009 12:56:52 PM net.sf.jodconverter.office.OfficeConnection$1 disposing INFO: disconnected: 'socket,host=127.0.0.1,port=8100' Apr 9, 2009 12:56:53 PM net.sf.jodconverter.office.ManagedOfficeProcess doEnsureProcessExited INFO: process exited with code 0
Note: As you can see it works because it assumes that OpenOffice listen to the port 8100, which happen to be the case (how lucky!).
In a Java (Server) App:
- Copy the following .jar files in your webapp lib directory
lib/jodconverter-3.0-beta-1.jar
lib/juh-3.0.0.jar
lib/jurt-3.0.0.jar
lib/ridl-3.0.0.jar
lib/unoil-3.0.0.jar - Use the following code snippet:
OfficeManager officeManager = new ManagedProcessOfficeManager(); officeManager.start(); OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager); converter.convert(sourceFile,targetFile); officeManager.stop();
Best Practice (from JODConverter group)
- OfficeManager instance can and should be shared across all
threads. It should typically be started when your webapp starts, and
stopped when the webapp stops. It can be e.g. a singleton, or a Spring
bean, or other equivalent approach of your choice. - The OfficeDocumentConverter instance can also be shared, or you can
create a different instance per thread. It doesn’t really matter,
since all conversions will be delegated to the OfficeManager anyway.
April 21st, 2009 at 10:55 am
I gave a try to JODConverter 3 -
perfect for PDF-1.4
i’d like to convert an ODT to PDF/A - tried with 2.x release of jodconverter, playing on OOo “SelectPdfVersion” property (set to 1) has no effect >> i get a PDF 1.4 doc in output.
That’s why i wanted to try jodconverter 3 >> i try to play on outputFormat.setStoreProperties (still no effect)
Any idea? Thanks
April 26th, 2009 at 6:41 pm
@Laurent, nop, sorry, I do not use it to generate PDF/A.
May 20th, 2009 at 8:12 am
Hmm thats strange.
SelectPdfVersion set to 1 should have worked.
PDFXNONE = 0
PDFX1A2001 = 1
PDFX32002 = 2
PDFA1A = 3
PDFA1B = 4
Maybe you can try with other stuff. I am trying to convert to PDF/A myself and I am not sure if I successfully do this.
Please let me know Laurent if you manage to do this correctly.
May 25th, 2009 at 12:24 am
I finally figured it out.
My problem was that I had an extra space in the pdfOption definition. Other than that, SelectPdfVersion 1 worked just fine.
Remember also to set DocumentFormat.TEXT
January 7th, 2010 at 9:30 am
In order to generate PDF/A with JODConverter 3.x I changed the definition of PDF in constructor of DefaultDocumentFormatRegistry:
DocumentFormat pdf = new DocumentFormat(”Portable Document Format”, “pdf”, “application/pdf”);
Map map = new HashMap();
map.put(”FilterName”, “writer_pdf_Export”);
PropertyValue[] aFilterData = new PropertyValue[1];
aFilterData[0] = new PropertyValue();
aFilterData[0].Name = “SelectPdfVersion”;
aFilterData[0].Value = 1;
map.put(”FilterData”, aFilterData);
pdf.setStoreProperties(DocumentFamily.TEXT, map);
March 6th, 2010 at 12:07 pm
[...] http://www.bitsandpix.com/entry/java-jodconverter-3x-getting-started/ [...]
June 22nd, 2010 at 10:56 am
@jeremy, I gave a try to JODConverter 3 -
November 14th, 2010 at 6:51 pm
I am using below code (using JODConverter) to connect to open office and convert files from java web application. Every time the user wants to preview document, below code gets executed. The web application is running on a weblogic on windows server (production server).
connection = new SocketOpenOfficeConnection(8100);
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
connection.disconnect();
Started open office using - ” soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard ”
Problem:
’soffice.exe’ gets killed constanly and the application is throwing error “Problem to convert Doc/DocX resume. Please contact the support.”. Everytime I have to start soffice manually using above command and it works fine.
So I made soffice.exe as windows service. But still facing the same issue that exe gets killed sometimes and getting error. I have been breaking my heads for a week but no solution yet.
Can you please help??
Chandar
November 16th, 2010 at 5:44 am
Hi,
I got Shervin Asgari’s code from http://shervinasgari.blogspot.com/2010/08/migrating-from-jodconverter-2-to.html and I was triying to set additional export options as follows:
DocumentFormat format = new DocumentFormat(”PDF/A”, PDF, “application/pdf”);
Map properties = new HashMap();
properties.put(”FilterName”, “writer_pdf_Export”);
Map filterData = new HashMap();
filterData.put(”SelectPdfVersion”, this.PDFX1A2001);
filterData.put(”EncryptFile”, Boolean.TRUE);
filterData.put(”DocumentOpenPassword”, “1234″);
filterData.put(”Changes”, 0);
filterData.put(”EnableCopyingOfContent”, Boolean.FALSE);
filterData.put(”Printing”, 0);
properties.put(”FilterData”, filterData);
format.setStoreProperties(DocumentFamily.TEXT, properties);
The file encription is working so the PDF viewer asks for me to enter a password, but other optios such as Changes, EnableCopyingOfContent and Printing (described in the table from http://www.artofsolving.com/node/18)
still not work.
I also tried to use filterData as a PropertyValue array instead of a Map, as Michael Greifeneder suggested, but It still not work.
Any ideas?
Thanks in advance.
November 17th, 2010 at 4:34 am
I finally figured it out!
It is neccesary to set both RestrictPermissions and PermissionPassword as well in order to make security restrictions work:
filterData.put(”RestrictPermissions”, Boolean.TRUE); filterData.put(”PermissionPassword”, “whatever”);
Regards.
February 13th, 2011 at 12:54 am
hello,i want to ask a question,whether or not JODConverter can convert doc to docx?
waiting for your reply!thank you
February 25th, 2011 at 10:20 pm
@Pear, I do not think so.
March 10th, 2011 at 4:05 am
Hi,
When I trun on EncryptFile and DocumentOpenPassword, the Printing and Changes setting would not working.
Any ideas?
Thanks in advance.
April 11th, 2011 at 1:57 am
To jeremychone
Thanks you reply!
You are right ,i have tried to converter doc to docx ,it failed,and i found that odt cannot be convertered to docx,but odt can be congvertered to doc,why?Do i have wrong coding ?