[Solved] Generating PDF without running OpenOffice engine

Creating a macro - Writing a Script - Using the API

[Solved] Generating PDF without running OpenOffice engine

Postby riddellg » Fri Feb 02, 2018 2:48 am


As I understand it from the documentation the way to generate a PDF from an open office text document using pure Java code, using no GUI, ie without the GUI driving Export as PDF, is to use the SDK API to make a call to a separately running OpenOffice engine via the API over HTTP. True?

I'm just getting familiar with the UNO / IDL / c++ codebase - is the PDF Filter in the c++ code, or Java code? Would it be feasible to run the ODF->PDF rendition entirely locally in a single Java JVM?

I've seen other pure Java implementations of ODF->PDF (ie xdocreport with itext underpinnings) but I'm thinking using the native OpenOffice capabilities would be more capable & more accurate. I'm hoping to do it pure Java using the OpenOffice PDF Filter approach. Is this feasible?

Last edited by riddellg on Mon Feb 05, 2018 5:14 pm, edited 2 times in total.
OpenOffice 4.1 on Windows
Posts: 6
Joined: Mon Jan 15, 2018 5:45 pm

Re: Generating PDF without running OpenOffice engine

Postby Zizi64 » Mon Feb 05, 2018 1:05 pm

- You can use a third party virtual PDF-printer software, or

- you can write a code for opening a document with the AOO/LO but without appearing the User Interface and the document itself. use the hidden property in the openPropertyes part of your code:

Code: Select all   Expand viewCollapse view
Dim OpenProperties(1) As New com.sun.star.beans.PropertyValue
   OpenProperties(0).Name = "Hidden"
   OpenProperties(0).Value = true
   OpenProperties(1).Name = "Password"
   OpenProperties(1).Value = "Some secret words"
Tibor Kovacs, Hungary; LO6.1.6 on Win7-10 x64Prof.
PortableApps, winPenPack: LO3.3.0-6.2.5; AOO4.1.6
Please, edit the initial post in the topic: add the word [Solved] at the beginning of the subject line - if your problem has been solved.
User avatar
Posts: 8147
Joined: Wed May 26, 2010 7:55 am
Location: Budapest, Hungary

Re: Generating PDF without running OpenOffice engine

Postby riddellg » Mon Feb 05, 2018 5:05 pm

Thankyou Tibor, as you suggested I was able to drive it as follows (Java code).

Note to others doing this in Java to read the "First Steps" doc for guidance on putting the 4 required jars in a UserLibrary and using a 32-bit JVM for compatibility with the OO DLLs. Specifically on my original question it's clear this method exits the JVM and leverages/requires an installed OpenOffice instance.

Code: Select all   Expand viewCollapse view
* Reliant on xContext = Bootstrap.bootstrap() call made in constructor..
public boolean export(String odfPath, String pdfPath) throws BootstrapException, Exception {

      XMultiComponentFactory xServiceManager = xContext.getServiceManager();
       String available = (xServiceManager != null ? "available" : "not available");
        LOG.info("remote ServiceManager is " + available);
        if (xServiceManager == null)
           throw new RuntimeException("Unable to get XMultiComponentFactory from OpenOffice engine");

        pdfPath = pdfPath.replace('\\', '/');
      // Get access to the component loader..
        XComponentLoader xLoader = (XComponentLoader)UnoRuntime.queryInterface(
              xServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", xContext));         
        // Load the document..
        PropertyValue[] loadProps = new PropertyValue[2];
        loadProps[0] = new PropertyValue();
        loadProps[0].Name = "ReadOnly";
        loadProps[0].Value = new Boolean(true);
        loadProps[1] = new PropertyValue();
        loadProps[1].Name = "Hidden";
        loadProps[1].Value = new Boolean(true);
        XComponent xDocument = (XComponent) xLoader.loadComponentFromURL("file:///" + odfPath,
              "_blank1", 0, loadProps);    
        LOG.info("doc loaded - {}", odfPath);
      // Grab the storable interface..
      XStorable xStorable = (XStorable) UnoRuntime.queryInterface(XStorable.class, xDocument);
      // Export the PDF using the pdf writer filter..
      // Note, the PDF writer supports many properties, all the same properties you see in the popup
      // dialog such as lossless compression, image resolutions etc. For now taking defaults...
      PropertyValue[] aMediaDescriptor = new PropertyValue[1];
      aMediaDescriptor[0] = new PropertyValue();
      aMediaDescriptor[0].Name = "FilterName";
      aMediaDescriptor[0].Value = "writer_pdf_Export";
      xStorable.storeToURL("file:///" + pdfPath, aMediaDescriptor);
      LOG.info("pdf written - {}", pdfPath);
      return true;
OpenOffice 4.1 on Windows
Posts: 6
Joined: Mon Jan 15, 2018 5:45 pm

Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests