Printer

Printer

The Printer control interface is used control the receipt printer (Star TSP100).

The following API is asynchronous and is using Promise to return values.
To use this API, you first need to retrieve the printer control interface thanks to IDAL.getControlInterfaces().

It is possible to print:

  • an image located on an HTTP or HTTPS server.
  • the content of an HTML canvas (can be used to generate dynamic contents locally).

Methods

addListener(listener): Promise.<boolean>

Attach an event handler to the Printer control interface.

Since:
  • firmware v1.08

This method was introduced with firmware version v1.08 to make it possible to run the same code on the player or on a remote browser using IDAL.getRemoteControlInterfaces().
When running on the video player, this method is equivalent to window.addEventListener('idal-printer-event', function(e) {}).

Example
var printer = ... // Get the Printer interface
printer.addListener(function(event) {
    // Here the value of event.type is always 'idal-printer-event'
      switch (event.detail.type) {
          case 'printer-state':
              console.log("Printer connected : " + event.detail.state.connected);
              break;

          case 'job-state':
              var job = event.detail.job;
              console.log("Job state change for job " + job.id);
              console.log("New state: " + job.state);
              break;

          // and so on...
      }
});
Parameters
Name Type Description
listener Listener

The function to run when a printing event occurs.

Returns

A promise resolved with a boolean value.

Type
Promise.<boolean>

cancelAllJobs(): Promise.<boolean> | Promise.<ErrorStatus>

Cancel all jobs currently in the printer queue.

Returns
  • A promise resolved with a boolean value.

    Type
    Promise.<boolean>
  • A promise rejected with an ErrorStatus.

    Type
    Promise.<ErrorStatus>

cancelJob(job): Promise.<boolean> | Promise.<ErrorStatus>

Cancel a print job.

Parameters
Name Type Description
job number

is a job identifier as retrieved by getAllJobStates() or print()

Returns
  • A promise resolved with a boolean value. The promise is resolved with true if the job has been cancelled, false otherwise (no such job).

    Type
    Promise.<boolean>
  • A promise rejected with an ErrorStatus.

    Type
    Promise.<ErrorStatus>

getAllJobStates(): Promise.<Array.<JobState>> | Promise.<ErrorStatus>

Get the state of all the print jobs.

Returns

getJobState(job): Promise.<(JobState|null)> | Promise.<ErrorStatus>

Get the state of a print job.

The state of a job is only available when the job is in the print queue.

Parameters
Name Type Description
job number

The job identifier.

Returns

getState(): Promise.<PrinterState> | Promise.<ErrorStatus>

Get the state of the printer.

Returns

print(parameters): Promise.<number> | Promise.<ErrorStatus>

Print an image.

It is possible to print an image referred by an URL (HTTP or HTTPS) or an image encoded in a data uri (as of RFC 2397).
The later method can be used to print the content of an HTML canvas (see HTMLCanvasElement.toDataURL()).

The images are resized to fit the paper width, so for better results (no resize) their width must be:

  • 406px for 58mm (2 inch) paper.
  • 576px for 80mm (3 inch) paper.
Parameters
Name Type Description
parameters Object
Properties
Name Type Attributes Description
url string <optional>

An image URL (HTTP or HTTPS), can be a jpeg, png or gif.

data string <optional>

An image encoded in a data URI (see RFC 2397)

Returns
  • A promise resolved with a job identifier.

    Type
    Promise.<number>
  • A promise rejected with an ErrorStatus.

    Type
    Promise.<ErrorStatus>

removeListener(listener): Promise.<boolean>

Removes an event handler that has been attached to the Printer interface.

Since:
  • firmware v1.08
Parameters
Name Type Description
listener Listener

The function previously attached with addListener()

Returns

A promise resolved with a boolean value.

Type
Promise.<boolean>

Type Definitions

JobState

A JobState indicates the state of a print job.

Properties
Name Type Attributes Description
id number

The job identifier.

state 'queued' | 'fetching' | 'printing' | 'printed' | 'failed' | 'canceled' | 'finished' | 'unknown'

The current job state.

message string <optional>

A human readable message describing the state.

It is possible to retrieve the state of the jobs using Printer.getJobState(), Printer.getAllJobStates() or JobStateEvent

PrinterState

A PrinterState indicates the state of the printer.

Properties
Name Type Attributes Description
connected boolean

Indicates whether the printer is connected and detected.

online boolean <optional>

Indicates whether the printer is online (able to print). Only available when connected is true.

paper 'ready' | 'near-empty' | 'empty' | 'unknown' <optional>

Indicates whether the receipt paper is available. Only available when connected is true.

cover 'open' | 'closed' | 'unknown' <optional>

Indicates the state of the printer cover. Only available when connected is true.

It is possible to retrieve the printer state using Printer.getState() or PrinterStateEvent.

Events

JobStateEvent

JobStateEvent are fired whenever a print job changes.

Tutorials:
Properties
Name Type Description
type 'idal-printer-event'

The printer event type..

detail Object
Properties
Name Type Description
type 'job-state'

The job state event type.

job JobState

The state of the job that has changed.

This event shall be registered using Printer.addListener().
It can also be registered using window.addEventListener() as defined by the DOM specification.
if you plan to use the remote control interfaces, the later method must be avoided.

Type
Example
// Preferred method: registration using Printer.addListener()
var printer = ... // Get the Printer control interface
printer.addListener(function(e) {
      if (e.detail.type == 'job-state') {
          // Do something useful...
          console.log("Job " + e.detail.job.id + ": " + e.detail.job.state);
      }
});

PrinterStateEvent

PrinterStateEvent are fired whenever the printer status changes or periodically (a few minutes) when there are no changes.

Tutorials:
Properties
Name Type Description
type 'idal-printer-event'

The printer event type..

detail Object
Properties
Name Type Description
type 'printer-state'

The printer event type.

state PrinterState

The current printer state.

This event shall be registered using Printer.addListener().
It can also be registered using window.addEventListener() as defined by the DOM specification.
if you plan to use the remote control interfaces, the later method must be avoided.

Type
Example
// Preferred method: registration using Printer.addListener()
var printer = ... // Get the Printer control interface
printer.addListener(function(e) {
      if (e.detail.type == 'printer-state') {
          // Do something useful...
          console.log("Printer is connected: " + e.detail.state.connected);
      }
});