文档生成仅在第一次时有效 [英] document generation only works the first time

查看:118
本文介绍了文档生成仅在第一次时有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在HTML5移动应用程序中使用openxml在移动设备上生成Word文档. 总的来说,openxml可以正常工作,但我一直在努力解决一个无法解决的问题. 在我启动应用程序后,文档生成仅在第一次时有效.这次,我可以打开并查看文档.重新启动应用程序意味着: -从开发机器重新部署 -从任务窗格中删除该应用程序(推开;我假定该应用程序已被删除了吗?)

I'm using openxml in my HTML5 mobile app to generate word documents on the mobile device. In general openxml works fine and straight forward, but I'm struggling with an annyoing problem. The document generation only works the first time after I've started the app. This time I can open and view the document. Restart the app means: - Redeploy from development machine - Removing the app from the task pane (pushing aside; I assume the app is removed then?)

第二次收到消息,表明文档已损坏,我无法查看文件

The second time I get the message the document is corrupted and I'm unable to view the file

更新: 在没有设置断点的情况下运行连接到远程调试器的应用程序时,无法重现此行为.这样,我总能得到一份工作文件.

UPDATE: I can't reproduce this behaviour when I'm running the app connected to the remote debugger without having a breakpoint set. Doing it this way I always get a working document.

无论我是否对文档进行任何更改,我都不会有所作为.只需打开并保存即可重现此错误.

I doesn't make a difference wether I do any changes on the document or not. Simply open and saving reproduce this error.

经过一些研究,我发现正在工作的docx.zip文件与损坏的文件的结构相同.它们也具有相同的文件长度.但是在损坏的docx中,有些文件我发现某些文件的CRC错误/无效.尝试从zip中获取损坏的文件时,请参见此处的示例.其他文件正在按预期方式工作.

After doing some research I've found that structure of the docx.zip file of the working and the corrupt file is the same. They also have the same file length. But in the corrupt docx there are some files I've found some files having a wrong/invalid CRC. See here an example when trying to get a corrupt file out of the zip. Other files are working as expected.

此文件的属性是-> (工作版本中的CRC为:44D3906C)

The properties for this file are-> (CRC in a working version is: 44D3906C)

用于处理文档模板的代码:

Code for processing the doc-template:

            /*
             * Process the template
             */
             function processTemplate(doc64, callback) 
             {
                "use strict";
                 console.log("PROCESS TEMPLATE");            
                var XAttribute = Ltxml.XAttribute;
                var XCData = Ltxml.XCData;
                var XComment = Ltxml.XComment;
                var XContainer = Ltxml.XContainer;
                var XDeclaration = Ltxml.XDeclaration;
                var XDocument = Ltxml.XDocument;
                var XElement = Ltxml.XElement;
                var XName = Ltxml.XName;
                var XNamespace = Ltxml.XNamespace;
                var XNode = Ltxml.XNode;
                var XObject = Ltxml.XObject;
                var XProcessingInstruction = Ltxml.XProcessingInstruction;
                var XText = Ltxml.XText;
                var XEntity = Ltxml.XEntity;
                var cast = Ltxml.cast;
                var castInt = Ltxml.castInt;

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

                var doc = new openXml.OpenXmlPackage(doc64);

                 // add a paragraph to the beginning of the document.
                 var body = doc.mainDocumentPart().getXDocument().root.element(W.body);
                 var tpl_row = ((doc.mainDocumentPart().getXDocument().descendants(W.tbl)).elementAt(1).descendants(W.tr)).elementAt(2);
                 var newrow = new XElement(tpl_row);

                 doc.mainDocumentPart().getXDocument().descendants(W.tbl).elementAt(1).add(newrow);

                 // callback(doc);    
                 var mod_file = null;
                 var newfile;
                 var path;

                 if (doc != null && doc != undefined ) {
                         mod_file = doc.saveToBlob();
                         // Start writing document
                         path = "Templates";
                         newfile = "Templates/Bau.docx";

                         console.log("WRITE TEMPLATE DOCUMENT");
                         fs.root.getFile("Templates/" + "MyGenerated.docx", {create: true, exclusive: false}, 
                         function(fileEntry) 
                         {  
                                fileEntry.createWriter(
                                function(fileWriter)
                                {
                                    fileWriter.onwriteend = function(e) {
                                        console.log("TEMPLATE DOCUMENT WRITTEN:"+e.target.length);
                                    };
                                    fileWriter.onerror = function(e) {
                                        console.log("ERROR writing DOCUMENT:" + e.code + ";" + e.message);
                                    };
                                    var blobreader = new FileReader();
                                    blobreader.onloadend = function() 
                                    {          
                                        fileWriter.write(blobreader.result);                   // reader.result contains the contents of blob as a typed array
                                    };                                                                                              
                                    blobreader.readAsArrayBuffer(mod_file);                         
                                }, 
                                null);                              
                         }, null);                   
                 };

有什么想法我在做什么错吗?

Any ideas what I'm doing wrong?

推荐答案

对我来说起作用的是,更改了向文档中添加图像(部件)的方式.我使用二进制"类型将图像添加到文档中.我将其更改为"base64"
因此,我将来源更改为:

What made it work for me, was changing the way of adding images (Parts) to the document. I was using the type "binary" for adding images to document. I changed this to "base64"
So I changed the source from:

mydoc.addPart( "/word/"+reltarget, openXml.contentTypes.png, "binary", fotodata );  // add Image Part to doc

收件人:

mydoc.addPart( "/word/"+reltarget, openXml.contentTypes.png, "base64", window.btoa(fotodata) );                 // add Image Part to doc

这篇关于文档生成仅在第一次时有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆