Page 1 of 1

How to Export to Browser instead of Saving to Directory?

PostPosted: Fri May 15, 2009 4:59 pm
by openeric
Hi. I have docx4j working well with my application, and I am quite pleased.

However I have a new requirement and can't seem to get it working.
The requirement:
When a user clicks on a button, they are prompted to either open the word file or browse to save it to their system.

What I'm doing:
1) Writing to a String
Code: Select all
String wordXML = "";
StringWriter sw = new StringWriter();
   org.docx4j.convert.out.xmlPackage.XmlPackage xmlPackage = new org.docx4j.convert.out.xmlPackage.XmlPackage(wordMLPackage);
   org.docx4j.xmlPackage.Package pkg = xmlPackage.get();   
   JAXBContext jc = Context.jcXmlPackage;
   Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(pkg, sw);
wordXML = sw.toString();


2) In my servlet, I'm using a PrintWriter to write to serlvet response
Code: Select all
PrintWriter pWriter = null;   
      
      try{
         pWriter = response.getWriter();   
         response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
         response.setHeader("Content-Disposition","attachment; filename=" + "test" + ".doc");
         response.setContentLength((int)wordString.length());
         pWriter.print(wordString);
         
      }


Result:
The Word file is opening, but with the xml structure in purple bubbles (see attachment), and the bytes of the jpegs written out as text. I'm wondering if I'm missing something small or going about this completely the wrong way. Any help would be greatly appreciated.

Re: How to Export to Browser instead of Saving to Directory?

PostPosted: Sat May 16, 2009 6:26 am
by jason
Hi Eric

With the approach you are currently taking, you are sending what is now being called the "Flat OPC" single XML file representation of the Word document to the browser. (Current SVN renames the package to reflect this)

That's fine, and should work. Hmm, perhaps the problem is:

There is an interesting characteristic of the binary data that is encoded into a base 64 string: the string must be broken into lines of 76 characters, and there must not be a line break at the beginning or end of the data. No big deal, but we must take this into consideration when converting OPC to Flat OPC and back again. [1]


We don't bother with the 76 characters bit, because I haven't found this to be a problem before.

An alternative approach that should work for you is to send a docx, rather than Flat OPC.

To do that, do:

Code: Select all
SaveToZipFile saver = new SaveToZipFile(wmlPackage);
saver.save(response.getOutputStream);


cheers .. Jason

[1] http://blogs.msdn.com/ericwhite/archive ... ormat.aspx

Re: How to Export to Browser instead of Saving to Directory?

PostPosted: Mon May 18, 2009 10:05 pm
by openeric
Awesome!, thanks.

Worked just as I hoped. Prompts to open in default program (Word) or save to system:
Code: Select all
response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");
response.setHeader("Content-Disposition","attachment; filename=" + "test" + ".docx");

SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(response.getOutputStream());