使用mammoth.js将docx文件转换成html
前言
最近接到一个需求,要求是把docx文档转换成html,显示在页面上,翻了好多资料,尝试了iframe嵌套,但问题是会自动下载,也不会显示html。于是继续搜索,找到了mammoth,一个将docx文件转换成html的东西,最后完美的解决了问题,下面谈谈使用心得吧!
实现步骤
1 首先将源代码下载了到本地,传送门:http://www.github.com/mwilliamson/mammoth.js
2 打开browser-demo文件夹下面的index.html,比较重要的是这个html引入的mammoth.browser.js
<script src="../mammoth.browser.js"></script>
1
注意:一开始你会发现运行这个页面的时候会发现报错,找不到这个文件,原因是没有执行
makefile,因为只有执行makefile才会生成这个js文件。
你可以使用make setup执行这个文件,当然如果不支持这个命令,还有下面的方法。
我们先看看这个文件的内容:
.PHONY: test mammoth.browser.js npm-install
test:
npm test
setup: npm-install mammoth.browser.min.js
npm-install:
npm install
mammoth.browser.js:
node_modules/.bin/browserify lib/index.js --standalone mammoth -p browserify-prepend-licenses > $@
mammoth.browser.min.js: mammoth.browser.js
node_modules/.bin/uglifyjs mammoth.browser.js -c > $@
1
显而易见,这个文件其实只需要执行以下三个命令就可以生成我们要的js文件:
1 npm install, 2 node_modules/.bin/browserify lib/index.js --standalone mammoth -p browserify-prepend-licenses > mammoth.browser.js, 3 node_modules/.bin/uglifyjs mammoth.browser.js -c > mammoth.browser.min.js
1
注意:将$@替换成mammoth.browser.min.js(我是在gitbush下面执行的,因为之前在webstorm中执行会失败)
此时这个js文件已经有了,你可以正常运行这个html
转换docx
此时选择本地的docx文件进行转换,docx完美的转换成了html。
是不是有点东西呢?小老弟~
————————————————
可参考:https://blog.csdn.net/qq_20074443/article/details/89335845
可参考(好):https://www.cnblogs.com/DZzzz/p/11405846.html
版权声明:本文为CSDN博主「我在杭州跟你发抛」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_20074443/article/details/89335845