Hubertformin / electron-pos-printer

Electron-pos-printer is a plugin that works to ease paper formatting and printing to thermal printers. it currently supports 80mm, 78mm, 76mm, 58mm, 57mm and 44mm printers thermal printers. it is built with Electron.js and Node.js
Apache License 2.0
316 stars 102 forks source link

Error: Cannot find module 'electron-pos-printer', vue-electron #46

Closed Ahmed-Sarmoum closed 2 years ago

Ahmed-Sarmoum commented 3 years ago

In my case I use Vue-electron, It works fine in case development mode But this problem only appears in production mode...

Capture

Drewdan commented 3 years ago

Did you find a solution to this?

Ahmed-Sarmoum commented 3 years ago

Did you find a solution to this?

not yet :(

robert52 commented 3 years ago

If you are using electron 12 or greater just move to ipcMain and ipcRenderer, I've tried also @electron/remote but it's the same error.

Code example:


// === in renderer code

import { ipcRenderer } from 'electron';

// in some async function ...
const printers = await ipcRenderer.invoke('serivce-printer-list');
const data = [ ... ]; // data to print
const options = {
  printerName: printers[0], // a printer from the printers list
  // ... other options
};
try {
  await ipcRenderer.invoke('serivce-printer-print', (data, options));
} catch(err) {
  console.error(err);
}

// === in main code

const { app, BrowserWindow, ipcMain } = require('electron');
const { PosPrinter } = require('electron-pos-printer');

// main window init logic
const mainWindow = new BrowserWindow({ ... });

ipcMain.handle('service-printer-list', async () => {
  const webContents = mainWindow.webContents;
  const printers = webContents.getPrinters();

  return printers;
});

ipcMain.handle('service-printer-list', async (data, options) => PosPrinter.print(data, options));
odysahe commented 3 years ago

If you are using electron 12 or greater just move to ipcMain and ipcRenderer, I've tried also @electron/remote but it's the same error.

Code example:

// === in renderer code

import { ipcRenderer } from 'electron';

// in some async function ...
const printers = await ipcRenderer.invoke('serivce-printer-list');
const data = [ ... ]; // data to print
const options = {
  printerName: printers[0], // a printer from the printers list
  // ... other options
};
try {
  await ipcRenderer.invoke('serivce-printer-print', (data, options));
} catch(err) {
  console.error(err);
}

// === in main code

const { app, BrowserWindow, ipcMain } = require('electron');
const { PosPrinter } = require('electron-pos-printer');

// main window init logic
const mainWindow = new BrowserWindow({ ... });

ipcMain.handle('service-printer-list', async () => {
  const webContents = mainWindow.webContents;
  const printers = webContents.getPrinters();

  return printers;
});

ipcMain.handle('service-printer-list', async (data, options) => PosPrinter.print(data, options));

I try this and modify the code it's working, but the printer doesn't do anything. i try set print preview: true in options, it run but not showing of the content. after I inspect the element an error appears:

image

and this my code.

 // === in main code ( background.js )
import { app, protocol, BrowserWindow, ipcMain } from 'electron'
const { PosPrinter } = require('electron-pos-printer');
const mainWindow = new BrowserWindow({ ... });
const options = {
    preview: true, // Preview in window or print
    width: 250, //  width of content body
    margin: "0px 0px 0px -10px", // margin of content body
    copies: 1, // Number of copies to print
    printerName: 'POS58 Printer(3)', // printerName: string, check it at webContent.getPrinters()
    timeOutPerLine: 500,
    silent: true,
  };
  ipcMain.handle('service-printer-print', (data) =>{
    PosPrinter.print(data, options);
  });

// === in renderer code ( invoice.vue )
import { ipcRenderer } from 'electron';
export default{
  data(){
      return {
          datass:[]
      }
  }
  methods: {
      async printr() {
            try {
              await ipcRenderer.invoke('service-printer-print', (this.datass));
            } catch(err) {
              console.error(err);
            }
      },
  }
}

And i try disable webSecurity in my vue.config.js but still not working. how about this?

Ahmed-Sarmoum commented 3 years ago

@robert52 @O-ID @Drewdan @sidneip Hi guys Are there other packages that do the same thing as an electron-post-printer?

Drewdan commented 3 years ago

I created a custom implementation of my own to handle printing using an EPOS printer, it created headings, paragraphs and tables. I can pull this into a package and put some docs together is people are interested

Drewdan commented 3 years ago

@Ahmed-dev-vpc https://www.npmjs.com/package/electron-epos-printer - created this package, it does not do as much as this one, but it builds for me. Feel free to open some issues if you want to see extra features in it.

davimatos commented 2 years ago

I have the same problem. It only happens in production. Any solution? :(

Drewdan commented 2 years ago

I believe it is an error with the node version, possibly reverting to an older version resolves it, but not ideal and this will probably upset some other package you are using. This is why I made my own package, though I have not utilized it in a while, so I am not sure how well it is working with the current version of node

davimatos commented 2 years ago

I believe it is an error with the node version, possibly reverting to an older version resolves it, but not ideal and this will probably upset some other package you are using. This is why I made my own package, though I have not utilized it in a while, so I am not sure how well it is working with the current version of node

Thanks for help. Do you have documentation for this package you created, or does it work the same as this one?

davimatos commented 2 years ago

After exhausting days of searching and debugging, I found the solution to the problem!!

Apparently it's an import error from the "vue-cli-plugin-electron-builder" package. See docs

You just need to add the following configuration:

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      // List native deps here if they don't work
      externals: ['electron-pos-printer'],
      // If you are using Yarn Workspaces, you may have multiple node_modules folders
      // List them all here so that VCP Electron Builder can find them
      nodeModulesPath: ['../../node_modules', './node_modules']
    }
  }
}
Ahmed-Sarmoum commented 2 years ago

@davimatos Yeah, that's what happened to me too :) After a very hard search, this was just the solution

// vue.config.js module.exports = { pluginOptions: { electronBuilder: { .... externals: ['electron-pos-printer'], ... } } }