JSPrintManager

repository·master·Indexed 43 days ago

https://github.com/neodynamic/jsprintmanager

A client-side printing and scanning solution that enables web applications to perform silent printing, raw command printing (ZPL, EPL, ESC/POS), and document scanning via JavaScript. It bridges web browsers to local hardware, supporting PDF, DOCX, XLSX, and image printing, as well as bidirectional communication over Serial, TCP/IP, Bluetooth, USB, HID, and IPP.

Tokens
2.5K
Snippets
4
Records
14
Agent score
5%

What's inside JSPrintManager

  1. Overview of JSPrintManager

    master
    JSPrintManager is a client-side printing and scanning solution that uses a combination of a JavaScript library and a lightweight native client application. It allows web applications to send raw data, text, native commands, and various file formats (PDF, TIFF, TXT, DOC, XLS, JPG, PNG, etc.) directly to printers installed on the client machine without displaying a print dialog box. It is compatible with any web platform (ASP.NET, PHP, Django, etc.) and any modern JavaScript framework (Angular, React, Vue, etc.).
  2. How JSPrintManager components work together

    master

    JSPrintManager operates using two primary components:

    1. The Website component (JSPrintManager.js): A JavaScript object integrated into your website used to generate "Client Print Jobs."
    2. The Client-side component: A lightweight native application installed on the client machine (Windows, Linux, macOS, or Raspberry Pi) that receives and processes the "Client Print Jobs" generated by the JavaScript object.

    This architecture enables silent printing and scanning from sandboxed environments like web browsers.

  3. Manage printers and monitor print jobs

    master

    JSPrintManager provides capabilities to interact with the local printing environment:

    • Printer Detection: Detect when a printer is added, created, updated, or removed.
    • Printer Types: Distinguish between virtual and real-physical printers.
    • Job Monitoring: Trace the status of print jobs from JavaScript.
    • Mobile/Sandboxed Support: Print raw commands and PDF files from iOS, Android, ChromeOS, and other sandboxed environments.
  4. Print known file formats (PDF, DOCX, XLSX, Images)

    master

    You can perform silent printing of various file formats with advanced settings from JavaScript:

    • PDF: Supports tray selection, paper size, rotation, and page range settings.
    • MS Word (*.doc, *.docx): Supports duplex, reverse, and page range settings.
    • MS Excel (*.xls, *.xlsx): Supports page range settings.
    • Images (*.jpg, *.png): Supports custom location, size, and page orientation settings.
    • HTML/CSS: Print HTML content directly to the client printer, including silent mode (without print dialog).
  5. Print RAW printer commands (ZPL, EPL, ESC/POS, etc.)

    master

    JSPrintManager allows you to send raw printer commands directly to various printer languages. This is useful for label printers and receipt printers that require specific command sets. Supported languages include:

    • Zebra: ZPL and EPL commands (including Unicode UTF8 support).
    • Epson: ESC/POS commands (can be generated using JSESCPOSBuilder).
    • Intermec: IPL commands.
    • BOCA: FGL commands.
    • Datamax: DPL commands.
    • Godex: EZPL commands.

    You can print these commands from Web APIs, WebServices, and both remote and local files.

  6. Initialize JSPrintManager and establish WebSocket connection

    master

    To use JSPrintManager, you must start the library and connect to the local WebSocket server. You can enable automatic reconnection using JSPM.JSPrintManager.auto_reconnect = true.

    Use JSPM.JSPrintManager.start(useSSL, host, port) to initiate the connection.

    It is common practice to assign your application instance to JSPM.JSPrintManager.MainApp so that the library's WebSocket event handlers can call back into your application logic.

    JSPM.JSPrintManager.auto_reconnect = true;
    JSPM.JSPrintManager.start(true, 'localhost', 29443);
    
    // Link your app instance to handle callbacks
    JSPM.JSPrintManager.MainApp = this;
  7. Get printer and device information

    master

    JSPrintManager provides APIs to query the state and capabilities of client hardware:

    • Printers Info: Retrieve a list of installed printers and details such as DPI Resolution, PortName, Is Connected, Is Shared, Is Local, Is Network, and whether it is a Virtual or Real/Physical printer. You can also retrieve supported Papers and trays/bins.
    • Printers Watcher (Windows only): Detect when printers are added, removed, or modified.
    • Print Jobs Status: Trace the status of print jobs, including completion and number of pages printed.
    • Bluetooth Devices Info: Get a list of available Bluetooth devices including Name, Address, Class of Device, Connected, Remembered, and Authenticated status.
  8. Supported Printing and Scanning Features

    master

    JSPrintManager supports a wide range of capabilities:

    Printing

    • Raw Data Printing: Send commands for EPSON ESC/POS, HP PCL, PostScript, Zebra ZPL, and Eltron EPL. Supports shipping labels (UPS, FedEx, etc.) and PRN files.
    • Advanced PDF Printing: Silent printing with settings for Tray Name, Paper Size, Rotation, Pages Range, Duplex, and Password-protection.
    • File Printing: Native support for PDF, TXT, PNG, JPG, and BMP. Support for DOC/x, XLS/x, and Multipage TIFF (requires external software like MS Word, Excel, or LibreOffice).
    • Advanced DOC/XLS Printing: Silent printing for Word and Excel files (Windows only, requires MS Office 97+).
    • Tray & Paper Size: Specify Tray Name and Paper Size for PDF, JPEG, PNG, BMP, and TXT files (requires printer driver support).

    Scanning & Imaging

    • Web Scan: Scan via TWAIN/WIA/SANE-compatible devices with settings for DPI, Pixel Mode (Grayscale, Color, B&W), and Output Format (JPG, PNG, TIF, PDF). Duplex/ADF supported on Windows.
    • Video Snapshots: Capture snapshots from video devices in Grayscale, Color, JPG, PNG, or PDF formats.

    Bidirectional (BIDI) Communication

    • Serial: RS-232, COM, and TTY ports.
    • TCP/IP: Send/Receive data to any TCP Address and port (must be in the client's allow list).
    • Bluetooth: Send/Receive data to BT Address and Channel (must be in the client's allow list).
    • USB: Send/Receive data to any USB device.
    • HID: Send/Receive data to any HID device.
    • IPP: Send/Request IPP JSON messages to IPP URI devices (must be in the client's allow list).
  9. Handle JSPrintManager WebSocket events

    master

    The JSPM.JSPrintManager.WS object provides hooks to respond to the lifecycle of the WebSocket connection. You can monitor connection status, errors, and closures.

    Available event handlers:

    • onOpen: Triggered when the connection is successfully established.
    • onError: Triggered when a WebSocket error occurs.
    • onStatusChanged: Triggered when the connection status changes.
    • onClose: Triggered when the connection is closed.

    Use JSPM.WSStatus to map the numeric WS.status to a human-readable string (e.g., "Open").

    JSPM.JSPrintManager.WS.onOpen = function() {
        console.log("Connected!");
    };
    
    JSPM.JSPrintManager.WS.onError = function (e) {
        console.log("WebSocket Error:", e);
    };
    
    JSPM.JSPrintManager.WS.onStatusChanged = function() {
        const status = JSPM.WSStatus[JSPM.JSPrintManager.WS.status];
        console.log("Status changed to:", status);
    };
    
    JSPM.JSPrintManager.WS.onClose = function() {
        console.log("Connection closed");
    };