- 下载 npm install --save react-to-print;
- 引用 import ReactToPrint from ‘react-to-print’;
- //触发按钮
<ReactToPrint
trigger={() => <a href="#">点此打印</a>}
content={() => this.componentRef}
/>
//打印内容
<div ref={el => (this.componentRef = el)}>
....内容
<div/>
- 将表格放入antd中的modal实现一次打印多张表格;
<Modal
title="打印付款通知书"
visible={printShow}
onOk={this.handleOk}
onCancel={handleCancel}
footer={
<div>
<ReactToPrint
trigger={() => <span>打印</span>}
content={() => this.componentRef}
/>
</div>
//将打印功能的一个按钮放入到FOOTER里
}
width={1200}
>
- 将ref 需要打印的内容放入外层DIV包着,然后使用stle={{pageBreakAfter:‘always’}} 实现样式分页打印
<div ref={el => (this.componentRef = el)} >
{listData.length > 0 ? listData.map((e, index) => (
<div className="cardHtml" style={{ pageBreakAfter: 'always' }} key={index}>
<Card title="付款通知书" bordered={false} >
<Form>