brite, a MVC Framework for jQuery

Java: JODConverter 3.x Getting Started

April 9th, 2009 by jeremychone

Requirements:

  • OpenOffice installed and OpenOffice started (e.g., OpenOffice in the system tray on Windows)
  • Java

Quick steps to test:

  1. Download the latest JODConverter 3.x from google code.
  2. Unzip the jar file
  3. Do a quick test:
    java -jar lib/jodconverter-${version}.jar test.odt test.pdf
  4. you should see something like
    C:appsjodconverter-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:UsersJEREMY~1AppDataLocalTemp.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:

  1. 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
  2. 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)

  1. 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.
  2. 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.

See Also

18 Responses to “Java: JODConverter 3.x Getting Started”

  1. Laurent Says:

    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

  2. jeremychone Says:

    @Laurent, nop, sorry, I do not use it to generate PDF/A.

  3. Shervin Asgari Says:

    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.

  4. Shervin Asgari Says:

    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

  5. Michael Greifeneder Says:

    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);

  6. Kay Ramme of Sun has created “ODF@WWW,” an ODF Wiki « Wave A Dead Chicken Says:

    [...] http://www.bitsandpix.com/entry/java-jodconverter-3x-getting-started/ [...]

  7. Avatar Oyunları Says:

    @jeremy, I gave a try to JODConverter 3 -

  8. chandar Says:

    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

  9. Francisco Diaz Says:

    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.

  10. Francisco Diaz Says:

    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.

  11. Pear Says:

    hello,i want to ask a question,whether or not JODConverter can convert doc to docx?

    waiting for your reply!thank you

  12. jeremychone Says:

    @Pear, I do not think so.

  13. Eiffy Chen Says:

    Hi,

    When I trun on EncryptFile and DocumentOpenPassword, the Printing and Changes setting would not working.

    Any ideas?
    Thanks in advance.

  14. Pear Says:

    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 ?

  15. uday Says:

    Hi,

    I am trying to use jodconverter as jar file as shown in point (3) in this post. I need to convert my ppt file to pdf (PDF1-A) but by default it is always pdf version 0. Looks like jodconverter jar acccpets ‘registry’ as a parameter but not “SelectPdfVersion”.

    Any idea how to pass this option to the jar file? How can i convert the doc to pdf only using jar command but not any java code?

    Thanks

  16. jeremychone Says:

    @uday Sorry, long time I have not played with jodconverter.

  17. uday Says:

    @jeremychone no issues, I could figure it out. Just sharing it in case some one may find it useful. Need to pass -r to jodconverter in which SelectPdfVersion value needs to be set to 1. An example file is present in jodconverter’s downloads (document-formats.js).

    sample document-formats.js file content –

    [
    {
    "name": "Portable Document Format",
    "extension": "pdf",
    "mediaType": "application/pdf",
    "storePropertiesByFamily": {
    "DRAWING": {"FilterName": "draw_pdf_Export", "FilterData": {"SelectPdfVersion": 1, "ReduceImageResolution": true, "MaxImageResolution": 400}},
    "SPREADSHEET": {"FilterName": "calc_pdf_Export","FilterData": {"SelectPdfVersion": 1, "ReduceImageResolution": true, "MaxImageResolution": 400}},
    "PRESENTATION": {"FilterName": "impress_pdf_Export","FilterData": {"SelectPdfVersion": 1, "ReduceImageResolution": true, "MaxImageResolution": 400}},
    "TEXT": {"FilterName": "writer_pdf_Export", "FilterData": {"SelectPdfVersion": 1, "ReduceImageResolution": true, "MaxImageResolution": 400}}
    }
    }]

  18. george Says:

    hi

    i have the following issue : i try to convert an xml file over linux operating system using jodconverter and openoffcie but i receive the error Exception in thread “main” org.artofsolving.jodconverter.office.OfficeException: could not load document: XML.xml.

    Using root from OS is working ok, i think there is a resoruce that is skyping , sorry for my english