PrintSpirit JSAPI User Guide


PrintSpirit offers three secondary development methods - JS API, Dynamic Link Library (DLL), and REST API - suitable for different application scenarios. They help third parties quickly integrate label printing/editing functions and develop professional printing software. See PrintSpirit Open Platform. This article serves as an introductory guide for SpiritWeb (based on JS API), detailing core steps and methods for developing barcode printing and batch barcode generation applications.

Before reading, we recommend using SpiritDesigner to design a label first and obtain the label ID.

For quick API learning, try the Interactive Test Sandbox

Application Scenarios

Spirit JS API is suitable for web applications (internet or intranet environments), providing higher quality output, more convenient operations, and finer control compared to native web printing. Ideal for various label printing requirements.

JS API is provided by the SpiritWeb module. Requires download and installation before use. For label editing, see Embedding "PrintSpirit" Label Editor

Quick Start

1. System Architecture

WEB Application Internet Intranet Browser websocket commands/responses SpiritWeb Printer

2. Reference JS

To use PrintSpirit JS API, first reference spirit.js, which automatically injects a global class: SPIRIT. Call JS APIs through SPIRIT.

HTTP method (SpiritWeb listens on two ports simultaneously to avoid port conflicts):

1<script src="http://127.0.0.1:9011/js/spirit.js"></script>
2<script src="http://127.0.0.1:19011/js/spirit.js"></script>

HTTPS method (use for HTTPS applications):

1<script src="https://127.0.0.1:9443/js/spirit.js"></script>
2<script src="https://127.0.0.1:19443/js/spirit.js"></script>

3. Execute Printing

Three printing steps:

  1. Get print instance p using SPIRIT.open command. Check if printing control is installed via SPIRIT and prompt download if needed.
  2. Complete printing with p.PrintLabel(). First parameter: template ID (How to get template ID), second: variable list (replaces template variables during printing, What are template variables?), third: template storage URL (defaults to current site).
  3. Close print instance p with p.close().

Sample code:

1SPIRIT.open(opt, function(p) {
2  p.PrintLabel(labelID, {var1:xxx, var2:xxx, ...});
3  p.close();
4})

Supports equivalent Promise/async-await syntax:

1const p=await SPIRIT.open(opt);
2p.PrintLabel(labelID, {var1:xxx, var2:xxx, ...});
3p.close();

The opt parameter in SPRINT.open specifies printer settings (printer name, type, resolution parameters, font settings, etc.), overriding defaults. Use empty object for default configuration.

Parameter Content Example Notes
name Printer name
type Printer type ZPL/CPCL/WIN
dpi Printer resolution (dots/inch) 203 Use dpi or dpmm
dpmm Printer resolution (dots/mm) 8
font Printer font MHEIGB18.TTF Invalid for Windows printers
cache Enable template cache false Default off
imgcache Enable image cache true Default on
col Labels per row 1 Default=1, auto calculates based on paper/label size
row Rows per page 1 Default=1, auto calculates based on paper/label size
master Master server IP in master-slave mode See Enterprise Intranet Solution

4. Complete Example

This example demonstrates using shared templates and setting template variables on webpage for printing. Uses template (View) ID: 953745b5-90f3-4852-805d-d11f994d2374 with two variables: name and phone.

Requirements: A printer (ZPL, CPLC, or Windows printer) with PrintSpirit control installed. First-time printing will prompt for installation if needed.

 1<!DOCTYPE html>
 2<html>
 3  <head>
 4      <title>Print Test</title>
 5      <!-- For HTTP sites, use these lines -->
 6      <script src="http://127.0.0.1:9011/js/spirit.js"></script>
 7      <script src="http://127.0.0.1:19011/js/spirit.js"></script>
 8      <!-- For HTTPS sites, use these lines 
 9      <!--script src="https://127.0.0.1:9443/js/spirit.js"></script-->
10      <!--script src="https://127.0.0.1:19443/js/spirit.js"></script-->
11  </head>
12  <body>
13  <script type="text/javascript">
14    function do_print() {
15      if (!SPIRIT) {
16        if (!confirm('Print control not detected. Install now?')) return;
17        window.location.href='http://www.printspirit.cn/download/spirit-web-setup.exe';
18      }
19
20      vars= {
21        name:document.getElementById("name").value, 
22        phone:document.getElementById("phone").value
23      };
24      SPIRIT.open({dpi:203, cache:true}, function(p) {
25        p.PrintLabel("953745b5-90f3-4852-805d-d11f994d2374", vars);
26        p.close();
27      })
28    }
29  </script>
30  <p>Name<input id="name" ></p>
31  <p>Phone<input id="phone" ></p>
32  <p><Button onclick="do_print()">Click to Print</Button></p>
33  </body>
34</html>  

Notes:
PrintSpirit supports both HTTP/HTTPS. The protocol in spirit.js references should match your main site.
To prevent port conflicts, if port 9011 (or 9443 for HTTPS) is occupied, PrintSpirit will try 19011 (19443 for HTTPS). Including both port references enables automatic port adaptation.

Other Printing Modes


Supports two additional advanced printing modes:

Command Mode

Send direct printing commands to printer.

Sample prints "Test Text" using Text command. More commands: JS API Manual

1SPIRIT.open(opt, function(p){
2    p.initLabel(80, 210);
3    p.Text(0, 0, "Test Text");
4    p.print();
5    p.close();
6})

Raw Mode

Send raw printer codes for low-level control. Suitable for serial printing with dot matrix printers.

1SPIRIT.open(opt, function(p){
2  prn=p;
3})
4
5...
6
7p.printRaw(data)
8
9...

Leave Your Message

login