打印HTML
代码演示
import {usePrinter} from "lpp";
const {print} = usePrinter();
const html= `<div>hello world</div>`
printer.print({
data: html,
type: 'html',
preview: true,
})
当然您也可以获取某个组件的html:
如果您的 html 是这样:
<div id="printObj">
<div>hello world</div>
<div>hello world</div>
</div>
var aa=document.getElementById("printObj")
//我们可以通过innerHTML获取到它的html
console.log(aa.innerHTML)
var htmlContent=aa.innerHTML
printer.print({
data: htmlContent,
type: 'html',
preview: true,
})
完整的示例代码
<html>
<body>
<div id="printObj">
<div>hello world</div>
<div>hello world</div>
<div>hello world</div>
</div>
<button onclick="printHandler">点击我打印</button>
<script src="https://unpkg.com/lpp@1.0.2/dist/lpp.js"></script>
<script type="application/javascript">
function printHandler(){
const html= document.getElementById("printObj").innerHTML
lpp.print({
data: html,
type: 'html',
preview: true,
})
}
</script>
</body>
</html>