当前位置: 首页 > news >正文

Demo: 前端生成条形码并打印

前端生成条形码并打印

在这里插入图片描述

安装依赖:
npm i print-js // 打印
npm i jsbarcode // 生成条形码

<template>
    <div id="printContent" style="display: none;">
        <div id="elTable">
            <div class="name">名称:{{ printInfo.name }}</div>
            <div class="name">品牌:{{ printInfo.brand }}</div>
            <div class="name">型号:{{ printInfo.model }}</div>
            <!-- 显示条形码 -->
            <canvas class="barcode" ref="barcodeRef"></canvas>
        </div>
    </div>
    <el-button type="success" @click="pdfPrint">打印pdf文件</el-button>
</template>
  
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import printJS from "print-js";
import JsBarcode from 'jsbarcode'; // 导入JsBarcode库

const barcodeRef = ref(null) // 条形码实例
// 打印信息
const printInfo = ref({
    name: '大疆无人机',
    brand: '南方',
    model: 'model'
})

// 生成条形码
const generateBarcode = () => {
    const canvas = barcodeRef.value; // 获取到canvas元素
    // 传递参数生成条形码
    JsBarcode(canvas, "No.202401250212118948", {
        format: "CODE128",//条形码的格式
        width: 2, //线宽
        height: 48, //条码高度
        lineColor: "#000", //线条颜色
        displayValue: true, //是否显示文字
        margin: 2, //设置条形码周围的空白区域
    })
}

// 打印
const pdfPrint = () => {
    printJS({
        printable: 'elTable', // HTML内容
        type: "html", // 打印类型
        header: "", // '表单名称',
        targetStyles: ["*"],
        style: "@page {margin:1mm 1mm};", // 可选-打印时去掉眉页眉尾
        ignoreElements: ["no-print"], // 接受打印父 html 元素时应忽略的 html id 数组。
        properties: null,
    })
}

onMounted(() => {
    generateBarcode()
})
</script>
  
<style lang="scss">
#elTable {
    width: 200px;

    .barcode {
        width: 192px;
    }
}
</style>

相关文章:

  • 第十四届蓝桥杯C组题目 三国游戏
  • Spring Retry机制详解
  • 鸿鹄工程项目管理系统em Spring Cloud+Spring Boot+前后端分离构建工程项目管理系统
  • vue3封装el-pagination分页组件
  • Python 一些常见的字符串操作
  • 【VS Code+Verilog+Vivado使用】(1)常用插件
  • 【python】爬取百度热搜排行榜Top50+可视化【附源码】【送数据分析书籍】
  • CSAPP shelllab
  • [docker] Docker 网络
  • qemu + vscode图形化调试linux kernel
  • openssl3.2/test/certs - 056 - all DNS-like CNs allowed by CA1, no SANs
  • 项目解决方案:市小区高清视频监控平台联网整合设计方案(上)
  • python flask request教程
  • JS-Window常见对象
  • 精要图示:园区金融数字化服务蓝图,以园区为支点推动信贷业务增长
  • Linux:简单聊聊线程调度
  • 百度搜索智能精选是什么东西、怎么加入?
  • Linux实验记录:使用RAID(独立冗余磁盘阵列)
  • PyInstaller 将 Python 程序生成可直接运行的程序
  • Emergent Abilities of Large Language Models 机翻mark
  • 贵州省总工会党组成员、副主席梁伟接受审查调查
  • 肖峰读《从塞北到西域》︱拉铁摩尔的骆驼
  • 黑灰产工作室为境外诈骗集团养号引流,冒充美女与男性裸聊后敲诈勒索
  • 面对非专业人士,科学家该如何提供建议
  • 工行回应两售出金条疑似有杂质:情况不属实,疑似杂质应为金条售出后的外部附着物
  • 梵蒂冈选出新教皇,外交部:望新教皇推动中梵关系不断改善