Trouble saving docs with images

Aug 6, 2013 at 9:02 PM

I'm really excited to use the new JS APIs, and I've had good luck creating and editing documents without images. However, if I try and simply re-save a document with images, it is way bigger on disk and fails to open in OSX Pages with an "unknown error" error. (I'm running this in Node on v0.8.14 on Mountain Lion.) When trying to open the created file in OSX Word, it displays a 98-page long document consisting of Base64. (Which is possibly the incorrectly-encoded image?)

This is my first experience with OpenXML so maybe I'm missing something obvious. The trick of renaming the file to ".zip" then opening it also fails with the created doc, throwing a "Error 2 - No such file or directory" error.


Below is the CoffeeScript I'm using, you can see that it's really simple.
fs = require 'fs'
vm = require 'vm'
_ = require 'underscore'
argv = (require 'optimist').argv
util = require 'util'

include = (path) ->
  code = fs.readFileSync path, 'utf8'
  vm.runInThisContext code, path


DOMParser = require('xmldom').DOMParser;
Ltxml.DOMParser = DOMParser;

XAttribute = Ltxml.XAttribute;
XCData = Ltxml.XCData;
XComment = Ltxml.XComment;
XContainer = Ltxml.XContainer;
XDeclaration = Ltxml.XDeclaration;
XDocument = Ltxml.XDocument;
XElement = Ltxml.XElement;
XName = Ltxml.XName;
XNamespace = Ltxml.XNamespace;
XNode = Ltxml.XNode;
XObject = Ltxml.XObject;
XProcessingInstruction = Ltxml.XProcessingInstruction;
XText = Ltxml.XText;
XEntity = Ltxml.XEntity;
cast = Ltxml.cast;
castInt = Ltxml.castInt;

W = openXml.W;
NN = openXml.NoNamespace;
wNs = openXml.wNs;

buffer = fs.readFileSync argv.file
b64 = buffer.toString 'base64'

doc = new openXml.OpenXmlPackage b64

content = doc.saveToBase64()
outBuffer = new Buffer content, 'base64'
fs.writeFileSync argv.out, outBuffer