将.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>
相关推荐
We་ct11 分钟前
LeetCode 106. 从中序与后序遍历序列构造二叉树:题解+思路拆解
前端·数据结构·算法·leetcode·typescript
Never_Satisfied25 分钟前
在HTML & CSS中,Animation 属性使用详解
前端·css·html
少云清27 分钟前
【UI自动化测试】9_web自动化测试 _元素等待
前端·web自动化测试
Never_Satisfied28 分钟前
在JavaScript / HTML中,模板克隆并添加监听的注意事项
前端·javascript·html
明月_清风36 分钟前
告别视口依赖:Container Queries 开启响应式组件的“后媒体查询”时代
前端·css
明月_清风38 分钟前
从样式表到渲染引擎:2026 年前端必须掌握的 CSS 架构新特性
前端·css
阿珊和她的猫9 小时前
前端应用首屏加载速度优化全攻略
前端·状态模式
Mike_jia10 小时前
LiteOps:轻量级CI/CD平台,重塑开发运维新体验
前端
浮游本尊10 小时前
React 18.x 学习计划 - 第十四天:实战整合与进阶收尾
前端·学习·react.js