• 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参数是二维码的内容

    实现效果:

  • 相关阅读:
    2022西式面点师(技师)考试练习题及答案
    MongoDB文档(二)
    VUE3-Cesium(3dtiles加载与设置)
    06【工厂设计模式】
    「Python循环结构」利用for循环输出信息和求阶乘
    LLVM学习入门(1):Kaleidoscope语言及词法分析
    milvus 迅速搭建
    LeetCode 23. 合并 K 个升序链表
    Python中直接赋值、浅拷贝和深拷贝的区别
    从零开始学安卓笔记:添加ImageButton后APP闪退问题记录
  • 原文地址:https://blog.csdn.net/hyupeng1006/article/details/136682727