admin 发表于 2024-10-19 17:59:15

一个全能的electron打印插件electron-pos-printer

一个全能的electron打印插件electron-pos-printer
可以一键打印图片、文本、二维码、条形码、表格不用安装其他多余插件,亲测十分好用electron-pos-printer1、安装
npm install electron-pos-printer
yarn add electron-pos-printer2、使用在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 = true
      return
      }
      const data = [
      {
          type: 'qrCode',
          value,
          height: 100,
          width: 100,
          displayValue: true, // Display value below barcode
          style: `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;`,
      },
      ]

大功告成!!!还有表格、图片我就不演示了,直接上作者democonst {PosPrinter} = require("electron-pos-printer");
const path = require("path");

const options = {
   preview: false,               // Preview in window or print
   width: '170px',               //width of content body
   margin: '0 0 0 0',            // margin of content body
   copies: 1,                  // Number of copies to print
   printerName: '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 path
   position: 'center',                                  // position of image: 'left' | 'center' | 'right'
   width: '60px',                                           // width of image in px; default: auto
   height: '60px',                                          // width of image in px; default: 50 or '50px'
   },{
      type: 'text',                                       // 'text' | 'barCode' | 'qrCode' | 'image' | 'table
      value: '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 codes
      width: 1,                     // width of barcode, applicable only to bar and QR codes
      displayValue: true,             // Display value below barcode
      fontsize: 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 table
       style: 'border: 1px solid #ddd',
       // list of the columns to be rendered in the table header
       tableHeader: ['Animal', 'Age'],
       // multi dimensional array depicting the rows and columns of the table body
       tableBody: [
         ['Cat', 2],
         ['Dog', 4],
         ['Horse', 12],
         ['Pig', 4],
       ],
       // list of columns to be rendered in the table footer
       tableFooter: ['Animal', 'Age'],
       // custom style for the table header
       tableHeaderStyle: 'background-color: #000; color: white;',
       // custom style for the table body
       tableBodyStyle: 'border: 0.5px solid #ddd',
       // custom style for the table footer
       tableFooterStyle: '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 header
       tableHeader: [{type: 'text', value: 'Animal'}, {type: 'image', path: path.join(__dirname, 'icons/animal.png')}],
       // multi dimensional array depicting the rows and columns of the table body
       tableBody: [
         [{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 footer
       tableFooter: [{type: 'text', value: 'Animal'}, 'Image'],
       // custom style for the table header
       tableHeaderStyle: 'background-color: #000; color: white;',
       // custom style for the table body
       tableBodyStyle: 'border: 0.5px solid #ddd',
       // custom style for the table footer
       tableFooterStyle: 'background-color: #000; color: white;',
    },
]

PosPrinter.print(data, options)
.then(() => {})
.catch((error) => {
    console.error(error);
});

很好用的一个打印工具插件!!!
static/image/hrline/1.gif
**** Hidden Message *****提取码下载:


页: [1]
查看完整版本: 一个全能的electron打印插件electron-pos-printer

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。