功能:在electron打印条形码或者二维码

在百度搜索找不到合适的博客与插件,就在github上找到一个插件

electron-pos-printer

可以一键打印图片、文本、二维码、条形码、表格

不用安装其他多余插件,亲测十分好用

1、安装

npm install electron-pos-printer
yarn add electron-pos-printer

2、使用

在main主进程中引入

const {PosPrinter} = require("electron-pos-printer");

在render渲染进程中引入

const {PosPrinter} = require('electron').remote.require("electron-pos-printer");

定义参数

const printOptions = {preview: false,               // 打印预览width: '80px',               //  宽度margin: '0 0 0 0',            // 外边距copies: 1,                    // 打印页数printerName: '',        // 打印机名称timeOutPerLine: 400,    //超时时间pageSize: { height: 301000, width: 71000 }  // 页面大小
}

注意:

页面大小需要根据你的纸张大小自己调试

打印机名称通过remote.getCurrentWebContents().getPrinters()获取

开始打印二维码

/*** 去打印* value:二维码数据* textLeft:文本左边距距离*/_toPrint(value, textLeft) {const printerName = new Store().get('printerName')if (!printerName) {//请先选择打印机this.showSelPrintModal = truereturn}const data = [{type: 'qrCode',value,height: 100,width: 100,displayValue: true, // Display value below barcodestyle: `margin-left:80px;`,},{type: 'text',value,style: `margin-left:${textLeft}px;`,},]printOptions = { ...printOptions, printerName }PosPrinter.print(data, printOptions).then(() => {}).catch((error) => {console.error('打印错误', error)})},

说明:先获取打印机名称,然后调用插件打印,边距需要调式这个距离

打印成果(纸张是700*500)

打印条形码也是如此(修改一下data)

 const data = [{type: 'barCode',value,height: 110,width: 1,displayValue: true, // 是否显示数值fontsize: 16,style: `margin-left:${left}px;`,},]

大功告成!!!

还有表格、图片我就不演示了,直接上作者demo

const {PosPrinter} = require("electron-pos-printer");
const path = require("path");const options = {preview: false,               // Preview in window or printwidth: '170px',               //  width of content bodymargin: '0 0 0 0',            // margin of content bodycopies: 1,                    // Number of copies to printprinterName: 'XP-80C',        // printerName: string, check with webContent.getPrinters()timeOutPerLine: 400,pageSize: { height: 301000, width: 71000 }  // page size
}const data = [{type: 'image',                                       path: path.join(__dirname, 'assets/banner.png'),     // file pathposition: 'center',                                  // position of image: 'left' | 'center' | 'right'width: '60px',                                           // width of image in px; default: autoheight: '60px',                                          // width of image in px; default: 50 or '50px'},{type: 'text',                                       // 'text' | 'barCode' | 'qrCode' | 'image' | 'tablevalue: 'SAMPLE HEADING',style: `text-align:center;`,css: {"font-weight": "700", "font-size": "18px"}},{type: 'text',                       // 'text' | 'barCode' | 'qrCode' | 'image' | 'table'value: 'Secondary text',style: `text-align:left;color: red;`,css: {"text-decoration": "underline", "font-size": "10px"}},{type: 'barCode',value: 'HB4587896',height: 12,                     // height of barcode, applicable only to bar and QR codeswidth: 1,                       // width of barcode, applicable only to bar and QR codesdisplayValue: true,             // Display value below barcodefontsize: 8,},{type: 'qrCode',value: 'https://github.com/Hubertformin/electron-pos-printer',height: 55,width: 55,style: 'margin: 10 20px 20 20px'},{type: 'table',// style the tablestyle: 'border: 1px solid #ddd',// list of the columns to be rendered in the table headertableHeader: ['Animal', 'Age'],// multi dimensional array depicting the rows and columns of the table bodytableBody: [['Cat', 2],['Dog', 4],['Horse', 12],['Pig', 4],],// list of columns to be rendered in the table footertableFooter: ['Animal', 'Age'],// custom style for the table headertableHeaderStyle: 'background-color: #000; color: white;',// custom style for the table bodytableBodyStyle: 'border: 0.5px solid #ddd',// custom style for the table footertableFooterStyle: 'background-color: #000; color: white;',},{type: 'table',style: 'border: 1px solid #ddd',             // style the table// list of the columns to be rendered in the table headertableHeader: [{type: 'text', value: 'Animal'}, {type: 'image', path: path.join(__dirname, 'icons/animal.png')}],// multi dimensional array depicting the rows and columns of the table bodytableBody: [[{type: 'text', value: 'Cat'}, {type: 'image', path: './animals/cat.jpg'}],[{type: 'text', value: 'Dog'}, {type: 'image', path: './animals/dog.jpg'}],[{type: 'text', value: 'Horse'}, {type: 'image', path: './animals/horse.jpg'}],[{type: 'text', value: 'Pig'}, {type: 'image', path: './animals/pig.jpg'}],],// list of columns to be rendered in the table footertableFooter: [{type: 'text', value: 'Animal'}, 'Image'],// custom style for the table headertableHeaderStyle: 'background-color: #000; color: white;',// custom style for the table bodytableBodyStyle: 'border: 0.5px solid #ddd',// custom style for the table footertableFooterStyle: 'background-color: #000; color: white;',},
]PosPrinter.print(data, options).then(() => {}).catch((error) => {console.error(error);});

很好用的一个打印工具插件!!!

electron打印条形码、二维码相关推荐

  1. 【C#】最全单据打印(打印模板、条形码二维码、字体样式、项目源码)

    系列文章 [C#]编号生成器(定义单号规则.固定字符.流水号.业务单号) 本文链接:https://blog.csdn.net/youcheng_ge/article/details/12912978 ...

  2. 个人用户永久免费,可自动升级版Excel插件,使用VSTO开发,Excel催化剂功能第12波-快速生成、读取、导出条形码二维码...

    根据指定的内容生成对应的条形码或二维码,在如今移动互联网时代,并不是一件什么新鲜事,随便百度一下,都能找到好多的软件或在线网站可以帮我们做到,但细想一下,如果很偶然地只是生成一个两这样的图形,百度一下 ...

  3. SMARTFORM 条形码 二维码

    目录 一.二维码 1.创建二维码样式 SE73 2.创建样式,并在SMARTFORM中使用 二.条形码 1.创建条形码样式 SE73 2.创建样式,并在SMARTFORM中使用 一.二维码 1.创建二 ...

  4. 条码条形码二维码检错系统供应

     条码条形码二维码检错系统可防止因人为误操作.设备损坏等原因导致的错误,减少产品的退货返工,避免客户的投诉,提高工作效率.减少人力成本.适用于各种需要对条码进行检查的场合,如标签打印检查.装箱前对箱型 ...

  5. GM65条形码二维码扫描识别模块与STM32学习

    目录 模块介绍: 简介: 原理介绍: 工作流程: 技术参数: 修改配置: 代码介绍: 串口函数: 主函数: 模块介绍: 简介: GM65 条形码二维码扫描识别模块是一种基于激光扫描头的识别设备 原理介 ...

  6. JS生成条形码/二维码 barcode.js、JsBarcode

    JS生成条形码/二维码 barcode.JsBarcode JsBarcode Barcode.js 以下代码均非纯原创.新手小白.网上一搜一大把的概念也不写了,直接上可运行的代码及遇到的小坑. Js ...

  7. Opencv+Zbar二维码识别(标准条形码/二维码识别)

    使用Opencv+Zbar组合可以很容易的识别图片中的二维码,特别是标准的二维码,这里标准指的是二维码成像清晰,图片中二维码的空间占比在40%~100%之间,这样标准的图片,Zbar识别起来很容易,不 ...

  8. 条形码/二维码生成探索

    条形码/二维码生成探索 所用依赖 <!--条形码生成依赖(轻量型,推荐使用这个)(生成条码的同时会把信息生成到条形码下)--><dependency><groupId&g ...

  9. flutter实现条形码二维码扫描

    flutter实现条形码二维码扫描 准备工作 使用 我们使用到的第三方插件为barcode_scan2 地址为:https://pub.flutter-io.cn/packages/barcode_s ...

  10. 条形码/二维码之开源利器ZXing图文介绍

    全文目录: 基本介绍 二维码(比如:QRCode)的编码和解码演示 条形码(比如:EAN-13)的编码和解码演示 [一]. 基本介绍 : 1-1. ZXing是一个开源Java类库用于解析多种格式的条 ...

最新文章

  1. 《树莓派开发实战(第2版)》——1.15 使用蓝牙设备
  2. Android与JS混编(js调用android相机扫描二维码)
  3. qt中的数据库可以创建在主函数中吗_在qt中怎么建立数据库
  4. 天线巴伦制作和原理_一种基于LTCC技术的新型Marchand巴伦滤波器
  5. 边缘计算架构_边缘计算架构梳理
  6. 【kali】kali环境下安装dvwa
  7. 一步步编写操作系统 58 门、调用门与RPL序 3
  8. 微信3.3.0 for Mac全新发布:新增视频号 可搜索微信号添加朋友
  9. postgres 把一个表的值转成另一个表的字段名_Postgres索引那些事
  10. 【Vue起步-Windows】N01:环境安装
  11. java单例接口_JAVA单例模式
  12. bootice添加黑苹果引导_非苹果电脑,怎样在不破坏Win10的前提下,安装苹果MacOS?...
  13. 空间梁单元C语言有限元编程,有人会有限元编程吗
  14. Java Instrument
  15. 全球爆发计算机勒索病毒
  16. 国内CDN的排名情况
  17. 使用 Mist 部署众筹合约
  18. 交通标志牌的检测与识别
  19. 群晖虚拟机安装centos7.9
  20. 天行数据的开放API接口

热门文章

  1. 查看aspx文件安装什么服务器,aspx文件怎么打开?手把手教你怎么打开aspx文件
  2. php 新session_PHP会话(session)工作原理分析
  3. Elasticsearch DSL 查询详解
  4. linux设置双屏强制设置分辨率,话说你们的双屏显示器是怎样设置的 尤其是外接显示器分辨率设置...
  5. 行为资产定价模型(转载)
  6. 【鲁棒优化、大M法、CCG算法】计及风、光、负荷不确定性两阶段鲁棒优化(Matlab代码实现)
  7. Windows XP瘦身有术
  8. secureCRT安装和使用教程【图文并茂】
  9. 卷积神经网络_2.基本cnn网络实现(使用selu激活函数)
  10. 给ubuntu server 16.04.6打preempt rt补丁