Page 1 of 1

[Solved] Generating PDF without running OpenOffice engine

Posted: Fri Feb 02, 2018 2:48 am
by riddellg
Hello,

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?

thanks,
-Graeme

Re: Generating PDF without running OpenOffice engine

Posted: Mon Feb 05, 2018 1:05 pm
by Zizi64
- 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

 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"

Re: Generating PDF without running OpenOffice engine

Posted: Mon Feb 05, 2018 5:05 pm
by riddellg
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.
-gr

Code: Select all

/*
 * 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( 
        		XComponentLoader.class, 
        		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;
}