电子无声打印 [英] Silent printing in electron

查看:20
本文介绍了电子无声打印的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在构建一个电子应用程序.我的本地文件系统上有一个 PDF,需要静默打印出来(在默认打印机上).我遇到了节点打印机库,但它似乎对我不起作用.有没有简单的解决方案来实现这一点?

I am currently building an electron app. I have a PDF on my local file system which I need to silently print out (on the default printer). I came across the node-printer library, but it doesn't seem to work for me. Is there an easy solution to achieve this?

推荐答案

首先,几乎不可能理解无声"打印的含义.因为一旦您向系统打印机发送打印订单,您将完全无法保持沉默.例如,在 Windows 上,一旦下达订单,至少系统托盘图标将指示正在发生的事情.也就是说,电子打印有很好的描述功能,甚至静音"也是其中之一:

Well first of all it is near impossible to understand what you mean with "silent" print. Because once you send a print order to your system printer it will be out of your hand to be silent at all. On Windows for example once the order was given, at least the systemtray icon will indicate that something is going on. That said, there are very good described features for printing with electron even "silent" is one of them:

如果没有,您需要获得所有系统打印机想使用默认打印机:

You need to get all system printers if you do not want to use the default printer:

contents.getPrinters()

这将返回一个 PrinterInfo[] 对象.

Which will return a PrinterInfo[] Object.

以下是 electron PrtinerInfo 文档中的对象外观示例:

{
  name: 'Zebra_LP2844',
  description: 'Zebra LP2844',
  status: 3,
  isDefault: false,
  options: {
    copies: '1',
    'device-uri': 'usb://Zebra/LP2844?location=14200000',
    finishings: '3',
    'job-cancel-after': '10800',
    'job-hold-until': 'no-hold',
    'job-priority': '50',
    'job-sheets': 'none,none',
    'marker-change-time': '0',
    'number-up': '1',
    'printer-commands': 'none',
    'printer-info': 'Zebra LP2844',
    'printer-is-accepting-jobs': 'true',
    'printer-is-shared': 'true',
    'printer-location': '',
    'printer-make-and-model': 'Zebra EPL2 Label Printer',
    'printer-state': '3',
    'printer-state-change-time': '1484872644',
    'printer-state-reasons': 'offline-report',
    'printer-type': '36932',
    'printer-uri-supported': 'ipp://localhost/printers/Zebra_LP2844',
    system_driverinfo: 'Z'
  }
}

要打印您的文件,您可以使用

To print your file you can do it with

contents.print([options])

这些选项在 docs for contents.print():

  • 选项对象(可选):
  • silent Boolean(可选)- 不询问用户打印设置.默认为 false.
  • printBackground 布尔值(可选)- 还打印网页的背景颜色和图像.默认为 false.
  • deviceName 字符串(可选)- 设置要使用的打印机设备名称.默认为 ''.

打印窗口的网页.当 silent 设置为 true 时,如果 deviceName 为空且打印的默认设置,Electron 将选择系统的默认打印机.

Prints window’s web page. When silent is set to true, Electron will pick the system’s default printer if deviceName is empty and the default settings for printing.

在网页中调用window.print()相当于调用webContents.print({silent: false, printBackground: false, deviceName: ''}).

Calling window.print() in web page is equivalent to calling webContents.print({silent: false, printBackground: false, deviceName: ''}).

使用 page-break-before: always; CSS 样式强制打印到新页面.

Use page-break-before: always; CSS style to force to print to a new page.

因此,您只需将 PDF 加载到隐藏窗口中,然后触发在电子中实现的打印方法,并将标志设置为静默.

So all you need is to load the PDF into a hidden window and then fire the print method implemented in electron with the flag set to silent.

// In the main process.
const {app, BrowserWindow} = require('electron');
let win = null;

app.on('ready', () => {
  // Create window
  win = new BrowserWindow({width: 800, height: 600, show: false });
  // Could be redundant, try if you need this.
  win.once('ready-to-show', () => win.hide())
  // load PDF.
  win.loadURL(`file://directory/to/pdf/document.pdf`);
 // if pdf is loaded start printing.
  win.webContents.on('did-finish-load', () => {
    win.webContents.print({silent: true});
    // close window after print order.
    win = null;
  });
});

不过,让我给你一点警告:一旦你开始打印,它就会变得令人沮丧,因为那里有驱动程序会以稍微不同的方式解释数据.这意味着可以忽略边距等等.由于您已经拥有 PDF,因此这个问题很可能不会发生.但是如果你想使用 这个方法例如 contents.printToPDF(选项,回调).那beeing说有很多选项可以避免感到沮丧,比如使用这个问题中描述的预定义样式表:打印:如何将每页的页脚粘贴到底部?

However let me give you a little warning: Once you start printing it can and will get frustrating because there are drivers out there which will interpret data in a slightly different way. Meaning that margins could be ignored and much more. Since you already have a PDF this problem will most likely not happen. But keep this in mind if you ever want to use this method for example contents.printToPDF(options, callback). That beeing said there are plently of options to avoid getting frustrated like using a predefined stylesheet like descriped in this question: Print: How to stick footer on every page to the bottom?

如果你想在 electron 中搜索功能并且不知道它们可以隐藏在哪里,你所要做的就是去所有"文档并使用你的搜索功能:https://electron.atom.io/docs/all/

If you want to search for features in electron and you do not know where they could be hidden, all you have to do is to go to "all" docs and use your search function: https://electron.atom.io/docs/all/

问候,大金

这篇关于电子无声打印的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆