• React实现生成及打印二维码


    前言:react中一般生成二维码都是用react.qrcode,但是发现这玩意生成的是canvas,不是图片,打印的时候预览不出来。所以想进一切办法去把canvas转成图片,无奈拿不到这个canvas标签。最后还是用js的qrcode来生成二维码,这样生成的默认是base64位的图片,打印正常。

    使用到的两个组件:

    1、生成二维码

    yarn add qrcode

    2、打印

    yarn add react-print-html

     参考代码(部分代码,不可直接粘贴运行)

    1. import { Button, Modal, Table, TableProps } from 'antd';
    2. import { useRef, useState } from 'react';
    3. import Print from 'react-print-html'
    4. let QRCode = require('qrcode')
    5. export default (props: PropsType) => {
    6. const { children } = props;
    7. const intl = useIntl();
    8. let printTemp = useRef(null)
    9. const formRef = useRef<ProFormInstance>();
    10. const [routingList, setRoutingList] = useState<TableDataType[]>([]);
    11. const [tableLoading, setTableLoading] = useState(false);
    12. const [qrcodeImg, setQrcodeImg] = useState(undefined)
    13. const getCode =(lotNo: string) =>{
    14. QRCode.toDataURL(lotNo)
    15. .then((url: any) => {
    16. setQrcodeImg(url)
    17. })
    18. .catch((err: any) => {
    19. console.error(err)
    20. })
    21. }
    22. const getList = async () => {
    23. const res: any = await request({});
    24. if (res.data?.lot?.length) {
    25. setRoutingList(res.data.lot);
    26. getCode(res.data.lot[0].lotNo)
    27. } else {
    28. setRoutingList([])
    29. }
    30. setTableLoading(false);
    31. };
    32. const handlePrint = () => {
    33. setTimeout(() => {
    34. Print(printTemp.current)
    35. })
    36. };
    37. return (
    38. <>
    39. <div
    40. style={{ display: 'inline-block' }}
    41. onClick={() => {
    42. setIsModalOpen(true);
    43. // getList();
    44. }}
    45. >
    46. {children}
    47. div>
    48. <Modal
    49. open={isModalOpen}
    50. title={children}
    51. width="60%"
    52. onCancel={() => {
    53. setIsModalOpen(false);
    54. formRef.current?.resetFields();
    55. }}
    56. footer={null}
    57. >
    58. <ProForm>
    59. <ProFormItem>
    60. <Button type="primary" danger onClick={handlePrint} style={{ marginLeft: 20 }} disabled={!routingList.length}>
    61. {intl.formatMessage({
    62. id: 'component.option.print',
    63. defaultMessage: '打印',
    64. })}
    65. Button>
    66. ProFormItem>
    67. ProForm>
    68. <div ref={printTemp}>
    69. {routingList.map((item) => {
    70. return (
    71. <div key={item.id} style={{ marginTop: 30}}>
    72. <div style={{display: 'inline-block', verticalAlign: 'middle'}}>
    73. <img src={qrcodeImg} style={{width:"100px",height:"100px"}}>img>
    74. div>
    75. <div style={{display: 'inline-block', verticalAlign: 'middle'}}>
    76. <div>
    77. {intl.formatMessage({
    78. id: 'component.option.productionOrder.table.lotNo',
    79. defaultMessage: '号码一',
    80. })}: {item.lotNo}div>
    81. <div>
    82. {intl.formatMessage({
    83. id: 'component.option.part.table.drawingNo',
    84. defaultMessage: '号码二',
    85. })}: {item.drawingNo}div>
    86. <div>
    87. {intl.formatMessage({
    88. id: 'component.option.workOrder.table.productionOrderNo',
    89. defaultMessage: '号码三',
    90. })}: {item.productionOrderNo}div>
    91. div>
    92. div>
    93. )
    94. })}
    95. div>
    96. Modal>
    97. );
    98. };

    补充说明几点:

    1. QRCode.toDataURL(lotNo) 其中的lotNo参数是二维码的内容

    实现效果:

  • 相关阅读:
    【MySQL】CONCAT、CONCAT_WS、GROUP_CONCAT 函数用法
    Midjourney入门:AI绘画真的能替代人类的丹青妙笔吗?
    自然语言处理通用框架BERT原理解读
    设计模式之适配器模式
    归并排序精讲
    Mybatis-Plus开发提速器mybatis-plus-generator-ui
    自适应分组多级并发框架
    Linux Shell入门常用命令使用
    课程设计-天天象棋作弊软件判别
    视频美颜SDK,提升企业视频通话质量与形象
  • 原文地址:https://blog.csdn.net/hyupeng1006/article/details/136682727