【问题标题】:How to print specific component in React?如何在 React 中打印特定组件?
【发布时间】:2021-08-13 16:32:41
【问题描述】:

我正在尝试使用 window.print() 为 POS-58 系列打印机打印发票模式组件,但我无法打印特定组件,因为页眉也在打印。

export default function InvoiceModal({ orderInfo }) {
 const [show, setShow] = useState(false);

 const handleClose = () => setShow(false);
  const handleShow = () => setShow(true);

 return(
      <Modal
        className="invoice-modal px-5"
        show={show}
        onHide={handleClose}
      >
        <Modal.Header className='d-flex justify-content-between'> 
          <Modal.Title>Invoice</Modal.Title>
          <Button
            className="invoice-print-button"
            variant="contained"
            onClick={() => window.print()}
          >
            Print
          </Button>
        </Modal.Header>
        <Modal.Body id="testInvoicePage">
          <div className="d-flex flex-column justify-content-center">
            <div className="invoice-heading d-flex flex-column text-center">
              <img
                style={{ width: "25%", display: "block", margin: "5px auto" }}
                src={orderInfo.salon.basic.logo} //images.MAIN[0].url
                alt="no data availabel"
              />
              <h4 style={{ fontSize: "23px" }} className="pt-3 pb-3">
                {orderInfo.salon.basic.name || "Salon Name"}
              </h4>
              <span>
                {orderInfo.salon.basic.location.address +
                  " " +
                  orderInfo.salon.basic.location.city.name +
                  " ," +
                  orderInfo.salon.basic.location.state.name ||
                  "Chattarpur, Delhi-110068"}
              </span>
              <span>GST Number - 22AAAAA0000A1Z5</span>
            </div>
            <Dropdown.Divider />
            <div className="d-flex justify-content-between">
              <span style={{fontSize:'14px'}}>
                <b>Invoice No:</b> 123/2021
              </span>
              <span style={{fontSize:'14px'}}>
                <b>Invoice Date:</b>{" "}
                {moment(orderInfo.basic.confirmed_at.date).format("Do MMM") ||
                  "13 May 2021"}
              </span>
            </div>
            <Dropdown.Divider />
            <Table>
              <thead>
                <tr>
                  <th style={{ fontSize: "16px", color:'#000000' }}>#</th>
                  <th style={{ fontSize: "16px", color:'#000000' }}>
                    Item
                  </th>
                  <th style={{ fontSize: "16px", color:'#000000' }}>Price</th>
                </tr>
              </thead>
              <tbody>
                {orderInfo.items.map((item, i) => {
                  //paddingRight: "250px",
                  return (
                    <tr key={i}>
                      <td style={{color:'#000', fontSize: "16px"}}>{i + 1}</td>
                      <td style={{color:'#000', fontSize: "16px"}}>{item.service.name}</td>
                      <td style={{color:'#000', fontSize: "16px"}}>{item.service.price.price_net}</td>
                    </tr>
                  );
                })}
              </tbody>
            </Table>
          </div>
        </Modal.Body>
      </Modal>
 )

}
  

因此,当我单击“打印”按钮时,会打开打印对话框选项,其中包含模态正文和模态标题,页面底部有一些额外的空间。

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    你可以使用

    @meida print {
    *{
    display:none
    }
    #print-content {
    display: auto
    }
    }
    

    在 css 中并显示非其他元素

    【讨论】:

    • 如何去掉底部空格?
    • @media print {*{display: none} #print-side: {display: block}}
    • 我可以删除模态页眉,但打印页面仍然很大,向下空间很大,如图所示
    • 给我发邮件帮助你 najafi.it@gmail.com
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2015-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多