element-ui打印多列表格
时间: 2023-09-02 11:05:39 浏览: 225
你可以使用第三方库 `jspdf` 和 `html2canvas` 来实现多个表格的打印。具体步骤如下:
1. 安装依赖:
```bash
npm install jspdf html2canvas --save
```
2. 在需要打印的页面引入依赖:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.5/jspdf.min.js"></script>
```
3. 编写打印函数:
```javascript
function print() {
const pdf = new jsPDF();
const options = {
background: 'white',
scale: 3
};
// 获取所有要打印的表格
const tables = document.querySelectorAll('.el-table');
// 遍历每个表格,生成对应的canvas,并添加到pdf中
for (let i = 0; i < tables.length; i++) {
html2canvas(tables[i], options)
.then((canvas) => {
const imgData = canvas.toDataURL('image/png');
pdf.addImage(imgData, 'PNG', 0, 0);
if (i !== tables.length - 1) {
pdf.addPage();
}
});
}
// 打印pdf
pdf.autoPrint();
pdf.output('dataurlnewwindow');
}
```
4. 在需要打印的按钮上绑定打印函数:
```html
<el-button type="primary" @click="print">打印</el-button>
```
注意:如果表格中有分页,需要将所有分页的数据合并成一个表格再进行打印。
阅读全文
相关推荐


















