react-to-print打印

react-to-print打印

介绍

React-to-print 是一个用于在 React 应用中实现打印功能的库。它提供了一些方便的方法和组件,可以帮助你轻松地将页面内容导出为打印格式

安装

arduino 复制代码
    npm install react-to-print

使用

javascript 复制代码
    import ReactToPrint from 'react-to-print';
ini 复制代码
     const popupContentRef = useRef(null);

<PrinterOutlined>是标签
<ReactToPrint> 是一个按钮,用来触发打印操作。
trigger 属性用于指定触发打印操作的元素或组件。它可以是一个按钮、链接或其他可以点击的元素。当用户点击该元素时,会触发打印操作。trigger 属性接受一个函数,该函数必须返回一个 React 元素。常见的用法是将其设置为一个函数组件,返回一个按钮或链接,如示例中的 <PrinterOutlined/>
content 属性用于指定要打印的内容。它可以是一个组件或一个函数,该函数返回要打印的内容。要确保要打印的内容是可见的,并且在渲染时可以正常访问。在示例中,我们将 popupContentRef.current 传递给了 content 属性,以便将其打印出来。

xml 复制代码
<div className={styles.printerOutlined}>
    <div>
        <ReactToPrint
            trigger={() => <PrinterOutlined />}
            content={() => popupContentRef.current}
        ></ReactToPrint>
    </div>
</div>
xml 复制代码
<div ref={popupContentRef} id="popupContentRef" className={styles.modalContent}>
    ...需要打印内容
</div>

分页效果

css 复制代码
@media print {
  html, body {
    height: initial !important;
    overflow: initial !important; // 如果出现无法分页加入此属性
    -webkit-print-color-adjust: exact;
  }
}
相关推荐
云水一下2 小时前
从零开始!VMware安装Fedora Workstation 44桌面系统完整教程
前端
小码哥_常3 小时前
安卓黑科技:实现多平台商品详情页一键跳转APP
前端
killerbasd3 小时前
还是迷茫 5.3
前端·react.js·前端框架
不会敲代码14 小时前
TCP/IP 与前端性能:从数据包到首次渲染的底层逻辑
前端·tcp/ip
kyriewen4 小时前
奥特曼借GPT-5.5干杯,而你的Copilot正按Token收钱
前端·github·openai
AC赳赳老秦4 小时前
投标合规提效:用 OpenClaw 实现标书 / 合同自动审核、关键词校验、格式优化,降低废标风险
开发语言·前端·python·eclipse·emacs·deepseek·openclaw
kyriewen5 小时前
代码写成一锅粥?3个设计模式让你的项目“起死回生”
前端·javascript·设计模式
千寻girling5 小时前
《 Git 详细教程 》
前端·后端·面试
之歆6 小时前
DAY08_CSS浮动与行内块布局实战指南(下)
前端·css
yqcoder7 小时前
CSS Position 全解析:5 种定位模式详解
前端·css