将.docx格式文件转成html,uniapp使用u-parse展示

使用mammoth。

1、在index.html中加入:

html 复制代码
<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=edge" />
		<meta name="viewport" content="width=device-width, initial-scale=1.0" />
		<title>docx转html</title>
	</head>
	<body>
		<input id="document" type="file" />
		<div id="output" class="well"></div>
	</body>
	<script src="https://cdn.bootcdn.net/ajax/libs/mammoth/1.4.17/mammoth.browser.min.js"></script>
	<script type="text/javascript">
		document.getElementById("document").addEventListener("change", readFileInputEventAsArrayBuffer, false);
 
		function displayResult(result) {
			let html = result.value;
			console.log(html); //获得html,类似:'<p style="text-align: center; line-height: 2;"></p>'
			let newHTML = html
				.replace(//g, "")
				.replace("<h1>", '<h1 style="text-align: center;">')
				.replace(/<table>/g, '<table style="border-collapse: collapse;">')
				.replace(/<tr>/g, '<tr style="height: 30px;">')
				.replace(/<td>/g, '<td style="border: 1px solid pink;">')
				.replace(/<p>/g, '<p style="text-indent: 2em;">');
			document.getElementById("output").innerHTML = newHTML;
		}
 
		function readFileInputEventAsArrayBuffer(event) {
			var file = event.target.files[0];
			var reader = new FileReader();
			reader.onload = function (loadEvent) {
				var arrayBuffer = loadEvent.target.result; //arrayBuffer
				mammoth.convertToHtml({ arrayBuffer: arrayBuffer }).then(displayResult).done();
			};
			reader.readAsArrayBuffer(file);
		}
	</script>
	<!-- 参考:https://github.com/mwilliamson/mammoth.js/ -->
</html>

2、从页面获得html内容。

3、uniapp中展示相关的用户协议、隐私协议之类:

javascript 复制代码
<template>
  <view>
    <u-parse :html="content" />
  </view>
</template>
相关推荐
chxii几秒前
Nginx 正则 location 指令匹配客户端请求的 URI
前端·nginx
qing222222222 分钟前
Linux:/var/log/journal 路径下文件不断增加导致根目录磁盘爆满
linux·运维·前端
Armouy7 分钟前
Nuxt.js 学习复盘:核心概念与实战要点
前端·javascript·学习
ZhaoJuFei18 分钟前
React生态学习路线
前端·学习·react.js
apcipot_rain35 分钟前
CSS知识概述
前端·css
837927397@QQ.COM37 分钟前
个人理解无界原理
开发语言·前端·javascript
冰暮流星40 分钟前
javascript之Dom查询操作1
java·前端·javascript
admin and root42 分钟前
XSS之Flash弹窗钓鱼
前端·网络·安全·web安全·渗透测试·xss·src
2501_915918411 小时前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
qq_381338501 小时前
[特殊字符] MonkeyCode AI 核心功能详解
前端