I have an Electron application where I have been working for a while. Recently I added a functionality that let's me print a ticket. It does open the window to print the ticket but sends nothing to the printer. Anyone knows whats happening? If I try this on the web application it does work and prints the ticket but I need that on electron app. I'll paste here my code
Main.js: (I think this is where the problem resides)
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 2024,
height: 1768,
minWidth: 400,
minHeight: 200,
resizable: true,
backgroundColor: '#222',
center: true,
autoHideMenuBar: true,
icon: 'produccion.ico',
label: 'FromScratch',
title: '',
webPreferences: {
nativeWindowOpen: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js'),
contextIsolation: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('Produccion/dist/index.html')
mainWindow.webContents.openDevTools();
mainWindow.maximize()
// Open the DevTools.
// mainWindow.webContents.openDevTools()
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)
component.ts: (Only calls the button that activates the printing)
imprimir(datos) {
this.optionCode = datos['optionCode']
this.lineaPrint = datos['Linea']
this.referencia = datos['Referencia']
this.orden = datos['Orden']
this.descripcion = datos['Descripcion']
this.cantidad = datos['Cantidad']
/* setTimeout(() => {
let printContents = document.getElementById('print-section').innerHTML;
let popupWin = window.open('', '_blank', 'top=0,left=0,height=100%,width=auto');
popupWin.document.open();
popupWin.document.write("\n <html>\n <head>\n <title>KIT</title>\n </head>\n <body onload=\"window.print();window.close()\">" + printContents + "</body>\n </html>");
popupWin.document.close();
}, 500);
*/
setTimeout(() => {
this.print.nativeElement.click();
}, 500);
}
component.html: (The section im printing and the button that prints the section)
<div id="print-section" hidden="true" style="background-color: white;">
<h1 style="font-size:2em">KIT - {{lineaPrint}}</h1>
<ngx-qrcode [elementType]="elementType" [value] = "optionCode" cssClass = "aclass" errorCorrectionLevel = "L"></ngx-qrcode>
<hr>
<p style="font-size:2em">MODELO: {{referencia}}</p>
<hr>
<p style="font-size:2em">OF: {{orden}}</p>
<hr>
<p style="font-size:2em">{{descripcion}} - {{cantidad}} unidades</p>
</div>
<button #print hidden="true" printTitle="report" printSectionId="print-section" ngxPrint>print</button>
This is a picture that I get when im about to print:
