let printStr = '<div align="center"><h1>打印内容标题</h1></div>';
let last = window.document.body.innerHTML;
window.document.body.innerHTML = printStr;
window.print();
window.document.body.innerHTML = last;
可以使用 innerHtml
,查看文档 https://developer.mozilla.org/zh-CN/docs/Web/API/Element/innerHTML
我不知道你的前端使用的什么框架,你拿到后端生成的html以后,我们定义一个变量来接收后端来的html,它本质上是一串字符串嘛
我们就叫它 htmlStr
Vue的写法是使用v-html
<div v-html="htmlStr"></div>
原生js的写法是
let dom =document.getElementById('你的需要放html内容的盒子的id');
dom.innerHtml=htmlStr
这样你就可以成功将后端生成的html渲染到前端了
调用浏览器打印的方法是 window.print();
如果我的回答对你有帮助,希望采纳一下哦