我只需要使用电子js来构建我的桌面应用程序,我使用简单的BrowserWindow在应用程序中加载我的网站。
我添加了一些功能,当连接问题时重新加载窗口,这样当互联网再次打开时,应用程序将重新加载页面,这样它就不会显示“找不到页面”。
在我的网页上,它收到一个订单并将其打印到收据打印机,我不想显示打印对话框,有什么解决方案可以静默打印收据吗?
我知道如何使用firefox静默打印它,但我现在需要在我的电子应用程序中使用它。
我的代码:
const electron = require('electron')
const app = electron.app
const BrowserWindow = electron.BrowserWindow
const path = require('path')
const url = require('url')
let mainWindow
function createWindow () {
mainWindow = new BrowserWindow({
width: 800,
height: 600,
minWidth: 800,
minHeight: 600,
icon: __dirname + '/icon.ico'
})
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))
mainWindow.on('closed', function () {
mainWindow = null
})
}
app.on('ready', createWindow)
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', function () {
if (mainWindow === null) {
createWindow()
}
})
发布于 2017-10-30 22:15:10
有BrowserWindow.webContents.print
的silent
选项
打印窗口的网页。当
silent
设置为true
时,如果deviceName
为空且为打印的默认设置,电子将选择系统的默认打印机。
在网页中调用window.print()
相当于调用webContents.print({silent: false, printBackground: false, deviceName: ''})
。
let win = new BrowserWindow(params);
win.webContents.print({silent: true});
发布于 2018-12-28 06:40:46
我不知道这是否对你的特定情况有帮助,但我遇到了一个问题,我需要从Windows上运行的Electron应用程序将原始文本打印到附加了几个命令代码(Epson ESC/P)的点阵打印机上。我最终所做的是将纯文本连同命令代码一起写入.txt文件,然后将该文件传递给Windows的“打印”命令。它打印无声,效果很好。您可能会遇到的唯一问题是,它会在作业完成后将页面的其余部分送出,尽管我不知道收据打印机是否会做同样的事情。下面是我使用的代码:
var fs = require('fs');
var printString = "whatever text you need to print with optional ascii commands";
var printer = "lpt1";
var tmpFileName ="c:\tmp.txt";
fs.writeFileSync(tmpFileName,printString,"utf8");
var child = require('child_process').exec;
child('print /d:' + printer + ' "' + tmpFileName + '"');
' printer‘变量可以是lpt1/lpt2或网络打印机共享。请参阅此处的print命令参考:
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/print
我还没有尝试过,但我确信可以使用lpr命令在Mac/Linux上解决类似的问题。
不管怎样,希望这能帮到什么人。我花了一天的时间,试图找到一种原生的电子方式,使用打印机的内置字体打印到我们的旧点阵上,结果发现,只需发出一个简单的Windows命令就足够了。
https://stackoverflow.com/questions/47017260
复制相似问题