我想做Docxtemplater.js在浏览器上工作。
不幸的是,我对 Node.js、Browserify.js、“npm”、“bower” 以及所有那些漂亮又酷的现代 JS 库中出现的东西一无所知。我想了解它们,但在未来......现在我只想让这个 Docxtemplater.js 工作。
在页面底部,他们展示了如何使用 npm 执行此操作,但我不想要那样,所以我转到 Quickstart In The Browser 。我一定做错了什么,因为它不起作用。
这就是我所做的:
我创建了一个名为
testpage.html
的文件,其中包含以下内容:<html><body> <script src="build/docxtemplater.js"></script> <script src="vendor/FileSaver.min.js"></script> <script src="vendor/jszip-utils.js"></script> <script> var loadFile = function(url,callback) { JSZipUtils.getBinaryContent(url,callback); } loadFile("examples/tagExample.docx", function(err, content) { if (err) { throw e }; doc = new Docxtemplater(content); doc.setData({ "first_name": "Hipp", "last_name": "Edgar", "phone": "0652455478", "description": "New Website" }); //set the templateVariables doc.render(); //apply them (replace all occurences of {first_name} by Hipp, ...) out=doc.getZip().generate({type:"blob"}); //Output the document using Data-URI saveAs(out,"output.docx"); }); </script> </body></html>
我创建了一个文件夹
build
并将从 here 下载的文件docxtemplater.js
放在那里。 ,特别是原始文件 docxtemplater.v2.1.3.min.js (当然,我将其重命名为docxtemplater.js
)。我创建了一个文件夹
vendor
并将文件FileSaver.min.js
和jszip-utils.js
放在那里,这两个文件均取自 here .我创建了一个文件夹
examples
并将文件tagExample.docx
放入其中,取自 here .
这正是我所做的。我没有添加任何其他文件或文件夹或任何内容。
然后,我在 Windows 7 上使用 Firefox 47.0.1 打开文件 testpage.html
。打开控制台后,我收到以下错误消息:
ReferenceError:Docxtemplater 未定义
- 在此行:doc = new Docxtemplater(content);
怎么了?如何解决这个问题?
最佳答案
我找到了解决方案:只需将 new Docxtemplater(content)
替换为 new Docxgen(content)
即可。
只有当 downloading the JS build directly from github 时才会发生这种情况,就像我一样。
我opened an issue关于它,现在这是 properly documented in Readme.md :
If you download the JS from there, you should use new Docxgen() instead of new Docxtemplater(), because I do not want to bring in a breaking change on a minor version change in the docxtemplater-build repository.
“Docxgen”似乎是该库的旧名称,现在更名为“Docxtemplater”。
关于javascript - 如何在不了解 Node/Browserify/npm 等知识的情况下使 Docxtemplater.js 在浏览器上工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38779470/