Printer : Printer operation

更新时间:
2024-05-15
下载文档

Printer : Printer operation

This module provides printer functionality, EdgerOS App can use this module to print files in various formats.

User can use the following code to import the Printer module.

var Printer = require('printer');

EdgerOS apps needs permission.printer permission to use the printer. For details, please refer to permission.

Support

The following shows Printer module APIs available for each permissions.

 User ModePrivilege Mode
Printer
Printer.support
Printer.list
Printer.scan 
Printer.add 
Printer.delete 
Printer.pause 
Printer.resume 
Printer.jobs 
Printer.cancel 
Printer.dones 
Printer.rmdones 
Printer.testpage 
printer.name
printer.print
printer.state
printer.capabilities

Printer Class

new Printer(name)

  • name {String} Printer name.
  • Returns: {Object} Printer object.

Create a printer object, name must be a printer name that has been added to the system.

Example

Printer.list(function(error, printers) {
  if (printers && printers.length > 0) {
    var p = new Printer(printers[0].name);
  }
});

Printer.support()

  • Returns: {Boolean} Whether the current system support printer.

Get whether the current system support printer, If not supported, this module cannot be used. This API is available on EdgerOS 1.9.9 and later.

Printer.list(callback)

  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • printers {Array} Array of printer information.

Get the list of printers currently added to the system. Each item of the printers array has the following members:

  • uri {String} Printer URI.
  • name {String} Printer name.
  • model {String} Printer model, eg: Canon MF230 Series.
  • state {String} Printer state. The possible states are shown in the table below:
StateDescription
'idle'Printer is idle.
'stopped'Printer is paused.
'processing'Printer is working.
'offline'Printer is offline.

Example

Printer.list(function(error, printers) {
  if (printers) {
    for (var p of printers) {
      console.log(p.name, p.model);
    }
  }
});

Printer.scan(callback)

  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • printers {Array} Array of printer detailed information.

Scan printers currently online and get detailed information. Each item of the printers array has the following members:

  • id {String} Printer device ID.
  • uri {String} Printer URI.
  • info {String} Printer information.
  • class {String} Printer class, eg: network.
  • model {String} Printer model, eg: Canon MF230 Series.
  • location {String} Printer location, usually empty.

Printer.add(uri, name, callback)

  • uri {String} Printer URI.
  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Add a scanned printer to the system and name this printer name, name cannot contain spaces. The added printer can be used by other EdgerOS apps.

Example

Printer.scan(function(error, printers) {
  if (printers && printers.length > 0) {
    Printer.add(printers[0].uri, 'P0', ...);
  }
});

Printer does not allow duplicate names.

Printer.delete(name[, callback])

  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Delete the specified printer that has been added to system. EdgerOS apps will not be able to use the printer after the printer is deleted.

Printer.pause(name[, callback])

  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Pause the specified printer, you can use Printer.resume to resume.

Printer.resume(name[, callback])

  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Resume the specified paused printer.

Printer.jobs(name, callback)

  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • jobs {Array} The current print jobs queue for the specified printer.

Get the current print jobs queue for the specified printer. Each item of the jobs array has the following members:

  • id {Integer} Job ID.
  • info {String} Job information.
  • format {String} Job format.
  • appid {Integer} The appid that this job initiated.

If appid is undefined, it means that this job may be initiated by the cloud host, Neither info nor format can be obtained for this kind of job.

If appid is -1, it means that the app that has been removed.

Printer.cancel(name[, id][, callback])

  • name {String} Printer name.
  • id {Integer} Job ID.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Cancel one or all of the queued print jobs, if the id parameter does not exist, or the id is undefined to clear the print job queue.

Example

// Cancel a print job with ID 30
Printer.cancel(name, 30, () => {});

// Cancel all jobs
Printer.cancel(name, () => {});
Printer.cancel(name, undefined, () => {});

Printer.dones(name, callback)

  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • jobs {Array} The completed jobs queue for the specified printer.

Get the queue of print jobs completed by the printer. Each item of the jobs array has the following members:

  • id {Integer} Job ID.
  • info {String} Job information.
  • format {String} Job format.
  • appid {Integer} The appid that this job initiated.

If appid is undefined, it means that this job may be initiated by the cloud host, Neither info nor format can be obtained for this kind of job.

If appid is -1, it means that the app that has been removed.

Printer.rmdones(name[, id][, callback])

  • name {String} Printer name.
  • id {Integer} Job ID.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Delete one or all of printer jobs completed record. if the id parameter does not exist, or the id is undefined to remove all.

Printer.testpage(name, callback)

  • name {String} Printer name.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.

Print a test page using the specified printer.

Printer Object

printer.name

  • {String} Printer name.

The name attribute is the same as the parameter when the printer object was created.

printer.print(output, format, info[, opt], callback)

  • output {Buffer | Readable} What needs to be printed.
  • format {String} Output content format.
  • info {String} Job information.
  • opt {Object} Print options.
  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • id {Integer} Job ID.

Submit a print job and the printer will print in the order of the job.

Supported document formats(case insensitive):

  • TXT | TEXT
  • PDF
  • GIF
  • BMP
  • PNG
  • JPG | JPEG
  • TIFF

Print options can contain the following:

  • color {String} 'color' or 'grayscale'. default: 'color'.
  • copies {Integer} Number of copies (1 ~ 20). default: 1.
  • range {Object} PDF file page range.
    • start {Integer} Start page number. default: 1.
    • end {Integer} End page number. default: last page number.
  • sides {Integer} Single and double-sided printing. default: 1. single side.
  • media {String} Page size. for example: ISO A4 paper is 'iso_a4_210x297mm'. default: The printer decides.
  • quality {String} An enumeration specifying the desired print quality. draft, normal, and high quality. default: The printer decides.

opt.sides values are as follows:

  • 1: Single page.
  • 2: Two sided long edge.
  • 3: Two sided short edge.

Example

// Submit a print job using stream.Readable
var output = fs.createReadStream('./a.pdf');
printer.print(output, 'pdf', 'a.pdf', (error, id) => {
  if (error == undefined) {
    console.log('submit ok, ID:', id);
  }
});

// Submit a print job using buffer
var chunk = fs.readFile('./b.jpg');
printer.print(chunk, 'jpg', 'b.pdf', (error, id) => {
  if (error == undefined) {
    console.log('submit ok, ID:', id);
  }
});

The media and quality option is available in EdgerOS 2.2.2 and above.

printer.state(callback)

  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • state {String} Printer state.

Get the state of the specified printer. The possible states are shown in the table below:

StateDescription
'idle'Printer is idle.
'stopped'Printer is paused.
'processing'Printer is working.
'offline'Printer is offline.

printer.capabilities(callback)

  • callback {Function} Callback function.
    • error {Error} Specify error message on error.
    • caps {Object} Printer caps.

Get printer capability attributes of the specified printer. The caps object has the following properties:

  • color {Boolean} Whether to support color printing.
  • sides {Integer} Whether to support double-sided printing. 1: one side, 2: two sides.
  • copies {Integer} Maximum number of print copies supported.
  • range {Boolean} Whether to support printing range selection.
  • media {Object} Page size information.
    • support {StringArray} List of paper sizes supported by the printer.
    • ready {StringArray} Printer-ready list of paper sizes.
  • quality {StringArray} A list of supported print qualities, for example draft, normal, and high.

This feature is supported in EdgerOS 2.0.6 and above. The media and quality option is available in EdgerOS 2.2.2 and above.

文档内容是否对您有所帮助?
有帮助
没帮助