node-escpos / driver

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

Question: This package works with electron? #17

Closed Kaplas85 closed 1 year ago

dohooo commented 1 year ago

Yep, certainly. I have used it in my electron project.

Kaplas85 commented 1 year ago

Can you give me a code example? Because I have this error image

And this is my main.js (Electron v15.0.0)

import { app, autoUpdater, dialog, ipcMain } from "electron";
import serve from "electron-serve";
import { createWindow } from "./helpers";
import { Printer } from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";

const isProd = process.env.NODE_ENV === "production";

if (isProd) {
  serve({ directory: "app" });
} else {
  app.setPath("userData", `${app.getPath("userData")} (development)`);
}

(async () => {
  await app.whenReady();

  const mainWindow = createWindow("main", {
    width: 1000,
    height: 600,
  });

  if (isProd) {
    await mainWindow.loadURL("app://./home.html");
  } else {
    const port = process.argv[2];
    await mainWindow.loadURL(`http://localhost:${port}/home`);
    mainWindow.webContents.openDevTools();
  }
})();

ipcMain.handle("get-version", async (e) => {
  return app.getVersion();
});

ipcMain.handle("find-printers", async (e) => {
  const usbDevices = USB.findPrinter();
  console.log(usbDevices);
});

ipcMain.handle("print-receipt", async (e, itemsData, orderData) => {
  const device = new USB();
  const options = {
    encoding: "utf-8",
  };
  const printer = new Printer(device, options);

  device.open(async function (err) {
    if (err) {
      console.log(err);
      return;
    }

    printer.align("CT").size(10, 10).style("B").text("Dimalchi");
    printer
      .align("CT")
      .style("NORMAL")
      .size(1, 1)
      .text("Inv. Alma Fashion C.A.")
      .text("RIF: J40556539-0")
      .feed(1)
      .text(
        "Calle 100 (COLOMBIA) Casa Nro 101-10 Sector Centro de Valencia Valencia Carabobo Zona Postal 2001"
      );
    printer
      .feed(1)
      .align("LT")
      .style("B")
      .text(`Nota de Entrega: ${orderData.id}`)
      .style("NORMAL")
      .text(`Cliente: ${orderData.clientName}`)
      .text(
        `Fecha: ${new Date(orderData.createdAt).toLocaleDateString("es-ve")}`
      )
      .feed(2);

    printer
      .table(["Producto", "Cantidad", "Precio", "Total"])
      .drawLine()
      .feed(1);
    itemsData.map((item) => {
      printer.tableCustom([
        { text: item.name, align: "CENTER" },
        { text: item.quantity, align: "CENTER" },
        { text: item.price, align: "CENTER" },
        { text: item.total, align: "CENTER" },
      ]);
    });
    printer.feed(1).drawLine().feed(1);

    printer
      .text("Subtotal: Bs " + orderData.subTotal)
      .text("IVA: Bs " + orderData.ivaAmount)
      .text("Total: Bs " + orderData.total);

    printer.feed(2);
    printer.align("CT").text("¡Gracias por su compra!");

    printer.feed(4);
    printer.cut().close();
  });
});
dohooo commented 1 year ago

It seems you didn't bundle the lib to your electron program. I have no more time to do this currently, sry...

YovanggaAnandhika commented 1 year ago

i Am tested from typescript or Webpack running normal. @Kaplas85 i think your running code by commonJS not require.

Kaplas85 commented 1 year ago

I was try using

...
import * as escpos from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";
...

and the error is the same :(

dohooo commented 1 year ago

Follow this #23. I'm so busy today... 🕊️

ferminc commented 1 year ago

I was try using

...
import * as escpos from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";
...

and the error is the same :(

Are you using typescript? If so, be sure you are compiling to commonjs Electron doesn’t support that import syntax (yet)

Kaplas85 commented 1 year ago

I was try using

...
import * as escpos from "@node-escpos/core";
import USB from "@node-escpos/usb-adapter";
...

and the error is the same :(

Are you using typescript? If so, be sure you are compiling to commonjs Electron doesn’t support that import syntax (yet)

No, I'm using JS and my original code uses commonJS

YovanggaAnandhika commented 1 year ago

you sure adding yarn add @node-escpos/core first ?.

before to electron run build from tsc -- build and after build the source in dist folder. pointing the main to dist/index.js or another file name. for run electron .

dohooo commented 1 year ago

https://github.com/node-escpos/electron-example

Kaplas85 commented 1 year ago

you sure adding yarn add @node-escpos/core first ?.

before to electron run build from tsc -- build and after build the source in dist folder. pointing the main to dist/index.js or another file name. for run electron .

Yeah, sure. When I use the electron in dev, the package works fine.

https://github.com/node-escpos/electron-example

Thanks! I'll check