react
react-to-print - npm
import { useReactToPrint } from "react-to-print";
const printTableRef = useRef<div | null>(null);
const handlePrint = useReactToPrint({
content: () => printTableRef.current,
});
<Button key="button" type="primary" onClick={()=> handlePrint()}> 打印 </Button>
<div style={
{ display:'none',width: '100%', height: 'auto'}}>
<Table
style={
{ width: '100%', height: 'auto'}}
ref={printTableRef}
columns={columnsTable}
dataSource={tableData}
pagination={false}
bordered
/>
</div>
js
// # HTML代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p id="xz">This is Printer!</p>
<button onclick="printer()">打印</button>
</body>
<script>
// # js代码
function printer(){
var newWin = window.open('printer', '', '');
var xiaozhi = document.getElementById("xz").innerText; // 这里的id是要打印的标签里的id
newWin.document.write(xiaozhi);
newWin.document.location.reload();
newWin.print();
newWin.close();
}
</script>
</html>