views:

27

answers:

2

Currently we use OpenOffice to grab bookmarks in a template file document and replace them with content from our DB via Java. The lines of code that actually save the file look like this...

  XStorable storable = UnoRuntime.queryInterface(XStorable.class, document);


        // Save as Word 97 Document
        PropertyValue[] properties = new PropertyValue[1];
        PropertyValue property = new PropertyValue();
        property.Name = "FilterName";
        property.Value = FORMAT_WORD_97;
        properties[0] = property;
        storable.storeAsURL(saveFileURL, properties);

We want to directly write the file to the servlet response outputstream, does anybody know of a way to directly get the document as a byte array or inputstream via OpenOffice's UNO api in Java?

+1  A: 

This depends on the implementation of the UNO API. We were able to do this with PDF,

    OutputStream os = response.getOutputStream();

    PropertyValue[] properties = new PropertyValue[2];
    PropertyValue property = new PropertyValue();
    property.Name = "FilterName";
    property.Value = FORMAT_WORD_97;
    properties[0] = property;
    PropertyValue streamProp = new PropertyValue();
    streamProp.Name = "OutputStream;
    streamProp.Value = os;
    properties[1] = streamProp;

    storable.storeAsURL("private:stream", properties);
ZZ Coder
A: 

I would suggest saving the file locally first (from the UNO API) then streaming the result from your java code before deleting the [temp] file. The reason for this is that you can separate a problem with the generation of the document by OpenOffice from your delivery to the client. For example, if you're document fails to generate, you can produce an error without concerns for a partially written response streamed to a client. Also, if you haven't looked at tools already, you might want to look at Docmosis which provides redundancy, performance optimisation and data-merging functions. It can render directly to a stream that you supply (and presumably takes care of the partial-streamed-result issue).

jowierun