搜索打印文件的 node 模块找到有 @thiagoelg/node-printer,node-native-printer 等等。简单的打印任务是可以胜任的,但要做到精准的控制就力不从心了。
需解决多打印机连续打印的场景,必须做到:
-
打印机查询:打印机列表,包含状态、打印任务
-
任务管理:打印、查询、取消
-
状态监控:状态改变回调(事件订阅模式)
只考虑在 win 平台上的实现,所以用了 Win32 Spooler + ndoe 实现
此前调研过几个模块也分别列出来
1 @thiagoelg/node-printer
Source
https://github.com/thiagoelg/node-printer
Features:
-
no dependecies;
-
native method wrappers from Windows and POSIX (which uses
CUPS 1.4/MAC OS X 10.6
) APIs;
-
compatible with node v0.8.x, 0.9.x and v0.11.x (with 0.11.9 and 0.11.13);
-
compatible with node-webkit v0.8.x and 0.9.2;
-
getPrinters() to enumerate all installed printers with current jobs and statuses;
-
getPrinter(printerName) to get a specific/default printer info with current jobs and statuses;
-
getPrinterDriverOptions(printerName) (
POSIX
only) to get a specific/default printer driver options such as supported paper size and other info
-
getSelectedPaperSize(printerName) (
POSIX
only) to get a specific/default printer default paper size from its driver options
-
getDefaultPrinterName() return the default printer name;
-
printDirect(options) to send a job to a specific/default printer, now supports
CUPS options
passed in the form of a JS object (see cancelJob.js example). To print a PDF from windows it is possible by using
node-pdfium module
to convert a PDF format into EMF and after to send to printer as EMF;
-
printFile(options) (
POSIX
only) to print a file;
-
getSupportedPrintFormats() to get all possible print formats for printDirect method which depends on OS. RAW and TEXT are supported from all OS-es;
-
getJob(printerName, jobId) to get a specific job info including job status;
-
setJob(printerName, jobId, command) to send a command to a job (e.g. 'CANCEL' to cancel the job);
-
getSupportedJobCommands() to get supported job commands for setJob() depends on OS. 'CANCEL' command is supported from all OS-es.
2 node-native-printer
source
https://github.com/MatteoMeil/node-native-printer
listPrinters()
return an array with all installed printers
defaultPrinterName()
return the default printer name
setPrinter(printer)
set the printer to work on
getCurrentPrinter()
get current printer you are working on
printerInfo(printer)
return general info about current working printer such jobs and options:
printerOptions(printer)
return printer-specific options:
-
printer: name of the printer to print on. If not specified it will print on previously setted printer. If printer is not set it will print on default printer.
-
Returning value:
-
-
{
"Collate": "array containing collation options",
"Duplexing": "array containing collation options",
"MaxCopy": "max number of copies you can send to printer",
"SupportsColor": "boolean indicating whether a print can print with colors",
"PaperSheets": "available paper formats supported from printer. If custom is present it can be submitted custom width and height",
"Resolutions": "printer resolutions (i.e.: High, Medium)"
}
-
print(filename[, options, printer])
printText(text[, options, printer])
Same as print() but you can pass directly a string of text on the first parameter.
3 Electron webContents.print
contents.getPrinters()
获取系统打印机列表
返回
PrinterInfo[]
contents.print([options], [callback])
-
options Object (可选)
-
-
silent Boolean (optional) - Don't ask user for print settings. 默认值为 false.
-
printBackground Boolean (optional) - Prints the background color and image of the web page. 默认值为 false.
-
deviceName String (optional) - Set the printer device name to use. Must be the system-defined name and not the 'friendly' name, e.g 'Brother_QL_820NWB' and not 'Brother QL-820NWB'.
-
color Boolean (optional) - Set whether the printed web page will be in color or grayscale. 默认值为 true。
-
margins Object (optional)
-
-
marginType String (optional) - Can be default, none, printableArea, or custom. If custom is chosen, you will also need to specify top, bottom, left, and right.
-
top Number (optional) - The top margin of the printed web page, in pixels.
-
bottom Number (optional) - The bottom margin of the printed web page, in pixels.
-
left Number (optional) - The left margin of the printed web page, in pixels.
-
right Number (optional) - The right margin of the printed web page, in pixels.
-
landscape Boolean (optional) - Whether the web page should be printed in landscape mode. 默认值为 false.
-
scaleFactor Number (optional) - The scale factor of the web page.
-
pagesPerSheet Number (optional) - The number of pages to print per page sheet.
-
collate Boolean (optional) - Whether the web page should be collated.
-
copies Number (optional) - The number of copies of the web page to print.
-
pageRanges Object[] (optional) - The page range to print. On macOS, only one range is honored.
-
-
duplexMode String (optional) - Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.
-
dpi Record<string, number> (optional)
-
-
header String (optional) - String to be printed as page header.
-
footer String (optional) - String to be printed as page footer.
-
pageSize String | Size (optional) - Specify page size of the printed document. Can be A3, A4, A5, Legal, Letter, Tabloid or an Object containing height.
-
callback Function (可选)
-
When a custom pageSize is passed, Chromium attempts to validate platform specific minimum values for width_microns and height_microns. Width and height must both be minimum 353 microns but may be higher on some operating systems.
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.
Use page-break-before: always; CSS style to force to print to a new page.
Example usage:
const options = {
silent: true,
deviceName: 'My-Printer’,
pageRanges: [
from: 0, to: 1
win.webContents.print(options, (success, errorType) => {
if (!success) console.log(errorType)
最后一个不是 node,因为是 electron 的原始 API,正好项目用了 electron 所以列了进来
为了连接工作马,它使用COM端口或USB Type A(USB使用虚拟COM端口驱动程序与
打印机
进行通信)
$ npm i
node
-pirit-printer --save
var connect = require('
node
-pririt-printer').connect;
connect(function(err, pirit) {
if (err) return console.error(err);
pirit
//wrote some text
.writeLine("Оно может сцепляться")
.writeLine("Можно напечатать много всего инетересного, но не по те
Visual
C++
6.0是开发Windows应用程序的强大工具,但是要通过它实现程序的
打印
功能,一直是初学者的一个难点,经常有朋友询问如何在VC中实现
打印
功能,他们往往感到在MFC提供的框架内实现这个问题很复杂,不知道如何下手。本例针对这个问题,介绍一种简单的方法实现文字串的
打印
功能,读者朋友可以在此基础上稍微改动一下,就可以实现文件、图像的
打印
功能。
一、实现方法
返回了一个:message信息?而这个message还不是后端给的。
最让人生气的是。打开浏览器控制台,居然可以看到401以及后端给的message!
在多方求助以后,发现。
打印
error是没有用的,这个时候,应该
打印
error.response
然后你想要的status和code以及dada里面的数据都有了
Windows和POSIX(使用 )API的本机方法包装器;
与节点v0.8.x,0.9.x和v0.11.x兼容(具有0.11.9和0.11.13);
与
node
-webkit v0.8.x和0.9.2兼容;
getPrinters()枚举具有当前作业和状态的所有已安装
打印机
;
getPrinter(printerName)获取具有当前作业和状态的特定/默认
打印机
信息;
getPrinterDriverOptions(printerName) (仅适用于 )以获
Node
.js PDF
打印
用于从
Node
.js和Electron
打印
PDF文件的实用程序。
:white_heavy_check_mark: 在Windows和类似Unix的操作系统上工作。
:white_heavy_check_mark: 支持和等标签
打印机
。
yarn add pdf-to-printer
npm install --save pdf-to-printer
将PDF文件
打印
到默认
打印机
:
import ptp from "pdf-to-printer" ;
. print ( "assets/pdf-sample.pdf" )
. then ( console . log )
. catch (