node-escpos / driver

🖨️ ESC/POS Printer driver for Node.js.
Other
318 stars 32 forks source link

How to install and run this module? #77

Open PradhaID opened 1 year ago

PradhaID commented 1 year ago

i have installed this module using npm i @node-escpos/core and npm i @node-escpos/usb-adapter and try using sample script

import { Printer, Image } from "@node-escpos/core";
// install escpos-usb adapter module manually
import USB from "@node-escpos/usb-adapter";
// Select the adapter based on your printer type
import { join } from "path";

const device = new USB();

device.open(async function(err){
  if(err){
    // handle error
    return
  }

  // encoding is optional
  const options = { encoding: "GB18030" /* default */ }
  let printer = new Printer(device, options);

  // Path to png image
  const filePath = join("/PATH/TO/IMAGE");
  const image = await Image.load(filePath);
  printer
    .font("a")
    .align("ct")
    .style("bu")
    .size(1, 1)
    .text("May the gold fill your pocket")
    .text("恭喜发财")
    .barcode(112233445566, "EAN13", { width: 50, height: 50 })
    .table(["One", "Two", "Three"])
    .tableCustom(
      [
        { text: "Left", align: "LEFT", width: 0.33, style: "B" },
        { text: "Center", align: "CENTER", width: 0.33 },
        { text: "Right", align: "RIGHT", width: 0.33 },
      ],
      { encoding: "cp857", size: [1, 1] }, // Optional
    )

  // inject qrimage to printer
  printer = await printer.qrimage("https://github.com/node-escpos/driver")
  // inject image to printer
  printer = await printer.image(
    image, 
    "s8" // changing with image
  )

  printer
    .cut()
    .close()
});

but when i execute this script, it's trow an error :

- error Error: No native build was found for platform=linux arch=x64 runtime=node abi=115 uv=1 libc=glibc node=20.5.0 webpack=true
    loaded from: /home/aqeela/Projects/pradha-next/.next/server/app/api

    at load.resolve.load.path (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/node-gyp-build/node-gyp-build.js:50:11)
    at load (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/node-gyp-build/node-gyp-build.js:20:32)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/bindings.js:9:194)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/bindings.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2686:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/device.js:6:11)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/device.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2719:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/index.js:17:16)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/index.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2741:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/webusb/index.js:185:11)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/webusb/index.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2763:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/index.js:158:16)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/index.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2675:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/dist/index.mjs:7:61)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/dist/index.mjs (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3577:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./src/app/api/queue/ticket/route.tsx:13:82)
    at (rsc)/./src/app/api/queue/ticket/route.tsx (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3467:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fqueue%2Fticket%2Froute&page=%2Fapi%2Fqueue%2Fticket%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fqueue%2Fticket%2Froute.tsx&appDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:15:126)
    at (rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fqueue%2Fticket%2Froute&page=%2Fapi%2Fqueue%2Fticket%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fqueue%2Fticket%2Froute.tsx&appDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:231:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at __webpack_exec__ (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3939:39)
    at /home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3940:28
    at Object.<anonymous> (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3943:3)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:119:18)
    at requirePage (/home/aqeela/Projects/pradha-next/node_modules/next/dist/server/require.js:112:75)
    at /home/aqeela/Projects/pradha-next/node_modules/next/dist/server/load-components.js:80:84
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async loadComponentsImpl (/home/aqeela/Projects/pradha-next/node_modules/next/dist/server/load-components.js:80:26)
    at async DevServer.findPageComponentsImpl (/home/aqeela/Projects/pradha-next/node_modules/next/dist/server/next-server.js:772:36) {
  digest: undefined
}
null
YovanggaAnandhika commented 1 year ago

i have installed this module using npm i @node-escpos/core and npm i @node-escpos/usb-adapter and try using sample script

import { Printer, Image } from "@node-escpos/core";
// install escpos-usb adapter module manually
import USB from "@node-escpos/usb-adapter";
// Select the adapter based on your printer type
import { join } from "path";

const device = new USB();

device.open(async function(err){
  if(err){
    // handle error
    return
  }

  // encoding is optional
  const options = { encoding: "GB18030" /* default */ }
  let printer = new Printer(device, options);

  // Path to png image
  const filePath = join("/PATH/TO/IMAGE");
  const image = await Image.load(filePath);
  printer
    .font("a")
    .align("ct")
    .style("bu")
    .size(1, 1)
    .text("May the gold fill your pocket")
    .text("恭喜发财")
    .barcode(112233445566, "EAN13", { width: 50, height: 50 })
    .table(["One", "Two", "Three"])
    .tableCustom(
      [
        { text: "Left", align: "LEFT", width: 0.33, style: "B" },
        { text: "Center", align: "CENTER", width: 0.33 },
        { text: "Right", align: "RIGHT", width: 0.33 },
      ],
      { encoding: "cp857", size: [1, 1] }, // Optional
    )

  // inject qrimage to printer
  printer = await printer.qrimage("https://github.com/node-escpos/driver")
  // inject image to printer
  printer = await printer.image(
    image, 
    "s8" // changing with image
  )

  printer
    .cut()
    .close()
});

but when i execute this script, it's trow an error :

- error Error: No native build was found for platform=linux arch=x64 runtime=node abi=115 uv=1 libc=glibc node=20.5.0 webpack=true
    loaded from: /home/aqeela/Projects/pradha-next/.next/server/app/api

    at load.resolve.load.path (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/node-gyp-build/node-gyp-build.js:50:11)
    at load (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/node-gyp-build/node-gyp-build.js:20:32)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/bindings.js:9:194)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/bindings.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2686:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/device.js:6:11)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/device.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2719:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/index.js:17:16)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/usb/index.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2741:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/webusb/index.js:185:11)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/webusb/index.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2763:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/index.js:158:16)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/node_modules/usb/dist/index.js (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:2675:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/@node-escpos/usb-adapter/dist/index.mjs:7:61)
    at (rsc)/./node_modules/@node-escpos/usb-adapter/dist/index.mjs (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3577:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./src/app/api/queue/ticket/route.tsx:13:82)
    at (rsc)/./src/app/api/queue/ticket/route.tsx (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3467:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at eval (webpack-internal:///(rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fqueue%2Fticket%2Froute&page=%2Fapi%2Fqueue%2Fticket%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fqueue%2Fticket%2Froute.tsx&appDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D!:15:126)
    at (rsc)/./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2Fqueue%2Fticket%2Froute&page=%2Fapi%2Fqueue%2Fticket%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2Fqueue%2Fticket%2Froute.tsx&appDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2Fhome%2Faqeela%2FProjects%2Fpradha-next&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=&middlewareConfig=e30%3D! (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:231:1)
    at __webpack_require__ (/home/aqeela/Projects/pradha-next/.next/server/webpack-runtime.js:33:43)
    at __webpack_exec__ (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3939:39)
    at /home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3940:28
    at Object.<anonymous> (/home/aqeela/Projects/pradha-next/.next/server/app/api/queue/ticket/route.js:3943:3)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10)
    at Module.load (node:internal/modules/cjs/loader:1091:32)
    at Module._load (node:internal/modules/cjs/loader:938:12)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:119:18)
    at requirePage (/home/aqeela/Projects/pradha-next/node_modules/next/dist/server/require.js:112:75)
    at /home/aqeela/Projects/pradha-next/node_modules/next/dist/server/load-components.js:80:84
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async loadComponentsImpl (/home/aqeela/Projects/pradha-next/node_modules/next/dist/server/load-components.js:80:26)
    at async DevServer.findPageComponentsImpl (/home/aqeela/Projects/pradha-next/node_modules/next/dist/server/next-server.js:772:36) {
  digest: undefined
}
null

I see you are using webpack. do you want to implement for reactjs ?

you cannot use the module on the client side. in this case webpack for react. because this module runs on the server side (requires CommonJS).

EliasLeguizamon123 commented 10 months ago

Hi, i have a similar issue here, im using Electron, and i trying to implement this project in main process (all with nodejs)

I try with this simple example:

Main process (NodeJS)

import { Printer } from '@node-escpos/core';
import USB from '@node-escpos/usb-adapter';

ipcMain.on('printTKS', (event, message) => {
    try {
        if (process.platform === 'win32') {
            const device = new USB(); //! >> Here is my error 
            const options = {
                encoding: 'GB18030',
            };

            device.open(async (error) => {
                if (error) {
                    console.log('some error', error);

                    return;
                } else {
                    const printer = new Printer(device, options);

                    printer
                        .font('a')
                        .align('ct')
                        .style('bu')
                        .size(1, 1)
                        .text('TKS EXAMPLE')
                        .text(' ')
                        .text(' ')
                        .text(' ')
                        .table(['a', 'b', 'c']);
                    printer.cut().close();
                }
            });
        }
    } catch (error) {
        console.error('Error:', error.message);
    }
});

Error in console

A JavaScript error occurred in the main process
Uncaught Exception:
Error: No native build was found for platform=linux arch=x64 runtime=electron abi=116 uv=1 libc=glibc node=18.15.0 electron=25.5.0
    loaded from: /media/user/work/myAppName/dist

    at load.resolve.load.path (/media/user/work/myAppName/dist/electron/main/index.js:15634:11)
    at Object.load [as exports] (/media/user/work/myAppName/dist/electron/main/index.js:15598:32)
    at Module.<anonymous> (/media/user/work/myAppName/dist/electron/main/index.js:15779:30)
    at Module._compile (node:internal/modules/cjs/loader:1269:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1324:10)
    at Module.load (node:internal/modules/cjs/loader:1124:32)
    at Module._load (node:internal/modules/cjs/loader:965:12)
    at f._load (node:electron/js2c/asar_bundle:2:13330)
YovanggaAnandhika commented 10 months ago

import express from 'express'; import { Printer, Image } from '@node-escpos/core'; // install escpos-usb adapter module manually import USB from '@node-escpos/usb-adapter'; // Select the adapter based on your printer type import { join } from 'path';

const router = express.Router(); // Ruta POST para manejar la impresión

router.post('/example-print', async (req, res) => { try { try { // Crear una instancia de USB adapter const device = new USB();

  // Abrir la conexión del dispositivo
  device.open(async function (err) {
    if (err) {
      // handle error
      return;
    }

    // encoding is optional
    const options = { encoding: 'GB18030' /* default */ };
    let printer = new Printer(device, options);

    // Path to png image
    // const filePath = join('/PATH/TO/IMAGE');
    // const image = await Image.load(filePath);

    printer
      .font('a')
      .align('ct')
      .style('bu')
      .size(1, 1)
      .text('May the gold fill your pocket')
      .text('恭喜发财')
      .barcode(112233445566, 'EAN13', { width: 50, height: 50 })
      .table(['One', 'Two', 'Three'])
      .tableCustom(
        [
          { text: 'Left', align: 'LEFT', width: 0.33, style: 'B' },
          { text: 'Center', align: 'CENTER', width: 0.33 },
          { text: 'Right', align: 'RIGHT', width: 0.33 },
        ],
        { encoding: 'cp857', size: [1, 1] } // Optional
      );

    // inject qrimage to printer
    // printer = await printer.qrimage('https://github.com/node-escpos/driver');
    // inject image to printer
    // printer = await printer.image(
    //   image,
    //   's8' // changing with image
    // );

    printer.cut().close();
  });
} catch (error) {
  console.error('Error al imprimir: ', error);
  res.status(500).send('Error interno del servidor: ' + error.message);
}
// res.status(201).json('gaaa');

} catch (error) { console.error(error); res.status(500).json({ mensaje: 'Error ' }); } });

export default router; --- lError al imprimir: Error: LIBUSB_ERROR_NOT_SUPPORTED at Device.open (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\usb@2.11.0\node_modules\usb\dist\usb\device.js:63:14) at USBAdapter.open (file:///C:/Users/User/Desktop/Sistema%20Lavanderia/Trujillo/Cesar/server/node_modules/.pnpm/@node-escpos+usb-adapter@0.3.1/node_modules/@node-escpos/usb-adapter/dist/index.mjs:81:18) at file:///C:/Users/User/Desktop/Sistema%20Lavanderia/Trujillo/Cesar/server/server/routes/print.js:19:14 at Layer.handle [as handle_request] (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\route.js:144:13) at Route.dispatch (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\route.js:114:3) at Layer.handle [as handle_request] (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\layer.js:95:5) at C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\index.js:284:15 at Function.process_params (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\index.js:346:12) at next (C:\Users\User\Desktop\Sistema Lavanderia\Trujillo\Cesar\server\node_modules.pnpm\express@4.18.2\node_modules\express\lib\router\index.js:280:10) { errno: -12 }

On Windows require installed Zadig for bridge your printer & the library Refference This Post. Hope you can help :)

If you use usb as an adapter :

Otherwise you will get LIBUSB_ERROR_NOT_SUPPORTED when attempting to open devices.

USB(vid, pid)

const escpos = require('escpos');
escpos.USB = require('escpos-usb');

const usbDevice = new escpos.USB(0x01, 0xff);

vid(Vendor Id) and pid (Product Id) can be checked with the lsusb command or escpos.USB.findPrinter() method.