从零开始写 win32 打印机任务管理的 node 模块 (1)为什么要重复造轮子?

搜索打印文件的 node 模块找到有 @thiagoelg/node-printer,node-native-printer 等等。简单的打印任务是可以胜任的,但要做到精准的控制就力不从心了。

需解决多打印机连续打印的场景,必须做到:

  1. 打印机查询:打印机列表,包含状态、打印任务

  2. 任务管理:打印、查询、取消

  3. 状态监控:状态改变回调(事件订阅模式)

只考虑在 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

API

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:

  • printer: printer of which get informations

  • returning value:

    • Windows: return only jobs in printer queue

    • Unix: return jobs in printer queue and CUPS options. Theese last depends on printer

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:

    • Windows: return an object containing main options for printer:

  • {

        "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)"

    }

    • Unix: return an object containing printer-specific options and from PPD file.

print(filename[, options, printer])

  • filename: file to print

  • options: a JSON object containing options for printer:

    • Windows: default options:

      {

          "collate": true,

          "color": true,

          "copies": 1,

          "duplex": "Default",

          "landscape": false,

          "paperSize": "",

          "fromPage": 0,

          "toPage": 0

      }

      List of supported extensions can be found here

      Notes: duplex is case sensitive, so be careful to write correctly. "paperSize" refers to size of sheet to print on; if you want to print on a paper with custom dimensions, pass "Custom.WidthxHeight" where Width and Height are integer dimensions in hundredths of inch. "fromPage": 0 means document will be printed from first page; "toPage": 0 means document will be printed from "fromPage" to last page.

    • Unix: you can use command-line options in JSON-style and/or printer-specific options retrieved from printerOptions(); i.e.:

      {

          "landscape": true,

          "n": 2,

          "sides": "two-sided-long-edge"

      }

      For options that doesn't have a value (like landscape or fit-to-page) you can assign a boolean (see above)

      It will be generated and executed a command like lp -d printerName /path/to/filename -o landscape -n 2 -o sides=two-sided-long-edge

  • Returning value (only for Unix): job id of work sent to 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[]

EN

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.

      • from Number - Index of the first page to print (0-based).

      • to Number - Index of the last page to print (inclusive) (0-based).

    • duplexMode String (optional) - Set the duplex mode of the printed web page. Can be simplex, shortEdge, or longEdge.

    • dpi Record<string, number> (optional)

      • horizontal Number (optional) - The horizontal dpi.

      • vertical Number (optional) - The vertical dpi.

    • 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 (可选)

    • success Boolean - Indicates success of the print call.

    • failureReason String - Error description called back if the print fails.

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 所以列了进来

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值