在 Vue 2 中实现 “点击下载条码 → 打开新窗口预览 → 自动唤起浏览器打印” 的功能

在 Vue 2 中实现 "点击下载条码 → 打开新窗口预览 → 自动唤起浏览器打印" 的功能

实现效果

javascript 复制代码
  1.模版中编写按钮绑定事件
   <!-- 操作列(下载按钮) -->
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-printer"
              @click="handlePrint(scope.row.barcodeUrl)"
            >
              打印
            </el-button>
          </template>
        </el-table-column>  


2.在script中编写逻辑
//打印操作 barcodeDownloadUrl是图片地址
    handlePrint(barcodeDownloadUrl) {
      if (barcodeDownloadUrl) {
        this.handlePrintQrCode(barcodeDownloadUrl);
      } else {
        this.$message.error("暂没有生成条形码");
      }
    },

    handlePrintQrCode(qrImageUrl) {
      try {
        const printWindow = window.open("", "_blank");
        if (!printWindow) {
          this.$message.error("浏览器阻止了弹出窗口,请允许弹出后重试");
          return;
        }

        const printContent = `
          <!DOCTYPE html>
          <html lang="zh-CN">
          <head>
            <meta charset="UTF-8">
            <title>二维码打印页</title>
            <style>
              * { margin: 0; padding: 0; box-sizing: border-box; }
              body {
                width: 100vw;
                height: 100vh;
                display: flex;
                justify-content: center;
                align-items: center;
                background: #fff;
                padding: 0;
                margin: 0;
              }
                @page {
                                margin: 0 !important;
                                size: 80mm 40mm;
                            }
                            
                            body, html {
                                margin: 0 !important;
                                padding: 0 !important;
                                width: 80mm !important;
                                height: 40mm !important;
                                overflow: hidden !important;
                                transform: translateY(0mm);
                            }
                            
                            @page {
                                margin: 0 !important;
                                -webkit-print-color-adjust: exact;
                            }
                            
                            body {
                                -webkit-print-color-adjust: exact;
                            }
              .print-qr-wrapper {
                width: ${80}mm;
                height: ${40}mm;
                border: 1px solid #eee;
                display: flex;
                justify-content: center;
                align-items:center;
                margin: 0 !important;
                padding: 0 !important;
              }
              .print-qr-img {
                width: 100%;
                height: 100%;
                object-fit: contain;
              }
            </style>
          </head>
          <body>
            <div class="print-qr-wrapper">
              <img 
                class="print-qr-img" 
                src="${qrImageUrl}" 
                alt="二维码"
                onload="window.focus()"
              >
            </div>
          </body>
          </html>
        `;

        printWindow.document.write(printContent);
        printWindow.document.close();

        printWindow.onafterprint = () => {
          printWindow.close();
        };

        setTimeout(() => {
          printWindow.print();
        }, 300);
      } catch (error) {
        this.$message.error("打印功能异常,请刷新页面重试");
      }
    },
相关推荐
格砸11 分钟前
从入门到辞职|从ChatGPT到OpenClaw,跟上智能时代的进化
前端·人工智能·后端
Live000001 小时前
在鸿蒙中使用 Repeat 渲染嵌套列表,修改内层列表的一个元素,页面不会更新
前端·javascript·react native
柳杉1 小时前
使用Ai从零开发智慧水利态势感知大屏(开源)
前端·javascript·数据可视化
兆子龙1 小时前
从高阶函数到 Hooks:React 如何减轻开发者的心智负担(含 Demo + ahooks 推荐)
前端
狗胜1 小时前
测试文章 - API抓取
前端
三小河1 小时前
VS Code 集成 claude-code 教程:告别海外限制,无缝对接国内大模型
前端·程序员
jerrywus1 小时前
前端老哥的救命稻草:用 Obsidian 搞定 Claude Code 的「金鱼记忆」
前端·agent·claude
球球pick小樱花1 小时前
游戏官网前端工具库:海内外案例解析
前端·javascript·css
前端Hardy2 小时前
干掉 Virtual DOM?尤雨溪开始"强推" Vapor Mode?
vue.js·vue-router