1.3.0 • Published 5 months ago

@plick/electron-pos-printer v1.3.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 months ago

Plick Electron pos printer

A customizable electron.js printing plugin specifically designed for thermal receipt printers. It supports 80mm, 78mm, 76mm, 58mm, 57mm, 44mm printers.

When recommend to use the 24.8.8 electron version, any major version of electron could not work properly

Installation

$ npm install @plick/electron-pos-printer

Usage

When using it on Electron require import is mandatory even if you are using typescript

const { PosPrinter } = require('@plick/electron-pos-printer');

Outside Electron and on a TS environment we can use import

import { PosPrinter } from '@plick/electron-pos-printer';

Otherwise, on JS environments we can use require

const { PosPrinter } = require('@plick/electron-pos-printer');

Typescript

import { PosPrinter, PosPrintData, PosPrintOptions } from '@plick/electron-pos-printer';

const options: PosPrintOptions = {
  preview: false,
  margin: '0 0 0 0',
  copies: 1,
  printerName: 'XP-80C',
  timeOutPerLine: 400,
  pageSize: '80mm', // page size
};

const data: PosPrintData[] = [
  {
    type: 'image',
    url: 'https://randomuser.me/api/portraits/men/43.jpg', // Can be a URL or change the key to "path" if you want to use a local file on disk
    position: 'center', // position of image: 'left' | 'center' | 'right'
    width: '160px', // 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' | 'divider'
    value: 'SAMPLE HEADING',
    style: { fontWeight: '700', textAlign: 'center', fontSize: '24px' },
  },
  {
    type: 'divider', // we could style it using the style property, we can use divider anywhere, except on the table header
  },
  {
    type: 'text',
    value: 'Secondary text',
    style: { textDecoration: 'underline', fontSize: '10px', textAlign: 'center', color: 'red' },
  },
  {
    type: 'barCode',
    value: '023456789010',
    height: 40, // height of barcode, applicable only to bar and QR codes
    width: 2, // width of barcode, applicable only to bar and QR codes
    displayValue: true, // Display value below barcode
    fontSize: 12,
  },
  {
    type: 'qrCode',
    value: 'https://www.npmjs.com/package/@plick/electron-pos-printer',
    height: 65,
    width: 65,
    position: 'center',
    style: { margin: '10 20px 20 20px' },
  },
  {
    type: 'text',
    value: 'Table 1',
    style: { fontWeight: '700', textAlign: 'center', fontSize: '24px' },
  },
  {
    type: 'table',
    // style the table
    style: { border: '1px solid #ddd', fontFamily: 'sans-serif' },
    // list of the columns to be rendered in the table header
    tableHeader: [
      { type: 'text', value: 'Animal' },
      { type: 'text', value: 'Count' },
    ],
    // multi dimensional array depicting the rows and columns of the table body
    tableBody: [
      [
        { type: 'text', value: 'Cat' },
        { type: 'text', value: '10' },
      ],
      [
        { type: 'text', value: 'Dog' },
        { type: 'text', value: '5' },
      ],
      [
        { type: 'text', value: 'Pig' },
        { type: 'text', value: '7' },
      ],
    ],
    // list of rows to be rendered in the table footer
    tableFooter: [
      [
        { type: 'text', value: 'Subtotal' },
        { type: 'text', value: '22' },
      ],
      [
        { type: 'text', value: 'Total' },
        { type: 'text', value: '22' },
      ],
    ],
    // custom style for the table header
    tableHeaderStyle: { backgroundColor: '#000', color: 'white' },
    // custom style for the table body
    tableBodyStyle: { border: '0.5px solid #ddd' },
    // custom style for the table footer
    tableFooterStyle: { backgroundColor: '#000', color: 'white' },
    // custom style for the header cells
    tableHeaderCellStyle: {
      padding: '2px 2px',
    },
    // custom style for the body cells
    tableBodyCellStyle: {
      padding: '10px 2px',
    },
    // custom style for the footer cells
    tableFooterCellStyle: {
      padding: '5px 2px',
      fontWeight: '400',
    },
  },
  {
    type: 'text',
    value: 'Table 2',
    style: { fontWeight: '700', textAlign: 'center', fontSize: '24px' },
  },
  {
    type: 'table',
    style: { border: '1px solid #000' }, // style the table
    // list of the columns to be rendered in the table header
    tableHeader: [
      { type: 'text', value: 'People' },
      { type: 'text', value: 'Image' },
    ],
    // multi-dimensional array depicting the rows and columns of the table body
    tableBody: [
      [
        { type: 'text', value: 'Marcus' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/43.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
      [
        { type: 'text', value: 'Boris' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/41.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
      [
        { type: 'text', value: 'Andrew' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/23.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
      [
        { type: 'text', value: 'Tyresse' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/53.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
    ],
    // list of rows to be rendered in the table footer
    tableFooter: [
      [
        { type: 'text', value: 'People' },
        { type: 'text', value: 'Image' },
      ],
    ],
    // custom style for the table header
    tableHeaderStyle: { backgroundColor: 'red', color: 'white' },
    // custom style for the table body
    tableBodyStyle: { border: '0.5px solid #000' },
    // custom style for the table footer
    tableFooterStyle: { backgroundColor: '#000', color: 'white' },
    // custom style for the header cells
    tableHeaderCellStyle: {
      padding: '5px 2px',
    },
    // custom style for the body cells
    tableBodyCellStyle: {
      padding: '1px 2px',
    },
    // custom style for the footer cells
    tableFooterCellStyle: {
      padding: '2px 2px',
      fontWeight: '400',
    },
  },
];

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

Javascript

const { PosPrinter, PosPrintData, PosPrintOptions } = require('@plick/electron-pos-printer');

const options = {
  preview: false,
  margin: '0 0 0 0',
  copies: 1,
  printerName: 'XP-80C',
  timeOutPerLine: 400,
  pageSize: '80mm', // page size
};

const data = [
  {
    type: 'image',
    url: 'https://randomuser.me/api/portraits/men/43.jpg', // Can be a URL or change the key to "path" if you want to use a local file on disk
    position: 'center', // position of image: 'left' | 'center' | 'right'
    width: '160px', // 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' | 'divider'
    value: 'SAMPLE HEADING',
    style: { fontWeight: '700', textAlign: 'center', fontSize: '24px' },
  },
  {
    type: 'divider', // we could style it using the style property, we can use divider anywhere, except on the table header
  },
  {
    type: 'text',
    value: 'Secondary text',
    style: { textDecoration: 'underline', fontSize: '10px', textAlign: 'center', color: 'red' },
  },
  {
    type: 'barCode',
    value: '023456789010',
    height: 40, // height of barcode, applicable only to bar and QR codes
    width: 2, // width of barcode, applicable only to bar and QR codes
    displayValue: true, // Display value below barcode
    fontSize: 12,
  },
  {
    type: 'qrCode',
    value: 'https://www.npmjs.com/package/@plick/electron-pos-printer',
    height: 65,
    width: 65,
    position: 'center',
    style: { margin: '10 20px 20 20px' },
  },
  {
    type: 'text',
    value: 'Table 1',
    style: { fontWeight: '700', textAlign: 'center', fontSize: '24px' },
  },
  {
    type: 'table',
    // style the table
    style: { border: '1px solid #ddd', fontFamily: 'sans-serif' },
    // list of the columns to be rendered in the table header
    tableHeader: [
      { type: 'text', value: 'Animal' },
      { type: 'text', value: 'Count' },
    ],
    // multi dimensional array depicting the rows and columns of the table body
    tableBody: [
      [
        { type: 'text', value: 'Cat' },
        { type: 'text', value: '10' },
      ],
      [
        { type: 'text', value: 'Dog' },
        { type: 'text', value: '5' },
      ],
      [
        { type: 'text', value: 'Pig' },
        { type: 'text', value: '7' },
      ],
    ],
    // list of rows to be rendered in the table footer
    tableFooter: [
      [
        { type: 'text', value: 'Subtotal' },
        { type: 'text', value: '22' },
      ],
      [
        { type: 'text', value: 'Total' },
        { type: 'text', value: '22' },
      ],
    ],
    // custom style for the table header
    tableHeaderStyle: { backgroundColor: '#000', color: 'white' },
    // custom style for the table body
    tableBodyStyle: { border: '0.5px solid #ddd' },
    // custom style for the table footer
    tableFooterStyle: { backgroundColor: '#000', color: 'white' },
    // custom style for the header cells
    tableHeaderCellStyle: {
      padding: '2px 2px',
    },
    // custom style for the body cells
    tableBodyCellStyle: {
      padding: '10px 2px',
    },
    // custom style for the footer cells
    tableFooterCellStyle: {
      padding: '5px 2px',
      fontWeight: '400',
    },
  },
  {
    type: 'text',
    value: 'Table 2',
    style: { fontWeight: '700', textAlign: 'center', fontSize: '24px' },
  },
  {
    type: 'table',
    style: { border: '1px solid #000' }, // style the table
    // list of the columns to be rendered in the table header
    tableHeader: [
      { type: 'text', value: 'People' },
      { type: 'text', value: 'Image' },
    ],
    // multi-dimensional array depicting the rows and columns of the table body
    tableBody: [
      [
        { type: 'text', value: 'Marcus' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/43.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
      [
        { type: 'text', value: 'Boris' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/41.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
      [
        { type: 'text', value: 'Andrew' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/23.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
      [
        { type: 'text', value: 'Tyresse' },
        { type: 'image', url: 'https://randomuser.me/api/portraits/men/53.jpg', position: 'center' },
      ],
      [
        {
          type: 'divider',
        },
      ],
    ],
    // list of rows to be rendered in the table footer
    tableFooter: [
      [
        { type: 'text', value: 'People' },
        { type: 'text', value: 'Image' },
      ],
    ],
    // custom style for the table header
    tableHeaderStyle: { backgroundColor: 'red', color: 'white' },
    // custom style for the table body
    tableBodyStyle: { border: '0.5px solid #000' },
    // custom style for the table footer
    tableFooterStyle: { backgroundColor: '#000', color: 'white' },
    // custom style for the header cells
    tableHeaderCellStyle: {
      padding: '5px 2px',
    },
    // custom style for the body cells
    tableBodyCellStyle: {
      padding: '1px 2px',
    },
    // custom style for the footer cells
    tableFooterCellStyle: {
      padding: '2px 2px',
      fontWeight: '400',
    },
  },
];

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

Printing options

OptionsRequiredTypeDescription
printerNameNostringThe printer's name. If not set, the system's default printer will be used.
copiesNonumberThe number of copies to print
previewNobooleanPreview print job in a window. Default is false
widthNostringWidth of a page's content
marginNostringMargin of a page's content. CSS margin values can be used Ex. 0 10px
timeOutPerLineNonumberTimeout per line in milliseconds. Default value is 400
silentNobooleanTo print silently without the system displaying the printer selection window. Default value is true
pageSizeNoPaperSize, SizeOptionsConfigure the paper size which is supported by your printer. values are are 80mm, 78mm, 76mm, 58mm, 57mm, 44mm or object with width and height properties in pixels.
pathTemplateNostringPath to custom html template. Can be used for custom print styles.
headerNostringText to be printed as page header.
footerNostringText to be printed as page footer.
marginsNoobjectPage margins. See electron docs
landscapeNobooleanWhether the page should be printed in landscape mode. Default is false.
scaleFactorNonumberThe scale factor of the web page.
pagesPerSheetNonumberThe number of pages to print per page sheet.
collateNobooleanWhether the page should be collated.
pageRangesNoobject[]The page range to print. See electron docs
duplexModeNostringSet the duplex mode of the printed web page. See electron docs
dpiNoobjectSee electron docs

The Print data object

Important!!

Use the style property, example: style: {fontWeight: "700", textAlign: 'center', fontSize: "24px"}

PropertyTypeDescription
typestringtext, qrCode, barCode, image, table, divider // type text can be an html string
valuestringValue of the current row
heightnumberApplicable to type barCode and qrCode
widthnumberApplicable to type barCode and qrCode
stylePrintDataStyleAdd css styles to line (jsx syntax) ex: {fontSize: 12, backgroundColor: '#2196f3}
displayValuebooleanDisplay value of barcode below barcode
positionstringleft, center, right applicable to types qrCode and image
pathstringPath or url to the image asset
urlstringUrl to image or a base 64 encoding of image
tableHeaderPosPrintTableField[]The columns to be rendered in the header of the table. Works with type table
tableBodyPosPrintTableField[][]The columns to be rendered in the body of the table. Works with type table
tableFooterPosPrintTableField[][]The columns to rendered it the footer of the table. Works with type table
tableHeaderStylePrintDataStyleSet a custom style to the table header
tableBodyStylePrintDataStyleSet a custom style to the table body
tableFooterStylePrintDataStyleSet a custom style to the table footer
tableHeaderCellStylePrintDataStyleSet a custom style to the header cells
tableBodyCellStylePrintDataStyleSet a custom style to the body cells
tableFooterCellStylePrintDataStyleSet a custom style to the footer cells

Additional Types

type PosPrintTableField = {
  type: 'text' | 'image' | 'divider';
  value?: string;
  path?: string;
  style?: PrintDataStyle;
  width?: string; // for type image
  height?: string; // for type image
}

// PrintDataStyle is a type of object with jsx css properties

FAQ

Having sizing issues? Try to set the pageSize option to:

pageSize: {
  width: 71000,
  height: 301000,
}

For previews only, you can set the width to 205 (close to 80mm size):

pageSize: {
  width: 205,
  height: 301000,
}

Having table issues? This fields are mandatory on tables, you can leave them empty but they must be present:

tableHeader
tableBody
tableFooter
tableHeaderStyle
tableBodyStyle
tableFooterStyle

Notes

Fork

Forked from https://github.com/Hubertformin/electron-pos-printer Version 1.3.6

1.2.0

5 months ago

1.3.0

5 months ago

1.1.0

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago

0.1.3

5 months ago

0.1.2

5 months ago

0.1.1

5 months ago

0.1.0

5 months ago