<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          一個全能的electron打印插件

          共 5825字,需瀏覽 12分鐘

           ·

          2021-10-30 00:39

          可以一鍵打印圖片、文本、二維碼、條形碼、表格

          不用安裝其他多余插件,親測十分好用

          electron-pos-printer

          1、安裝

          npm install electron-pos-printeryarn?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 = 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;`,        },      ]

          大功告成!!!

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

          const {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);  });


          瀏覽 142
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  欧美大鸡巴在线观看 | 色就是色,欧美 | 97国产超碰 | 日韩精品在线免费观看视频 | 成人性爱视频免费看 |