electron打印条形码、二维码

article/2025/8/26 6:21:11

功能:在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);});

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

 


http://chatgpt.dhexx.cn/article/jHjDm7Qb.shtml

相关文章

佳博打印机打印条码和二维码的方法

最近项目需求连接蓝牙打印机打印小票 其中小票中有条码和二维码,很多朋友刚接触打印接的打印逻辑,看不太明白,因此我之前给各位提供现成的打印方法: 关键就是安装打印的文档 提前写好各种打印的调用发放,直接在打印模…

【C#】简单二维码制作和打印

系列文章 【C#】条码管理操作手册 本文链接:https://blog.csdn.net/youcheng_ge/article/details/126589496 【C#】IIS平台下,WebAPI发布及异常处理 本文链接:https://blog.csdn.net/youcheng_ge/article/details/126539836 【C#】简单二维…

一维码二维码的生成及打印

VUE条形码及二维码的生成及输出到打印机 文章目录 VUE条形码及二维码的生成及输出到打印机1. 条形码的生成2. 二维码的生成3. 输出打印4. 将代码提取一下 1. 条形码的生成 安装插件 npm install jsbarcode --save github地址: https://github.com/lindell/JsBarcode…

C#二维码的生成及打印

窗体设计显示图 一&#xff0c;生成二维码 /// <summary>/// 生成二维码/// </summary>/// <param name"text">内容</param>/// <param name"width">宽度</param>/// <param name"height">高度</p…

生成二维码,并且打印

生成二维码&#xff0c;并且打印出来 加入依赖 //二维码依赖 npm install --save qrcodejs2 //打印依赖 npm install vue-print-nb --save在mian文件夹中使用引入并使用&#xff0c;或者按需引入 //打印依赖 import Print from vue-print-nb Vue.use(Print)本次二维码是按需引…

二维码打印

1. 二维码简介 二维码&#xff08;2-dimensional bar code&#xff09;&#xff0c;又称二维条码&#xff0c;它是用特定的几何图形按一定规律在平面&#xff08;二维方向&#xff09;上分布的黑白相间的图形&#xff0c;是所有信息数据的一把钥匙。在现代商业活动中&…

vue生成条形码和二维码并打印

文章目录 前言一、生成条形码二、生成二维码三、效果图四、打印 前言 最近有一个需求&#xff0c;需要将产品信息生成标签&#xff0c;每个信息生成一个条形码&#xff0c;拿到所有数据生成二维码&#xff0c;最后打印标签。 一、生成条形码 使用jsbarcode&#xff0c;直接in…

vue二维码生成、打印及识别

生成二维码 引入QRCode模块 npm install --save qrcode import QRCode from qrcode; 页面代码 批量创建二维码及打印&#xff0c;所以加了个v-for <div id"printDiv"/**后面打印用到的区域id*/ :disabled"hussar_20Disabled" ref"hussar_20Ref&quo…

如何将内网ip映射到外网

这个百度经验上就有&#xff0c;不过对于着急使用的我来说&#xff0c;差了最后一步。所以还是一起记上吧。 第一步 首先登陆你的路由器&#xff0c;就改密码那个。一般就用浏览器直接打开192.168.1.1就可以&#xff0c;账号密码有可能是 admin admin吧&#xff0c;默认可能是…

VMWare虚拟机局域网网络配置,主机访问虚拟机上的网站(端口映射)

文章目录 1 problem2 背景知识3 配置主机网络4 配置虚拟机网络 1 problem 如题&#xff0c;现在虚拟机的80端口和8080端口都有一个网站&#xff0c;我们想要在主机访问它。 2 背景知识 VmWare支持3种网络连接模式。 1、桥接模式&#xff1a; 所谓桥接就是把两个本来分开…

映射公网的几种方式

转载自&#xff1a;http://blog.csdn.net/sadshen/article/details/48240519 这篇文章花了好几天&#xff0c;系统地梳理出了映射公网的几种方式。虽然是针对微信开发的外网服务器来寻找解决方案&#xff0c;但这个知识梳理可能会在其他地方也受益。平常我也有用TeamViewer&…

VMware NAT端口映射 外网可以访问内网虚拟机

我想尝试的是利用本机的ipport来访问虚拟机上的web服务器&#xff0c;因为这样的话&#xff0c;我就能够将我的web服务器部署成为一个能让外网访问的服务器了&#xff0c;首先说下我的环境&#xff1a; 主机&#xff1a;系统win7&#xff0c;ip地址172.18.186.210 虚拟机版本是…

如何把内网IP映射到公网IP

鸽子出品 2017-12-05 22:28:22 我们讲了如何搭建网站&#xff0c;可是有很多小伙伴私信跟我说怎么映射&#xff0c;今天我就教大家如何把内网地址映射到公网&#xff01; 我们所需要的工具有&#xff1a; 内网IP&#xff08;这个是品&#xff0c;也是必有的&#xff01;&…

简单内网映射到公网方法--免费

我是一名Android API Player&#xff0c;最近公司需要做微信公众号二次开发&#xff0c;我跟着学学&#xff0c;公司后台.net。 我mac安装windows之后用vs感觉太差了&#xff0c;可能是我的mac要淘汰了吧。 所以我决定用java后台来跟着做。 仔细一想我没有服务器啊。 再仔细…

本地主机搭建服务器后如何让外网访问?快解析内网端口映射

本地主机搭建应用、部署服务器后&#xff0c;在局域网内是可以直接通过计算机内网IP网络地址进行连接访问的&#xff0c;但在外网电脑和设备如何访问呢&#xff1f;由于内网环境下&#xff0c;无法提供公网IP使用&#xff0c;外网访问内网就需要一个内外网转换的介质。这里介绍…

内网和外网之间的通信(端口映射原理)

内网和外网之间的通信&#xff08;端口映射原理&#xff09; 首先解释一下“内网”与“外网”的概念&#xff1a; 内网&#xff1a;即所说的局域网&#xff0c;比如学校的局域网&#xff0c;局域网内每台计算机的IP地址在本局域网内具有互异性&#xff0c;是不可重复的。但两…

实现内网通过外网域名访问NAT映射的内网服务器

大家都知道在用Huawei的中低端路由器做NAT地址映射时只能支持 “外网访问” 不支持 “内网访问”。也就是说只支持NAT映射后外网IP通过外网域名&#xff08;外网IP&#xff09;访问NAT映射的服务器 &#xff0c;不支持NAT映射后内网IP通过外网域名&#xff08;外网IP&#xff0…

给内网映射增加安全防护

1.背景 内部网络资源的访问&#xff0c;通用的做法是部署企业VPN&#xff0c;通过VPN设备提供公网接入服务&#xff0c;客户端接入后&#xff0c;则可以像本地网络一样访问内网资源。但是部署VPN需要专业设备&#xff0c;且配置管理往往需要专业人员&#xff0c;对普通小…

公网映射思路

1、使用环境 目前政务微信的有两台内网环境71和14服务器&#xff0c;外网不能访问&#xff0c;只能内部访问&#xff0c;但是项目部署在14上面71的nginx转发到14服务器上面&#xff0c;但是现在14的项目需要网络支持&#xff0c;另外需要一台公网环境180服务器。 2、公网环境…

外网做ip映射访问内网链接

首先这个外网必须是独立ip 然后在路由器设置ip映射&#xff0c;注意防火墙放通该端口&#xff0c;可以telnet连一下试试有没通