使用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
docx转换html(mammoth)

<script src="../mammoth.browser.js"></script>

注意:一开始你会发现运行这个页面的时候会发现报错,找不到这个文件,原因是没有执行
makefile,因为只有执行makefile才会生成这个js文件。

docx转换html(mammoth)

你可以使用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 > [email protected]

mammoth.browser.min.js: mammoth.browser.js
	node_modules/.bin/uglifyjs mammoth.browser.js -c > [email protected]

显而易见,这个文件其实只需要执行以下三个命令就可以生成我们要的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

注意:将[email protected]替换成mammoth.browser.min.js(我是在gitbush下面执行的,因为之前在webstorm中执行会失败)
此时这个js文件已经有了,你可以正常运行这个html

转换docx

此时选择本地的docx文件进行转换,docx完美的转换成了html。
docx转换html(mammoth)
docx转换html(mammoth)
是不是有点东西呢?小老弟~

相关文章:

  • 2021-06-29
  • 2021-08-03
  • 2022-01-15
  • 2022-02-09
  • 2021-11-14
  • 2022-01-22
  • 2022-12-23
  • 2021-07-11
猜你喜欢
  • 2022-12-23
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-02
相关资源
相似解决方案