Page 1 of 1

Exporting to string instead of saving to disk

PostPosted: Fri Jan 17, 2014 8:44 pm
by davide
Hi,

i'm developing an eclipse plugin that uses docx4j to produce docx documentation. What i need is getting a string rapresentation of the output docx file, and not saving it do disk. I need something like

WordprocessingMLPackage wordMLPackage = new ...

[...]

String docxFile = wordMLPackage.toString()

How can i do something like this?

Re: Exporting to string instead of saving to disk

PostPosted: Fri Jan 17, 2014 9:35 pm
by jason
You could get a byte[] representation of the docx, or you could get the "Flat OPC XML" representation as a string.

You can use docx4j 3.0's facade (org.docx4j.Docx4J) for both of these:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
        /**
         *  Save a Docx Document to an OutputStream
         */
   
        public static void save(WordprocessingMLPackage wmlPackage, OutputStream outStream, int flags) throws Docx4JException {
                if (flags == FLAG_SAVE_FLAT_XML) {
                        JAXBContext jc = Context.jcXmlPackage;
                        FlatOpcXmlCreator opcXmlCreator = new FlatOpcXmlCreator(wmlPackage);
                        org.docx4j.xmlPackage.Package pkg = opcXmlCreator.get();
                        Marshaller marshaller;
                        try {
                                marshaller = jc.createMarshaller();
                                NamespacePrefixMapperUtils.setProperty(marshaller,
                                                NamespacePrefixMapperUtils.getPrefixMapper());                 
                                marshaller.marshal(pkg, outStream);                            
                        } catch (JAXBException e) {
                                throw new Docx4JException("Exception marshalling document for output: " + e.getMessage(), e);
                        }
                }
                else {
                        SaveToZipFile saver = new SaveToZipFile(wmlPackage);
                        saver.save(outStream);
                }
        }

 
Parsed in 0.016 seconds, using GeSHi 1.0.8.4

Re: Exporting to string instead of saving to disk

PostPosted: Fri Jan 17, 2014 10:22 pm
by davide
I've tried this

Code: Select all
var baos = new ByteArrayOutputStream();
Docx4J.save(wordMLPackage, baos, Docx4J.FLAG_SAVE_ZIP_FILE);
String output = baos.toString


but it doesn't work. I know it seems useless but, to save the document, my plugin export to docx (and other formats) the content of output string. So i can't use the save() method, even if it works :? :roll:

Re: Exporting to string instead of saving to disk

PostPosted: Fri Mar 21, 2014 10:39 pm
by AndersG
Hope this helps: This had me stumped for quite a while :( , but then I compared the saved PDF with the one returned from code and saw that the character encoding was wrong. You cannot use toString() here, at least not what I know. Instead, do this:

Code: Select all
            
byte [] ba;
ba = ((ByteArrayOutputStream)os).toByteArray();
response.getOutputStream().write(ba);


The catch here (in my case where the same servlet is supposed to return either HTML or PDF) is to open your out's wisely. If you open a PrintWriter out = response.getWriter(); then you cannot open a binary stream.

It might even be possible to write os directly, but I have not had the time to try, yet.

Re: Exporting to string instead of saving to disk

PostPosted: Sat Mar 22, 2014 7:14 am
by jason
Yes, ordinarily you can/should write your docx directly to servlet output stream.

Re: Exporting to string instead of saving to disk

PostPosted: Sat Mar 22, 2014 8:09 am
by AndersG
So, you mean:

Code: Select all
response.getOutputStream().write(os);


? If so, I must test nest week.

Re: Exporting to string instead of saving to disk

PostPosted: Sun Mar 23, 2014 7:12 am
by jason
Using docx4j 3.0's facade (org.docx4j.Docx4J), save(wordMLPackage, response.getOutputStream(), ..